Java Question Write a Java application program in which main

Java Question:

Write a Java application program in which main will read 3 ints from the user, calls a static method (you write) that returns (in a return statement) the average (as a rounded int), and then calls another static method (you write) that returns (in a return statement) the largest of the 3 ints. Also in main, print the return values of the methods (don\'t print inside the methods). Be sure to allow for 2 or 3 of the numbers having the same value. ALL METHODS SHOULD BE STATIC.

Solution

NumbersTest.java

import java.util.Scanner;


public class NumbersTest {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter first number: \");
       int first = scan.nextInt();
       System.out.println(\"Enter second number: \");
       int second = scan.nextInt();
       System.out.println(\"Enter third number: \");
       int thrid = scan.nextInt();
       double avg = average(first, second, thrid);
       System.out.println(\"Average: \"+avg);
       int large = largest(first, second, thrid);
       System.out.println(\"Largest: \"+large);
   }
   public static double average(int a, int b, int c){
       double sum = a + b + c;
       double average = sum/3;
           return average;
   }
   public static int largest(int a, int b, int c){
       if(a >= b && a >= c){
           return a;
       }
       else if(b >= c){
           return b;
       }
       else{
           return c;
       }
   }

}

Output:

Enter first number:
3
Enter second number:
3
Enter third number:
2
Average: 2.6666666666666665
Largest: 3

Enter first number:
10
Enter second number:
10
Enter third number:
10
Average: 10.0
Largest: 10

Java Question: Write a Java application program in which main will read 3 ints from the user, calls a static method (you write) that returns (in a return statem
Java Question: Write a Java application program in which main will read 3 ints from the user, calls a static method (you write) that returns (in a return statem

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site