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

The energy used in an apartment building is dependent on the number of apartment

ID: 3650792 • Letter: T

Question

The energy used in an apartment building is dependent on the number of apartments, the number of residents in each apartment, and the energy that each resident consumes. Design the class Apartment with the private integer instance variables residents and energyConsumptionPer Resident, and the following methods; //construtor public Apartment (int numOfRes, double energyPerRes)
//accessor and mutator (getter and setter) methods for the instance variabes
public double apartmentEnergy()

design a class TestApartment. it should have a main method that bahaves 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 variables.
-Compute and print the energy consumed in that aparment by invoking your method apartmentEnergy()
-if the user evers enters a negative value for the number of residents, then loop will end.
-Compute and print the total number of energy used by all of the apartments in the apartment building and teh average 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:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote