Exception Handling

 

 

Complete two exercises that will allow you to practice your skill with the topics of interfaces and exceptions

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 objects getCarbonFootprint method
d. 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.

 

complete two exercises that will allow you to practice your skill on the topics of GUI design and regular expressions.

Exercise 1
Spam (or junk e-mail) costs U.S. organizations billions of dollars a year in spam-prevention software, equipment, network resources, bandwidth, and lost productivity. In this exercise, you will be doing your part to help identify the likelihood that a given message is spam by assigning a spam score to a message entered by the user.
1. Create a graphical interface that looks similar to the one shown in Figure 1 below.

Figure 1. Graphical User Interface for Exercise 1

2. Table 1 below provides a list of 30 words or phrases that commonly occur in spam messages (this list is far from exhaustive). Write an application in which the user enters an email message into a JTextArea. When the user clicks the verify button, your application must scan the message for each of the 30 keywords or phrases. For each occurrence of one of these within the message, add a point to the messages spam score.

3. After calculating the spam score of the message, the application will display the spam score to the user via a message box, similar to that shown in Figure 2.

4. When the user clicks the Clear button, erase the contents of the JTextArea box (i.e., set the text property to an empty string).

This question has been answered.

Get Answer