[Solved] Homework 1 Solution

$25

File Name: Homework_1_Solution.zip
File Size: 178.98 KB

SKU: [Solved] Homework 1 Solution Category: Tag:
5/5 - (1 vote)

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.

  1. Create 3 small classes unrelated by inheritance: Building, Car, and Bicycle
  2. 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.
  3. Write an interface CarbonFootprint with a getCarbonFootprint
  4. Have each of your classes implement that interface so that its getCarbonFootprint method calculates the appropriate carbon footprint for that class, as specified below:
    1. Building footprint = ([monthly gas bill / 10.68] * 119.58 * 12) + ([monthly electric bill / 0.1188] * 1232 * 12)
    2. Car footprint = miles driven per year / miles per gallon * 19.82
    3. Bicycle footprint = miles traveled per month * 0.9
  5. Write an application that does the following:
    1. Creates objects of each of the 3 classes
    2. Places references to those objects in an array of CarbonFootprint
    3. Iterates through the array, polymorphically invoking each objects getCarbonFootprint method
    4. 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.

  1. Create the 4 constructors as discussed in this module/weeks presentations.
  2. Extend the appropriate superclass.
  3. Throw the InvalidFootprintException in the constructor of the Building, Car, and Bicycle classes if any of the provided parameter values are negative.
  4. 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.

Figure 1. Exercise 1 Class Diagram

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] Homework 1 Solution
$25