LAB
EXERCISE 13: Rainfall Statistics
Goal:
In this exercise, you will learn how to:
1.
Insert the Main Method (Main Entry Point to the Java Program)
2 Import a Java Class Library
3. Declare Variables and Initialize
4. Create a Method which will return
data
5. Create a Method which will accept an
argument
6. Create Arrays to
store data of the same data type in memory
7. Use the For Loop to
allow the iteration and the repetitive execution of code
8. Create an Object from the Class
9. Call the Object’s Method and pass an
argument
Program
Specifications:
Create
an application that allows the user to input each month’s amount of rainfall
(in inches) and calculates the total and average rainfall of a year. The total and average rainfall is displayed
on the screen.
A. Pre-requisites:
1.
Create a folder on your desktop Exercise-13
2. Launch Java EE- Eclipse
Note: You will need to use the Java
Perspective Workbench for this exercise
3. Setup your Eclipse Workspace to point to the Exercise-13
folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-13 folder as your Workspace.
B. Requirements:
1. Create
a Java Project and name it as RainfallStatistics
2. Create
the first Class that will have the Main Method
a.
Name the Class as MainApp
b.
Choose the main method to insert into the
class
3. Create
the second Class that will have the Constructor and Methods
a.
Name the Class as Statistics
b.
Do not select the main method stub
C. Requirements for the Statistics Class:
1. Insert
the import java.util.Scanner class
which will allow data to be inputted from the console.
2. Declare
the variable- totalRainFall
and avgRainFall using the double data type.
Initialize both variables to 0
3. Declare
the array statsArray[] using
the double data type. This will store each month’s rainfall data.
4. Declare
the monthArray[] to
store the name of the month. Initialize
the values in the array. Below is the code you can copy and paste.
String
monthArray[] =
{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; |
5. Create
the Method called getRainFallStats() that
will prompt the user to input the data and accumulate the total rainfall.
a. Declare
the scanner input class objects - Scanner inputStats = new Scanner(System.in)
b. Used
the For Loop to perform the following iteration:
1. Prompt the user to input the rainfall
stats in inches for each month (Jan to Dec)
2. Store the monthly rainfall stats in
the array
3. Accumulate the total sales using a
Running Total
c. Close the scanner input class object - inputStats.close()
d. Return
the results - totalRainFall
6. Create
the Method called calcAverageRainFall() that
will calculate the average yearly rainfall
a. This method should accept the
argument parameters (double total)
b. Calculate the avgRainFall = total /12
and return the result value
D. Requirements for the MainApp Class:
1. Add
comments (documentation)– Program Description, Author and Date
2. Create
the instance of object called rainfall from the Statistics class
3. Call
the object’s method - rainfall.getRainFallStats() that
will get user data and calculate the running rainfall total. Store the returned result to the rainTotal variable using a
double data type
4. Call
the object’s method - rainfall.calcAverageRainFall(rainTotal) that
will calculate the yearly average rainfall.
Pass the rainTotal
value as an argument. Store the returned
result to the avgYearlyRain
variable using a double data type
5. Display
the returned result on the console.
Below is the code you can copy and paste.
System.out.println(); |
E. Test:
1. Save your Java code
2. Compile and run your Java program.
3. Verify there is no syntax, logical or run-time
errors.
4. Use the following set of test data to
determine if the application is calculating properly:
F. Submit your exercise in the Canvas Lab
Exercise #13 Drop Box.
1. Submit the screen shot of the Eclipse
Workbench window showing the Console output screen.
You can use Paint (save as JPG) or Word to paste the screenshot.
2. Zip up and submit the compressed RainfallStatistics subfolder that is in the Exercise-13
folder.
NOTE: Right click on the subfolder and select
Send to “Compress Folder”. The file will
have a file extension of .zip.