Goals
Write math methods for finding the sum, average, high, and low values of a series of numbers and run the methods in a main program.
Skills
- Methods
- Void vs. non-void methods
- Linear search algorithm
Program Details
Your program needs a class with the following methods:
- Sum Method
- Passes in 5 floats.
- Returns the sum of the five numbers as a float.
- Average Method
- Passes in 5 floats.
- Returns the average of the 5 numbers as a float.
- highNum Method
- Passes in 5 floats.
- Returns the largest of the 5 numbers as a float.
- If you use the Math.max method I will take 1 letter grade off. Write you own code for finding max and min as you will need this algorithm many times throughout the year.
- lowNum Method
- Passes in 5 floats.
- Returns the smallest of the 5 numbers as a float.
- If you use the Math.min method I will take 1 letter grade off. Write you own code for finding max and min as you will need this algorithm many times throughout the year.
- A Main Program that will
- Ask the user to enter 5 numbers.
- Assume they will enter a valid number. You do not need to check to see if they entered a valid number.
- Call each of the 4 methods by passing in the 5 numbers.
- Outputs the results of each method to the console.
Test your methods one at a time. Don’t expect them to all work first time.
Test initially with number that you can calculate sums and averages easily in your head. For example:
- [10, 10, 10, 10, 10]: Sum = 50; Average = 10; High and Low = 10;
- [0, 0, 5, 10, 10]: Sum = 25; Average = 5; High = 10; Low = 0;
- [-10, -5, 0, 5, 10]: Sum = 0; Average = 0; High = 10; Low = -10;
Hints:
- All of your methods should be public static floats.
- Your main program will need to save all five numbers as separate variable floats.
- Your low and high num programs should be somewhat efficient. You should only have to make 4 or 5 checks, not 15 or more.
Resources
Java Textbook
Chapter 7: Object Oriented Design