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

Write in java with comments ( CarbonFootprint Interface: Polymorphism) Using int

ID: 3709713 • Letter: W

Question

Write in java with comments ( CarbonFootprint Interface: Polymorphism) Using interfaces, as you learned in

this chapter, you can specify similar behaviors for possibly disparate classes.

Governments and companies worldwide are becoming increasingly concerned with

carbon footprints (annual releases of carbon dioxide into the atmosphere) from

buildings burning various types of fuels for heat, vehicles burning fuels for power, and

the like. Many scientists blame these greenhouse gases for the phenomenon called

global warming.

Create three small classes unrelated by inheritance—classes Building, Car and Bicycle.

Give each class some unique appropriate attributes and behaviors that it does not have

in common with other classes.

Write an interface CarbonFootprint with a getCarbonFootprint method. Have each of

your classes implement that interface, so that its getCarbonFootprint method calculates

an appropriate carbon footprint for that class (check out a few websites that explain

how to calculate carbon footprints).

Write an application that creates objects of each of the three classes, places references

to those objects inArrayList<CarbonFootprint>, then iterates through theArrayList,

polymorphically invoking each object’s getCarbonFootprint method. For each object,

print some identifying information and the object’s carbon footprint.

Explanation / Answer

Hi Dear,

Question is too long.

I have implemented all required classes.

//CarbonFootprint.java:

//Declare the interface CarbonFootprint.

public interface CarbonFootprint

{

     //Declare a method getCarbonFootprint() to calculate

     //the carbon foot print of car, bicycle, and building.

     double getCarbonFootprint();

     //Declare the method dispDetailsOfObject() to display

     //the details of a object of a class to calculate the

     //carbon footprints.

     void dispDetailsOfObject();

}

//ApplicationCarbonFootprints.java:

//Import the required package.

import java.util.ArrayList;

//Define the class ApplicationCarbonFootPrints.

public class ApplicationCarbonFootPrints

{

     //Start the execution of the main() method.

     public static void main( String[] args )

     {

          //Create three objects of three classes Building,

          //Car, and Bicycle.

          Building bd = new Building(4000);

          Car ca = new Car(1000, 30);

          Bicycle bi = new Bicycle(6000.00);

          //Create an array list containing carbon foot

          //prints of the objects of three classes

          //Building, Car, and Bicycle.

          ArrayList<CarbonFootprint> carbonfoot = new

          ArrayList<CarbonFootprint>();

      

          //Place the reference of the object of the class

          //Building to the array list.

          carbonfoot.add(bd);

         

          //Place the reference of the object of the class

          //Car to the array list.

          carbonfoot.add(ca);

         

          //Place the reference of the object of the class

          //Bicycle to the array list.

          carbonfoot.add(bi);

          //Start a for each loop over the array list to

          //display the carbon foot prints of Building,

          //Car, and Bicycle.

          for(CarbonFootprint dispPrint : carbonfoot)

          {

               dispPrint.dispDetailsOfObject();

               System.out.print("The total carbon foot ");

               System.out.print("print emmision is: " +

               dispPrint.getCarbonFootprint());

               System.out.println(" pounds per year");

               System.out.println("");

          }

     }

}

//Building.java:

//Define the class Building implementing the interface

//CarrbonFootPrint.

public class Building implements CarbonFootprint

{

     //Declare the required private member variables to

     //calculate the carbon foot print of a building.

     private double kiloWattHour;

     private final int period = 12;

    

     //Define the constructor of the class with the given

     //monthly consumption of power.

     public Building(double totalMonConsOfPow)

     {

          //Initialize the member variable kiloWattHour

          //with the total monthly consumption of power

          //using required setter method.

          setKiloWattHour(totalMonConsOfPow);

     }

    

     //Define the setter method setKiloWattHour() to set

     //the average kilo watt hour of monthly consumption.

     public void setKiloWattHour(double monthlyPower)

     {

          kiloWattHour = monthlyPower;

     }

    

     //Define the getter method getKiloWattHour() to return

     //the value of average kilo watt hour of power of a

     //building.

     public double getKiloWattHour()

     {

          return kiloWattHour;

     }

    

     //Override the dispDetailsOfObject() method to display

     //the monthly consumption of a building.

     public void dispDetailsOfObject()

     {

          System.out.print("Information about building ");

         System.out.print("to calculate Carbon ");

         System.out.println("footprint: ");

         System.out.print("The total consumption of ");

         System.out.print("power in a month for a ");

         System.out.println("building is: " +

         this.getKiloWattHour());

    }

    

     //Override the method getCarbonFootPrint() declared

     //in the interface CarbonFootprint to calculate the

     //carbon foot print of building.

     public double getCarbonFootprint()

     {

          return this.getKiloWattHour() * period;

     }

}

//Car.java:

//Define the class Car implementing the interface

//CarbonFootprint.

public class Car implements CarbonFootprint

{

     //Declare the required private member variables of the

     //class.

    private double disInMilesPerYear;

    private double milesPerGallon;

    private final int releasedCarbon = 10;

    //Define the constructor of the class.

    public Car(double disInMiles, double

    numOfMilesPerGallon)

    {

    disInMilesPerYear = disInMiles;

    milesPerGallon = numOfMilesPerGallon;

    }

    //Define the setter method setDistanceInMiles() to

    //initiaize the value of variable disInMilesPerYear.

    public void setDistanceInMiles(double milesPerYear)

    {

    disInMilesPerYear = milesPerYear;

    }

    //Define the setter method setMilesPerGallon() to

    //initiaize the value of variable milesPerGallon.

    public void setMilesPerGallon(double numMile)

    {

    milesPerGallon = numMile;

    }

  

    //Define the getter method getDistanceInMiles() to

    //return the value of the variable disInMilesPerYear.

    public double getDistanceInMiles()

    {

        return disInMilesPerYear;

    }

    //Define the getter method getMilesPerGallon() to

    //return the value of the variable milesPerGallon.

    public double getMilesPerGallon()

    {

        return milesPerGallon;

    }

    //Define the dispDetailsOfObject() method to display

    //the average distance of car in miles per year and

    //average of distance in miles per gallon.

    public void dispDetailsOfObject()

    {

    System.out.print("Information about car to ");

    System.out.println("calculate Carbon footprint:");

    System.out.print("The average distance in ");

    System.out.print("miles per year of a car ");

    System.out.println("is: " +

    this.getDistanceInMiles());

   

    System.out.print("The average distance in ");

    System.out.print("miles per gallon of a ");

    System.out.println("car is: " +

    this.getMilesPerGallon());

    }

    //Override the method getCarbonFootPrint() declared

    //in the interface CarbonFootprint to calculate the

    //carbon foot print of car.

    public double getCarbonFootprint()

    {

    //Return the carbon foot print of the car.

        return((getDistanceInMiles() *

        getMilesPerGallon()) * releasedCarbon);

    }

}

//Bicycle.java:

//Define the class Bicycle implementing the interface

//CarbonFootprint.

public class Bicycle implements CarbonFootprint

{

     //Declare the required private member variables.

     private double totDisInMilesPerYear;

     private final int co2Emmision = 40;

     //Define the constructor of the class with given total

     //distance in miles as parameter of the constructor.

     public Bicycle(double disInMiles)

     {

          totDisInMilesPerYear = disInMiles;

     }

    

     //Define the getter method getTotalDistance() to

     //return the value of the variable

     //totalDistanceInMiles.

     public double getmilesPerYear()

     {

          return totDisInMilesPerYear;

     }

    

     //Define the setter method setTotalDistance() to

     //initialize the variable totDisInMilesPerYear with

     //average distnace in miles per year.

     public void setTotalDistance(double disPerYear)

     {

          totDisInMilesPerYear = disPerYear;

     }

    

     //Define the dispDetailsOfObject() method to display

     //the average distance of bicycle in miles per year.

     public void dispDetailsOfObject()

     {

          System.out.print("Information about bicycle ");

          System.out.print("to calculate Carbon ");

          System.out.println("footprint:");

          System.out.print("The average distance in ");

          System.out.print("miles per year of a bicycle ");

          System.out.println("is: " +

          this.getmilesPerYear());

     }

    

     //Override the method getCarbonFootPrint() declared

     //in the interface CarbonFootprint to calculate the

     //carbon foot print of bicycle.

     public double getCarbonFootprint()

     {

          //Return the carbon footprint of the bicycle.

          return totDisInMilesPerYear * co2Emmision;

     }

}

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