Objectives:

  • Write Java statements to implement the decision structure
  • Practice introductory defensive programming by enforcing preconditions

Overview:

This week the text talks about the decision structure - a way to have your programs evaluate conditions and determine which set of statements will run. We're also going to use this week as a chance to start talking about Defensive Programming the idea that we're going to keep our application running in spite of some invalid data being passed to our methods. Now be aware that defensive programming means a lot of different things to different people. For now, let's just say we're going to check the values that get passed in to our methods and make sure they are reasonable before doing any kind of functionality with that value. We will *not* display any kind of message to our users, we'll just make sure that our object is valid by storing another more reasonable value in place of whatever was passed in. To help you see what we're talking about, the Starter File includes some Javadoc comments using the tag @precondition. Note that this is not an actual Javadoc tag, but rather something that we'll be using in our courses here. We'll make use of this tag to list out our assumptions on the values being passed, and then practice the decision structure as we enforce these preconditions.

As far as the exercise this week goes, we'll have a look at tornadoes. We'll write a Tornado class that will keep up with information about a tornado and then write some informal test code to go along with it. Along the way, we'll write some precondition and postcondition statements and have our Java enforce those preconditions.

The Problem Statement Tornado

The class you will develop is named Tornado, which will keep track of the wind speed estimate of a tornado. In addition to providing the tornado's wind speed, it will also have the ability to:

  • Apply an adjustment to the original wind speed estimate by adding or subtracting some value. Note that the adjustment must be between 10.0 and 10.0
  • Provide the corresponding intensity, based on the Enhanced Fujita Scale

Wind Speed (miles per hour) Intensity Scale
< 65 Not a tornado
65 - 85 EF0
86 - 110 EF1
111 - 135 EF2
136 - 165 EF3
166 - 200 EF4
> 200 EF5

  • Determine whether or not the tornado caused severe damage (based on the fact that an EF3 or above is severe)
  • Determine whether this Tornado is stronger than another Tornado, based on the wind speed.

Getting Started:

Be sure to read through all items before you begin.

1. Download the Zipped started file from Moodle and extract its contents to a location where you'll be able to find it later.

2. Use Eclipse to open this Java project (if you've forgotten how to do this, refer to the earlier lab that describes this process). Rename the Project YourLastNameLab03

3. Once opened, you'll find the following class/package structure: see image.

4. Go through each of the classes present and read the existing code and comments. There are a number of //TODO comments throughout the different classes giving you tips on what needs to be done. Replace each //TODO with one or more Java statements that will perform the task. Pay close attention to existing code as it may give you a hint as to how to model your code.

5. As you complete each item, be sure to compile and run the code to be sure it functions correctly. Be sure to run Checkstyle often to be sure that each of the new pieces of code is correct.

6. Once you have completed all of the //TODO tasks inside all of the classes present, be sure your code such that it passes Checkstyle without any issues in the Problems tab, compiles, runs, and delivers the correct results, next we'll turn to potential users and write an interactive program.

7. Add a new package named edu.westga.cs6311.storm.controller. Add a new class named InteractiveTornado to this package. Inside this class, declare three instance variables: one of type Scanner, one of type Tornado and one of type int.

8. Define a constructor that accepts no parameters. Inside this constructor write the code to instantiate the Scanner object, then simply assign the value null to the Tornado object and 0 to the int instance variable.

9. Define a method inputTornadoSpeed, which is a void method that accepts no parameters. Inside this method prompt the user for an int value and then read it in using a Scanner object's nextLine method.

Note: We will *not* be including any code here for error checking (because our Tornado object is already handling that for us!) This means that if the user chooses to try to create a Tornado with a speed like 6, we'll be more than happy to pass that value along to the Tornado constructor. This is actually one nice thing about defensive programming this way, the class handles invalid data as it sees fit.

Of course, as we've mentioned all along, we are making the assumption that the user will enter an integer value for the wind speed (we'll learn how to handle the situation when they don't next semester)

10. Define a method named initializeTornado, which is a void method that accepts no parameters. Use the user's input value and create the corresponding Tornado object.

11. Define a method named demonstrateTornado, which is a void method that accepts no parameters. Inside this method display the return values of all the Tornado object's accessor methods (except isStrongerThan) onto the console using println statements. Note that because you don't know the user's input values, you will not be able to print any expected values.

12. Add a new class named TornadoDemonstrationDriver to the controller package. This new class should define a main method, which will then create an InteractiveTornado object and use it to get the user's input values, create the corresponding Tornado, and display the output.

13. Run the application to confirm it works as expected.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.