Please help with this assignment. Goal The goal of this assignment is to lean ho
ID: 3589950 • Letter: P
Question
Please help with this assignment. Goal The goal of this assignment is to lean how to use interface and polymorphism. Tasks Your tasks are to design and implement an application to calculate carbon footprints from buildings, cars, and bicycles. Create three small classes urelated 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 getCarhonFootprint method. Have each of your classes implement that interface, so that its getCarhonFootprint method calculates an appropriate carbon footprint for that class. Please visit these websites to lean about carbon footprint and how to calculate from the following websites: /ecience.howstuffworks.com/e e/carbon https-fcarbonfund orghow-we-calculate bon Write an application that creates objects of each of the three classes, places references to those objects in AraylistarbonFootprint?, then iterates through the Array- List, polymorphically invoking each object's getCarbonEootprint method. For each object, print some identifying information and the object's carbon footprint. The attributes that are used to calculate the carbon footprints must be reasonable, but the values of those attributes do not need to be precise. In your test program, while iterating elements in the object of Arraxlist-CarbonFootprintz- print the followings: Attribute names and the values of the attributes that will be used to calculate carbon footprint Formula to compute the carbon footprint or some descriptions about how you came up with the formula The units of carbon footprint (tons of carbon year, kilograms of carbon day, etc) .The results of the carbon footprints calculation . Any additional information you want to displajy If an element of the Arraist object is Car, print the year of the car. You will need to declare instance variable year for class Car and downcast the reference of CerbenFeomzint to Car as shown here in Figure 10.9: THIS IS FIGURE 10.9 iS FOR REFERENCE TO "you will need to declare instance variable year for class Car and downcast the reference of GarbooFeotprint to Car not the solutionExplanation / Answer
CarbonFootprint.java
public interface CarbonFootprint {
double getCarbonFootprint();
}
_____________________
Building.java
public class Building implements CarbonFootprint {
private double avgMonthlyKWH;
private final int months = 12;
public Building(double avgMonthlyKWH) {
super();
this.avgMonthlyKWH = avgMonthlyKWH;
}
public double getAvgMonthlyKWH() {
return avgMonthlyKWH;
}
public void setAvgMonthlyKWH(double avgMonthlyKWH) {
this.avgMonthlyKWH = avgMonthlyKWH;
}
@Override
public double getCarbonFootprint() {
return getAvgMonthlyKWH() * months;
}
@Override
public String toString() {
return "Building [avgMonthlyKWH=" + avgMonthlyKWH + ", months=" + months + "]";
}
}
_____________________
Car.java
public class Car implements CarbonFootprint {
private double avgYearMiles;
private double avgMPG;
private final int CO2PerMile = 9;
public Car(double avgYearMiles, double avgMPG) {
super();
this.avgYearMiles = avgYearMiles;
this.avgMPG = avgMPG;
}
public double getAvgYearMiles() {
return avgYearMiles;
}
public void setAvgYearMiles(double avgYearMiles) {
this.avgYearMiles = avgYearMiles;
}
public double getAvgMPG() {
return avgMPG;
}
public void setAvgMPG(double avgMPG) {
this.avgMPG = avgMPG;
}
@Override
public double getCarbonFootprint() {
return avgMPG * avgYearMiles * CO2PerMile;
}
@Override
public String toString() {
return "Car [avgYearMiles=" + avgYearMiles + ", avgMPG=" + avgMPG + ", CO2PerMile=" + CO2PerMile + "]";
}
}
_______________________
Bike.java
public class Bike implements CarbonFootprint {
private double yearlyMiles;
private final int CO2Permile = 34;
public Bike(double yearlyMiles) {
super();
this.yearlyMiles = yearlyMiles;
}
public double getYearlyMiles() {
return yearlyMiles;
}
public void setYearlyMiles(double yearlyMiles) {
this.yearlyMiles = yearlyMiles;
}
@Override
public String toString() {
return "Bike [yearlyMiles=" + yearlyMiles + ", CO2Permile=" + CO2Permile + "]";
}
@Override
public double getCarbonFootprint() {
return yearlyMiles * CO2Permile;
}
}
______________________
Test.java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList < CarbonFootprint > arl = new ArrayList < CarbonFootprint > ();
Building b = new Building(4000);
arl.add(b);
Car c = new Car(20000, 20);
arl.add(c);
Bike bi = new Bike(2000);
arl.add(bi);
for (int i = 0; i < arl.size(); i++) {
System.out.println("CarbonFoot Print " + arl.get(i).getCarbonFootprint());
System.out.println(arl.get(i).toString());
System.out.println("___________________");
}
}
}
______________________
Output:
CarbonFoot Print 48000.0
Building [avgMonthlyKWH=4000.0, months=12]
___________________
CarbonFoot Print 3600000.0
Car [avgYearMiles=20000.0, avgMPG=20.0, CO2PerMile=9]
___________________
CarbonFoot Print 68000.0
Bike [yearlyMiles=2000.0, CO2Permile=34]
___________________
________________Thank You