Answer to Question #167507 in Java | JSP | JSF for ritheesh kumar

Question #167507

1. Implement the following using java:

Consider three classes named Diagnosis, Grocery and Electricity containing variables for medical report of the patient, grocery bill, electricity and methods for getting and displaying the details from the user.

a. Diagnosis class can have instance variables for patient id, name, symptoms, COVID_Status and functions for reading these values from the user and displaying it.

b. Grocery class can have instance variables required for computing a monthly expense on grocery such as Monthly_budget, No_of_times_grocery_purchased, Average_ expense_on_each_purchase, and functions for reading these values and computing the Amount_spent_on_grocery and percentage_of_budget_alloted.

c. Electricity class can contain the instance variables such as amount of units consumed every month and electricity bill for every month and average electricity consumption for every day. It can have functions for reading the values from the user and computing the electricity bill and consumption/month. 


1
Expert's answer
2021-02-28T07:03:46-0500
import java.util.ArrayList;
import java.util.Scanner;

public class Diagnosis {
    private int patientId;
    private String name;
    private ArrayList<String> symptoms;
    private boolean COVID_Status;

    public void readData(Scanner in) {
        System.out.println("Enter the patient id:");
        patientId = Integer.parseInt(in.nextLine());
        System.out.println("Enter the name:");
        name = in.nextLine();
        symptoms = new ArrayList<>();
        System.out.println("Enter symptoms(blank line for stop)");
        String tmp;
        while (true) {
            tmp = in.nextLine();
            if (tmp.length() == 0) {
                break;
            }
            symptoms.add(tmp);
        }
        System.out.println("Does patient have COVID(yes|no)");
        tmp = in.nextLine();
        COVID_Status = tmp.equalsIgnoreCase("yes");
    }

    @Override
    public String toString() {
        StringBuilder buffer = new StringBuilder();
        buffer.append("Diagnosis: patient ID- ").append(patientId).append(", patient name- ").append(name);
        buffer.append("\nSymptoms:\n");
        for (String symptom : symptoms) {
            buffer.append(symptom).append('\n');
        }
        buffer.append("COVID_Status- ").append(COVID_Status ? "positive" : "negative");
        return buffer.toString();
    }

}
import java.util.Scanner;

public class Grocery {
    private double monthlyBudget;
    private double timesPurchased;
    private double averagePurchase;

    public void readData(Scanner in) {
        System.out.println("Monthly budget:");
        monthlyBudget = in.nextDouble();
        System.out.println("No of times grocery purchased:");
        timesPurchased = in.nextDouble();
        System.out.println("Average expense on each purchase:");
        averagePurchase = in.nextDouble();
    }

    public void computeData() {
        System.out.printf("Amount spent on grocery: %.2f\n", timesPurchased * averagePurchase);
        System.out.printf("Percentage of budget allotted: %.2f%s\n", (timesPurchased * averagePurchase) * 100 / monthlyBudget, "%");
    }
}
import java.util.Scanner;

public class Electricity {
    private int unitsPerMonth;
    private double billPerMonth;
    private double averagePerDay;

    public void readData(Scanner in) {
        System.out.println("Amount of units consumed every month:");
        unitsPerMonth = in.nextInt();
        System.out.println("Electricity bill for every month:");
        billPerMonth = in.nextDouble();
        System.out.println("Average electricity consumption for every day:");
        averagePerDay = in.nextDouble();
    }

    public void computeData() {
        System.out.println("Electricity bill " + billPerMonth);
        System.out.println("Consumption/month " + unitsPerMonth);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog