In: Computer Science
(Java) Create a program using 3 methods. The methods must be public and static. Ask the user for 3 numbers, then call the methods from main to print max, min, and average.
The first method max (int x, int y, int z) returns the maximum value of 3 integer values.
The second method min (int X, int y, int z) returns the minimum value of 3 integer values.
And the third average (int x, int y, int z) returns the average of 3 integer values.
Sample Output: (bold = user input)
Enter # 1: 5
Enter # 2: 9
Enter # 3: 2
Min is 2
Max is 9
Average is 5.33333
Sample output 2
Enter # 1: 45
Enter # 2: 11
Enter # 3: -3
Min is -3
Max is 45
Average is 17.6667
Code Example:
int addTwoNumbers (int x, int y) {
int result = x + y;
return result;
Code:
import java.util.*;
class Test60 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a, b, c;
int minimum, maximum;
float avg;
// Inputting the value of the 1st integer
System.out.print("Enter # 1: ");
a = sc.nextInt();
// Inputting the value of the 2nd integer
System.out.print("Enter # 2: ");
b = sc.nextInt();
// Inputting the value of the 3rd integer
System.out.print("Enter # 3: ");
c = sc.nextInt();
minimum = Test60.min(a, b, c); // Calling the method- min()
maximum = Test60.max(a, b, c); // Calling the method- max()
avg = Test60.average(a, b, c); // Calling the method-
average()
// Displaying the maximum, minimum and the average of the 3
integers
System.out.println("Min is " + minimum);
System.out.println("Max is " + maximum);
System.out.printf("Average is %.5f", avg);
}
// Method- max() -> Returns the maximum of the 3
integers
public static int max(int x, int y, int z) {
if(x >= y && x >= z)
return x; // x is the largest
else if(y >= x && y >= z)
return y; // y is the largest
else
return z; // z is the largest
}
// Method- min() -> Returns the minimum of the 3
integers
public static int min(int x, int y, int z) {
if(x <= y && x <= z)
return x; // x is the smallest
else if(y <= x && y <= z)
return y; // y is the smallest
else
return z; // z is the smallest
}
// Method- average() -> Returns the average of the 3 integers
public static float average(int x, int y, int z) {
float avg = (x + y + z) / 3.0f;
return avg;
}
}