In: Computer Science
Problem 5: (10 pts) Modify the patient class with two overloaded methods to display a bill for astandard visit based on age. In the first method do not use any parameters to pass in data. If the patient is over 65, then a standard visit is $75. If the patient is under 65, then the standard doctors office visit is $125. Build a second method where you pass in a discount rate. If the patient is over 65, then apply the discount rate to a standard rate of $125. Create a main method that calls both of these methods and displays the results
Hi,
Hope you are doing fine. I have coded the above question in java keeping all the conditions and requirements in mind. The line by line explanation is provided using comments that have been highlighted in bold. Before you look at the code, here are a few thing for you to take a special note of:
System.out.println("Standard vist after discount is $"+(125-discount)); with System.out.println("Standard vist after discount is $"+(125-(125*discount/100)));
Program:
//class patient
public class patient {
//declaring class attribute age of type
int
int age;
//non parameterized constructor that is
initialized when an object for patient is created
public patient(int age) {
this.age = age;
}
//displayBill() is method to display bill
for a standard visit without any input parameters
public void displayBill()
{
//if age is greater than
65
if (age>65)
{
System.out.println("Standard visit is $75");
}
else
System.out.println("Standard visit is $125");
}
//Overloading the displayBill() method by
changing its signature i.e passing a parameter discount of type
int
public void displayBill(int discount)
{
//if age is greater than
65
if(age>65)
//Here
we are subtracting the discount rate from 125
System.out.println("Standard vist after discount is
$"+(125-discount));
else
System.out.println("Standard visit is $125");
}
//main method
public static void main(String[] args) {
// TODO Auto-generated method
stub
//creating object p for
class patient with age 75
patient p=new patient(75);
//calling displayBill()
method without parameter
p.displayBill();
//calling displayBill()
method with parameter
p.displayBill(100);
}
}
Executable code snippet:
Output: