211k views
3 votes
Lab9A: Warmup. Write a program that contains three methods: Method max (int x, int y, int z) returns the maximum value of three integer values. Method min (int X, int y, int z) returns the minimum value of three integer values. Method average (int x, int y, int z) returns the average of three integer values. Note: for Java and C#, these methods must be public and static. Prompt the user for three numbers, then call these methods from main to print out the minimum, maximum and average of numbers that the user entered. Your program should behave like the sample output below. Sample output #1 Enter number 1: 5 Enter number 2: 9 Enter number 3: 2 Min is 2 Max is 9 Average is 5.33333 Sample output 2 Enter number : 45 Enter number 2 : 11 Enter number 3: -3 Min is-3 Max is 45 Average is 17.6667

1 Answer

0 votes

import java.util.Scanner;

import java.util.Arrays;

public class JavaApplication30 {

public static int max(int x, int y, int z){

int[] arr = {x, y, z};

Arrays.sort(arr);

return arr[2];

}

public static int min(int x, int y, int z){

int[] arr = {x, y, z};

Arrays.sort(arr);

return arr[0];

}

public static double average(int x, int y, int z){

return (double) (x + y + z) / 3;

}

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter your first number");

int x = scan.nextInt();

System.out.println("Enter your second number");

int y = scan.nextInt();

System.out.println("Enter your third number");

int z = scan.nextInt();

System.out.println("Min is "+min(x,y,z));

System.out.println("Max is "+max(x,y,z));

System.out.println("Average is "+average(x,y,z));

}

}

I hope this helps! If you have any more questions, I'll try my best to answer them.

User Brian Frantz
by
4.3k points