QuestionQuestion

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 method.
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:
a. Building footprint = ([monthly gas bill / 10.68] * 119.58 * 12) + ([monthly electric bill / 0.1188] * 1232 * 12)
b. Car footprint = miles driven per year / miles per gallon * 19.82
c. Bicycle footprint = miles traveled per month * 0.9
5. Write an application that does the following:
a. Creates objects of each of the 3 classes
b. Places references to those objects in an array of CarbonFootprint
c. Iterates through the array, polymorphically invoking each object’s getCarbonFootprint method
d. For each object, print identifying information, such as miles traveled or monthly bill amount, along with the object’s 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/week’s 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.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public class Bicycle implements CarbonFootprint{

    private double miles_traveled_per_month;

    /**
    * default constructor
    */
    public Bicycle() {
       this.miles_traveled_per_month = 0.0;
    }

    /**
    * constructor
    * @param miles_traveled_per_month
    */
    public Bicycle(double miles_traveled_per_month) {
       this.miles_traveled_per_month = miles_traveled_per_month;
    }

    public double getMiles_traveled_per_month() {
       return miles_traveled_per_month;
    }

    public void setMiles_traveled_per_month(double miles_traveled_per_month) {
       this.miles_traveled_per_month = miles_traveled_per_month;
    }
   
   
    @Override
    public double getCarbonFootprint() {
       return miles_traveled_per_month * 0.9;
    }
   
    @Override
    public String toString() {
      
       String s = this.getClass().getName() + ": ";
      
       s += "miles traveled per month: " + miles_traveled_per_month ;               
      
       return s;
    }
   
}...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$20.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats