Practise Problem – 6: Write a program that computes and displays the charges for
ID: 3854958 • Letter: P
Question
Practise Problem – 6: Write a program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an in-patient or an outpatient. If the patient was an in-patient, the following data should be entered: • Number of days spent in the hospital • Daily rate • Medication charges • Other services charges (lab tests, etc.) The program should ask for the following data if the patient was an out-patient: • Medication charges • Other services charges (lab tests, etc.) The program should use two overloaded functions to calculate the total charges. One of the functions should accept arguments for the in-patient data, while the other function accepts arguments for outpatient information. Both functions should return the total charges. Practise Problem – 6: Write a program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an in-patient or an outpatient. If the patient was an in-patient, the following data should be entered: • Number of days spent in the hospital • Daily rate • Medication charges • Other services charges (lab tests, etc.) The program should ask for the following data if the patient was an out-patient: • Medication charges • Other services charges (lab tests, etc.) The program should use two overloaded functions to calculate the total charges. One of the functions should accept arguments for the in-patient data, while the other function accepts arguments for outpatient information. Both functions should return the total charges. Practise Problem – 6: Write a program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an in-patient or an outpatient. If the patient was an in-patient, the following data should be entered: • Number of days spent in the hospital • Daily rate • Medication charges • Other services charges (lab tests, etc.) The program should ask for the following data if the patient was an out-patient: • Medication charges • Other services charges (lab tests, etc.) The program should use two overloaded functions to calculate the total charges. One of the functions should accept arguments for the in-patient data, while the other function accepts arguments for outpatient information. Both functions should return the total charges.Explanation / Answer
HospitalTest.java
import java.util.Scanner;
public class HospitalTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the patient was admitted 1. In-patient 2. Out-patient");
int choice = scan.nextInt();
double totalCharges = 0;
if(choice == 1) {
System.out.println("Enter Number of days spent in the hospital: ");
int days = scan.nextInt();
System.out.println("Enter Daily rate: ");
double rate = scan.nextDouble();
System.out.println("Enter Medication charges: ");
double charges = scan.nextDouble();
System.out.println("Enter Other services charges: ");
double others = scan.nextDouble();
totalCharges = getTotalCharges(days, rate, charges, others);
} else {
System.out.println("Enter Medication charges: ");
double charges = scan.nextDouble();
System.out.println("Enter Other services charges: ");
double others = scan.nextDouble();
totalCharges = getTotalCharges(charges, others);
}
System.out.println("Total charges: "+totalCharges);
}
public static double getTotalCharges(int days, double rate, double charges, double others) {
return days * rate + charges+others;
}
public static double getTotalCharges(double charges, double others) {
return charges+others;
}
}
Output:
Enter the patient was admitted
1. In-patient
2. Out-patient
1
Enter Number of days spent in the hospital:
3
Enter Daily rate:
450
Enter Medication charges:
1000
Enter Other services charges:
1500
Total charges: 3850.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.