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

If - Else

You are to create a program, which can perform some comparisons and find the maximum and minimum among two numbers and print out the result on the screen.
Later we expand the program to handle 3 numbers !
Tip: Use the Average and the AverageApp class as foundations to the construction of this program

Question 1  Worker and application classes
Type in the IfElse class, which must resemble the following template:

  public class IfElse {

    //Method to find maximum
    public double findMax(int num1, int num2) {
      if (num1 > num2){
       return num1;
      }
      else {
        return num2;
      }
    }//Max

   // IfElse

Save and compile!

Create the following application class:

  public class IfElseApp {

    public static void main(String[] args) {
      int max, n1, n2;
      //Declaration of an instance of the IfElse class, do it yourself

      //Read two numbers and store them in n1 & n2, do it yourself

      //The biggest number is found by calling the object's method findMax, do it yourself

      System.out.println("Maximum = " + average);
    }//main()
  }//IfElseApp


Compile and run.


Question 2  Finding minimum of two numbers

Extend the class with a new method findMin(int num1, int num2), which returns the minimum of the two numbers.
Test this method in IfElseApp.


Question 3  Maximum of three numbers

Extend the class with a new method findMax(int num1, int num2, int num3), which returns the maximum of the three numbers.
Test this method in IfElseApp.

Question 4. Finding minimum of three numbers
Extend the class with a new method findMin(int num1, int num2, int num3), which returns the minimum of the three numbers.
Test this method in IfElseApp.