Allan Didier

Math Methods

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

Program Details

Your program needs a class with the following methods:

  • Sum Method
    1. Passes in 5 floats.
    2. Returns the sum of the five numbers as a float.
  • Average Method
    1. Passes in 5 floats.
    2. Returns the average of the 5 numbers as a float.
  • highNum Method
    1. Passes in 5 floats.
    2. Returns the largest of the 5 numbers as a float.
  • lowNum Method
    1. Passes in 5 floats.
    2. Returns the smallest of the 5 numbers as a float.
  • A Main Program that will
    1. Ask the user to enter 5 numbers.
    2. Assume they will enter a valid number. You do not need to check to see if they entered a valid number.
    3. Call each of the 4 methods by passing in the 5 numbers.
    4. 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

Edhesive Term 1

Lesson 31: Methods

Lesson 32: Method parameters

Lesson 33: Parameter preservation: objects vs primitives

Lesson 34: Method returns: void vs. non-void methods

Lesson 27: Searching algorithms

T2 Lesson 14: Linear Search