LAB EXERCISE 18: Hospital Charges

Goal: In this exercise, you will learn how to:
1. Insert the Main Method (Main Entry Point to the Java Program)
2   Import a Java Class Library
3.  Create a Superclass
4.  Create Subclasses
5.  Declare Public Variables that can be access by a Subclass
6.  Create Public Void Method that can be access by a Subclass
7.  Use a Try-Catch block statement for error handling and exceptions
8.  Create Methods that returns a value to the calling object’s method
9.  Use the This keyword to reference a method in a Superclass
10.  Create the Object from the Class
11.  Call the Object’s Method

Program Specifications:
Create an application that calculates the total cost of a hospital stay.  The application should accept the following input:

·       The number of days spent in the hospital

·       The amount of medication charges

·       The amount of surgical charges

·       The amount of lab fees

·       The amount of physical rehabilitation charges

The hospital charges $350 per day.  The application should display the hospital stay charges, total miscellaneous charges and total hospital charges.
A. Pre-requisites:
1. Create a folder on your desktop Exercise-18

2. Launch Java EE- Eclipse
Note:  You will need to use the Java Perspective Workbench for this exercise

3. Setup your Eclipse Workspace to point to the Exercise-18 folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-18 folder as your Workspace.

B. Requirements:

1.  Create a Java Project and name it as HospitalCharges

2.  Create the first Class that will have the Main Method

a.       Name the Class as MainApp

b.       Choose the main method to insert into the class


3.  Create the second Class that will be the superclass

a.      Name the Class as Patient

b.      Do not select the main method stub


4.  Create a class called Charges.  This will be a subclass that will inherit the methods and variables of the Patient superclass

C. Requirements for the Patient Class:

1.  Insert the import java.util.Scanner class which will allow data to be inputted from the console. 
Insert the line @SuppressWarnings("resource") 


2.  Declare the Public variables patientID and patientName using the String data type.
Declare the Public variable dayStay using the Integer data type
NOTE:  These variables will be accessed by the subclass

3.  Create the Public Void Method called getPatient() that that will prompt the user to input the data
Note: This method executes code and does not return a value.  The method will be access by the subclass.

a.  Below is the code to prompt the user to enter the data

Scanner inputID = new Scanner(;
System.out.print("Enter Patient ID: ");
patientID = inputID.nextLine();

Scanner inputName = new Scanner(;
System.out.print("Enter Patient Name: ");
patientName = inputName.nextLine();

b.  Use the Try-Catch block statement to perform error handling.

Scanner inputDays= new Scanner(;
System.out.print("Enter length of stay in Hosptial (days) for " + patientName + ": ");
dayStay = inputDays.nextInt();        

c.   If the user inputs an invalid data, the code below in the Catch block will be executed:

System.out.println("Invalid data entered.")

























D. Requirements for the Charges Class:

1.  The Charges class will be a subclass of the Patient parent class (superclass)
NOTE: A subclass will inherit the methods and variables from the superclass

Add the keyword "extends Patient" after the name of class


2.  Declare the constant variable and initialize - final static int COSTPERDAY = 350

3.  Declare the local variables using a double data type that will be used in this subclass.
hospitalStayCharges, totalMiscCharges, totalCharges, surgicalCharges, labFees & rxCharges

4.  Create the Method - calcHospitalStayCharges() that will return a double data type value
a.  Call the this.getPatient() method from the parent class- Patient
NOTE:  Use the “this” keyword to call the method
b.  Calculate the hospitalStayCharges = dayStay* COSTPERDAY
c.  Return the result value

5.  Create the Method - calcMiscCharges() that will return a double data type value
a.  Prompt the user for the data.

Scanner inputRx = new Scanner(;
System.out.print("Enter Prescription charges for " + patientName + ": $");
rxCharges = inputRx.nextInt();

Scanner inputSurgical = new Scanner(;
System.out.print("Enter Surgical Charges: "  + patientName + ": $");
surgicalCharges = inputSurgical.nextDouble();

Scanner inputLabFees = new Scanner(;
System.out.print("Enter Lab Fees: "  + patientName + ": $");
labFees = inputLabFees.nextDouble();



b. Calculate the totalMiscCharges = rxCharges + surgicalCharges + labFees
c. Return the result value


6.  Create the Method - calcTotalCharges() that will return a double data type value
a. Calculate the totalCharges= hospitalStayCharges + totalMiscCharges

 b. Return the result value




































E. Requirements for the MainApp Class:

1.  Add comments (documentation)– Program Description, Author and Date

2.  Create the instance of object called hospital from the Charges class

3.  Call the object’s method - hospital.calcHospitalStayCharges() and store the result value to chargeStay variable

4. Call the object’s method - hospital.calcMiscCharges() and store the result value to chargeMisc variable


5. Call the object’s method - hospital.calcTotalCharges() and store the result value to chargeTotal variable

6. Display the results

// display the results on the console
System.out.print("Hospital Stay Charges: ");
System.out.format("$%,.02f", chargeStay);

System.out.print("Miscellanous Charges: ");
System.out.format("$%,.02f", chargeMisc);

System.out.print("Total Hospital Charges: ");
System.out.format("$%,.02f", chargeTotal);



























F. Test:  

1.   Save your Java code

2.   Compile and run your Java program.

3.   Verify there is no syntax, logical or run-time errors.

4.   Use the following set of test data to determine if the application is running properly









G. Submit your exercise in the Canvas Lab Exercise #18 Drop Box.

1. Submit the screen shot of the Eclipse Workbench window showing the Console output screen.
You can use Paint (save as JPG) or Word to paste the screenshot.

2. Zip up and submit the compressed HospitalCharges subfolder that is in the Exercise-18 folder.
NOTE: Right click on the
subfolder and select Send to “Compress Folder”.  The file will have a file extension of .zip.