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

1. (CarbonFootprint Interface: Polymorphism) Using interfaces, you can specify s

ID: 3698425 • Letter: 1

Question

1. (CarbonFootprint Interface: Polymorphism) Using interfaces, 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. Write an interface CarbonFootprint with a
getCarbonFootprint method. Have each of your classes implement that interface, so that its
getCarbonFootprint method prints out an appropriate message (e.g., this message from
Building CarbonFoot).Write an application that creates objects of each of the three classes
and one interface reference variable, assign each object to the interface reference variable,
and then invoke each object’s getCarbonFootprint method by means of dynamic method
resolution. For each object, print a simple message (e.g., this message from Building
CarbonFoot).

Explanation / 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