RHS =>  Allan =>  Basic Programming => Exercises => Average Updated:  02/02/2004 23:36
WebMaster: allanhn@rhs.dk

Average

You are to create a program which can perform some simple calculations on three numbers and print out the result on the screen.
First the numbers are fixed; later they must be read in from the screen.

Question 1 Worker class
Type in the following program:

  /* Fill in your comments
   */
  public class Average {

    public static int findAverage(int num1, int num2, int num3) {
      return (num1 + num2 + num3)/3;
    }//findAverage(int, int, int)
  }//Average

Save and compile the program..

 

Question 2  Application class
Type in the following program:

  public class AverageApp {

    public static void main(String[] args) {
      int average;
      Average avObj = new Average();
      average = avObj.findAverage(10, 20, 30);
      System.out.println(average);
    }//main
  }//AverageApp

Compile and run!

Question 3  Finding the sum
Extend the class with a method findSum(), calculating the sum of three numbers.
Test the method in AverageApp.

The present program is unfavourable as it only handles the same three numbers..

 

Question 4  Read data
The program is to be extended so the three numbers can be typed in and read from the screen.
Change AverageApp so it reads the three numbers before activating the methods using these numbers.