Write a Java method that returns the largest value passed to

Write a Java method that returns the largest value passed to it.

    Use the following prototype:

    public static int max(int val1, int val2, int val3) {

      // return the largest of the three values...

    }

    For example, the following call of your method sets \'n\' to 30:

    int n = max(20, 30, 10);

    Note that any three values can be passed to this method.

    The following would also set \'n\' to 30:

    int n = max(30, 30, 10);

    Write a main() method to test your max method. It should input

    three values, call your max method, and then print the three

    values and returned max value as output, eg:

    Input values: 30 20 10 - max value is 30

   

    The max() method cannot read any input values, nor print any

    output values. All input and printing is done by the main()

    method that calls max().

    Paste in both your source code for your max() method, and

    the runtime output showing the parameter values and returned

    maximum value for the following sample input values:

    a) 30, 30, 30

    b) 10, -10, 20

    c) 40, 60, 50

    d) 90, 70, 80

    e) 50, 50, 25

    f) 50, 50, 75

    g) 200, 100, 200

    h) 200, 400, 200

    i) 300, 500, 500

    j) 700, 500, 500

Solution

MaxCheck.java


public class MaxCheck {

  
   public static void main(String[] args) {
       int n = max(20, 30, 10);
       System.out.println(\"max value is \"+n);
       n = max(30, 30, 30);
       System.out.println(\"max value is \"+n);
       n = max(10, -10, 20);
       System.out.println(\"max value is \"+n);
       n = max(40, 60, 50);
       System.out.println(\"max value is \"+n);
       n = max(90, 70, 80);
       System.out.println(\"max value is \"+n);
       n = max(50, 50, 25);
       System.out.println(\"max value is \"+n);
       n = max(200, 100, 200);
       System.out.println(\"max value is \"+n);
       n = max(200, 400, 200);
       System.out.println(\"max value is \"+n);
       n = max(300, 500, 500);
       System.out.println(\"max value is \"+n);
       n = max(700, 500, 500);
       System.out.println(\"max value is \"+n);
   }
   public static int max(int val1, int val2, int val3) {
   if(val1 > val2 && val1 > val3){
       return val1;
   }
   else{
       if(val2 > val3){
           return val2;
       }
       else{
           return val3;
       }
   }
   }

}

Output:

max value is 30
max value is 30
max value is 20
max value is 60
max value is 90
max value is 50
max value is 200
max value is 400
max value is 500
max value is 700

Write a Java method that returns the largest value passed to it. Use the following prototype: public static int max(int val1, int val2, int val3) { // return th
Write a Java method that returns the largest value passed to it. Use the following prototype: public static int max(int val1, int val2, int val3) { // return th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site