Java Using interfaces, as you learned in this chapter, you can specify similar b
ID: 3860976 • Letter: J
Question
Java
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 in ArrayList then iterates through the ArrayList, polymorphically invoking each object's getCarbonFootprint method. For each object, print some identifying information and the object's carbon footprintExplanation / 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.