Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hi, I need someone to help me with two exercises in JAVA. The exercises must be done in Java. ----------------------------------------------------------- Exercise 1 Governments and companies
Hi, I need someone to help me with two exercises in JAVA. The exercises must be done in Java.
-----------------------------------------------------------
Exercise 1
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 fuel for heat, vehicles burning fuels for power, and the like.
- Create 3 small classes unrelated by inheritance: Building, Car, and Bicycle
- Give each class the unique attributes and behaviors as specified in the class diagram in Figure 1 below. Create a single constructor for each class to allow consumers of the class to provide initial values for each attribute.
- Write an interface CarbonFootprint with a getCarbonFootprint method.
- Have each of your classes implement that interface so that its getCarbonFootprint method calculates the appropriate carbon footprint for that class, as specified below:
- Building footprint = ([monthly gas bill / 10.68] * 119.58 * 12) + ([monthly electric bill / 0.1188] * 1232 * 12)
- Car footprint = miles driven per year / miles per gallon * 19.82
- Bicycle footprint = miles traveled per month * 0.9
- Write an application that does the following:
- Creates objects of each of the 3 classes
- Places references to those objects in an array of CarbonFootprint
- Iterates through the array, polymorphically invoking each objects getCarbonFootprint method
- For each object, print identifying information, such as miles traveled or monthly bill amount, along with the objects carbon footprint.
Exercise 2
Extend Exercise 1 by creating and demonstrating the use of an InvalidFootprintException class.
- Create the 4 constructors as discussed in this module/weeks presentations.
- Extend the appropriate superclass.
- Throw the InvalidFootprintException in the constructor of the Building, Car, and Bicycle classes if any of the provided parameter values are negative.
- Add code in your main method that handles the InvalidFootprintException, and write code that demonstrates that your handler works (i.e., write code that purposefully causes the InvalidFootprintException to be thrown, but then ensure that it does not crash your program). Instead of crashing, your main method must print a message to the error stream that an invalid footprint was detected.
Building Car - milesDrivenPerYear : double - milesPerGallon : double - monthlyElectricBill : double - monthlyGasBill : double + Car(milesDrivenPerYear : double, milesPerGallong : double) + getMilesDrivenPerYear() : double + setMilesDrivenPerYear(milesDrivenPerYear : double) : void + getMilesPerGallon() : double + setMilesPerGallon(milesPerGallon : double) : void + Building(monthlyElectricBill : double, monthlyGasBill : double) + getMonthlyElectricBill() : double + setMonthlyElectricBill(monthlyElectricBill : double) : void + getMonthlyGasBill() : double + setMonthlyGasBill(monthlyGasBill : double) : void Bicycle > CarbonFootprint - milesTraveledPerMonth : double + getCarbonFootPrint() : double + Bicycle(milesTraveledPerMonth : double) + getMilesTraveledPerMonth() : double + setMilesTraveledPerMonth(milesTraveledPerMonth : double) : void
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started