In: Computer Science
Hello this is an intro to CS course. The question is:
Using Java, write a program that asks the user to enter three numbers. Then passes these numbers to the following methods and prints out the numbers returned by these methods.
Example output is given below:
Enter three numbers
10
4
16
The largest is 16
The smallest is 4
The median is 10
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a, b, c; int largest, smallest, median; //prompt user for 3 numbers System.out.println("Enter three numbers"); a = scanner.nextInt(); // store the 1st in a b = scanner.nextInt(); // store the 2nd in b c = scanner.nextInt(); // store the 3rd in c // call appropriate methods largest = largest(a, b, c); smallest = smallest(a, b, c); median = median(a, b, c); // display them System.out.println("The largest is " + largest); System.out.println("The smallest is " + smallest); System.out.println("The median is " + median); } /** * * @param a * @param b * @param c * @return the middle number of the 3 */ private static int median(int a, int b, int c) { return (a + b + c) - largest(a, b, c) - smallest(a, b, c); } /** * * @param a * @param b * @param c * @return the smallest of the 3 numbers */ private static int smallest(int a, int b, int c) { if (a < b && a < c) return a; if (b < a && b < a) return b; return c; } /** * * @param a * @param b * @param c * @return the largest of the 3 numbers */ private static int largest(int a, int b, int c) { if (a > b && a > c) return a; if (b > a && b > a) return b; return c; } }
===================================================================