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

Q2. Write a java program: (CarbonFootprintInterface: Polymorphism) Using interfa

ID: 647098 • Letter: Q

Question

Q2. Write a java program:

(CarbonFootprintInterface: Polymorphism) Using interfaces, as you learned, 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

Explanation / Answer

public class TestCarbonFootprint { /** * Main method to run test. * * @param args * - This program does not take any arguments. */ public static void main(String[] args) { TestCarbonFootprintServices service = new TestCarbonFootprintServices(); ArrayList footprintItems = new ArrayList(); double totalFootprint = 0; // Print out an introduction. service.outputIntroduction(); // Create list of different objects that implement the // CarbonFootprint interface. try { footprintItems.add(new building("Rich's House", 2, 100, 100, 100, 100, 100, 100, 0)); footprintItems.add(new Car("Rich's Mini Cooper S", 10000, 25)); footprintItems.add(new Bicycle("Rich's Bicycle", 1000, Bicycle.PowerSource.CHEESEBURGERS)); // Loop through all items and print out the information. for (CarbonFootprint item : footprintItems) { double footprint = item.getCarbonFootprint(); totalFootprint += footprint; System.out.println(" Item: " + item.toString()); System.out.println(" Carbon footprint: + service.toCommaNumber(service.toFloat(2, footprint)) + " Metric Tons of CO2"); } System.out.println(" Total carbon footprint for this session: " + service.toFloat(2, totalFootprint) + " Metric Tons of CO2"); } catch (Exception exception) { System.out.println("Error computing the carbon footprint."); } } }