Java How to Program, by Deitel, 10th Edition, Chapter 10, Making a Difference 10
ID: 3675136 • Letter: J
Question
Java How to Program, by Deitel, 10th Edition, Chapter 10, Making a Difference 10.17
10.17 ( 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
/* CarbonFootprint interface. File name as CarbonFootprint.java */
punlic interface CarbonFootprint
{
double getCarbonFootprint(); // calculates the carbon footrpint
}
// end of interface
/* Bicycle.java : Bicycle calss */
public class Bicycle implements CarbonFootprint
{
private double yearlymiles;
private final int caloriesPerMile = 25;
// constructor
public Bicycle(double miles )
{
yearlyMiles = miles;
} // end of constructor
public double getYearMiles()
{
return yearlyMiles;
}
public void setYearlyMiles(double miles )
{
yearlyMiles = miles;
}
@override
public String toString()
{
return String.format("%s : %.2f" , "Yearly miles are " , getYearlyMiles() );
}
@override
public double getCarbonFootprint()
{
return yearlyMiles * caloriesPerMile;
}
} // endof Bicycle class
/* car.java : car class */
public class Car implements CarbonFootprint
{
private double averageyearlymiles;
private final int kgCO2PerMile = 10;
// constructor
public car(double miles, double MPG )
{
averageYearlyMiles = miles;
averageMPG = MPG;
} // end of constructor
public void setAverageYearlyMiles(double miles )
{
averageYearlyMiles = miles;
}
public void setAverageMPG(double MPG )
{
averageMPG = MPG;
}
public double getAverageYearlyMiles()
{
return averageYearlyMiles;
}
public double getAverageMPG()
{
return averageMPG;
}
@override
public String toString()
{
return String.format("%s : %.2f %s : %.2f ", "Average Yearly miles is " , getAverageYearlyMiles(),"Average MPG is " , getAverageMPG());
}
@override
public double getCarbonFootprint()
{
return (( getAverageYearlyMiles() * getAverageMPG() ) * kgc02PerMile );
}
} // endof car class
/* Building.java : Building class */
public class Building implements CarbonFootprint
{
private double averagemonthlyKwh;
private final int months = 12;
// constructor
public Building(double monthlyConsumption )
{
averagemonthlyKwh = monthlyConsumption ;
} // end of constructor
public void setAverageMonthlyKwh(double monthlyConsumption )
{
averagemonthlyKwh = monthlyConsumption ;
}
public double getAverageMonthlyKwh()
{
return averageMonthlyKwh;
}
@override
public String toString()
{
return String.format("%s : %.2f ", "the monthly consumption is is " , getAverageMonthlyKwh() );
}
@override
public double getCarbonFootprint()
{
return getAverageMonthlyKwh() * months;
}
} // endof Building class
/* CarbonFootprintInterfaceTest.java : testing the interface */
// Main class
import java.util.ArrayList;
public class CarbonFootprintInterfaceTest
{
public static void main( Sting[] args )
{
Arrayist< CarbonFootprint > categories = new ArrayList< >();
//Creates array of objects of type CarbonFootprint
Cattegories.add( new Bicycle(150.00 ));
Cattegories.add( new Building(5000.28 ));
Cattegories.add( new Car(4345.75 , 34.5 ));
Cattegories.get( 0 ).getCarbonFootprint();
Cattegories.get( 1 ).getCarbonFootprint();tring
Cattegories.get( 2 ).getCarbonFootprint();
System.out.println("Data of each Object : ");
CarbonFootprint set = Cattegories.set(0, new Bicycle(150.00 ));
CarbonFootprint set = Cattegories.set(1, new Building(5000.28 ));
CarbonFootprint set = Cattegories.set(2, new Car(4345.75 , 34.5 ));
System.out.println("Data of each Object : ");
Cattegories.stream().forEach((CarbonFootprint currentObject) -> { System.out.printf(" %s: %s %s %s: %.2f ","Details for ", currentObject.getClass().getName(),currentObject.toString(),"carbon footprint is ",currentObject.getcarbonFootprint());
}); // end for loop
} // end of mian
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.