Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

design a class Apartment with private integer instance variables residents and e

ID: 3650793 • Letter: D

Question

design a class Apartment with private integer instance variables residents and energyConsumptionPerResident. and the these methods public Apartment (int numOfRes, double energyPerRes) public double apartmentEnergy() design a class test apartment it should have a main method that behaves as follows: print name of the program. In a loop, ask the user for the number of residents and the energy consumption per resident in an apartment. Create an instance of an Apartment( an object of type Apartment) with these values. compute and print the energy consumed in that apartment by invoking your method apartmentenergy. if the user ever enters a negative value for the number of residents then end the loop compute and print the total amount of energy used by all of the apartments in the apartment building and the average amount of energy used per apartment

Explanation / Answer

Please rate...

Program Apartment.java

===================================================

class Appartment
{
    private int residents;
    private double energyConsumptionPerResident;

    public Appartment(int numOfRes,double energyPerRes)
    {
        residents=numOfRes;
        energyConsumptionPerResident=energyPerRes;
    }
    public void setResidents(int r)
    {
        residents=r;
    }
    public void setEnergyConsumptionPerResident(double e)
    {
        energyConsumptionPerResident=e;
    }
    public int getResidents()
    {
        return residents;
    }
    public double getEnergyConsumptionPerResident()
    {
        return energyConsumptionPerResident;
    }
    public double apartmentEnergy()
    {
        return (residents*energyConsumptionPerResident);
    }
}

===================================================

Program TestApartment.java

===================================================

import java.util.Scanner;

class TestApartment
{
    public static void main(String args[])
    {
        System.out.println("Apartment Energy Calculation");
        int c=0;
        double sum=0;
        Scanner s=new Scanner(System.in);
        while(true)
        {
            System.out.print("Enter the number of residents: ");
            int nr=s.nextInt();
            if(nr<0)break;
            System.out.print("Enter the energy consumption per resident: ");
            double e=s.nextDouble();

            Appartment a=new Appartment(nr,e);
            System.out.println("The energy consumed in the apartment is: "+a.apartmentEnergy());
            c++;
            sum=sum+a.apartmentEnergy();
        }
        System.out.println("The total energy consumed is: "+sum);
        System.out.println("The average energy consumed is: "+(sum/c));
    }
}

===================================================

Sample output: