LAB EXERCISE 18: Hospital Charges
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
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.
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
a. Select File-> Switch Workspace
b. Browse and select your Exercie-18 folder as your Workspace.
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
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:
the import java.util.Scanner class
which will allow data to be inputted from the console.
Insert the line @SuppressWarnings("resource")
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
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
inputID = new Scanner(System.in);
b. Use the Try-Catch block statement to perform error handling.
inputDays= new Scanner(System.in);
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
the constant variable and initialize - final static int COSTPERDAY = 350
the local variables using a double data type that will be used in this
hospitalStayCharges, totalMiscCharges, totalCharges, surgicalCharges, labFees & rxCharges
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
the Method - calcMiscCharges() that will return a double data type
a. Prompt the user for the data.
inputRx = new Scanner(System.in);
b. Calculate the totalMiscCharges = rxCharges + surgicalCharges + labFees
c. Return the result value
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
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
the results on the console
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
NOTE: Right click on the subfolder and select Send to “Compress Folder”. The file will have a file extension of .zip.