Question

In: Computer Science

Hello this is an intro to CS course. The question is: Using Java, write a program...

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.

  1. largest() – returns the largest of the three numbers
  2. smallest() – returns the smallest of the three numbers
  3. median() – returns the median of the three numbers

Example output is given below:

Enter three numbers

10

4

16

The largest is 16

The smallest is 4

The median is 10

Solutions

Expert Solution

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;

    }
}

===================================================================


Related Solutions

Time Calculator – Intro To Programming - JAVA Write a program that asks the user to...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to enter a number of seconds. • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to...
Hello, Please write this program in java and include a lot of comments and please try...
Hello, Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was: Create your own class Your own class can be anything you want Must have 3 instance variables 1 constructor variable → set the instance variables 1 method that does something useful in relation to the class Create a driver class that creates an...
Write this program using an IDE. Comment and style the code according to the CS 200...
Write this program using an IDE. Comment and style the code according to the CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct. Monster collector is a game of chance, where the user tries to collect monsters by guessing the correct numbers between 1 and 5. If the user doesn't guess the incorrect number, you catch...
Question 2: Write a java program that calculates the weighted average of three grades using the...
Question 2: Write a java program that calculates the weighted average of three grades using the method with header public static double CalculateWeightedAverage (double grade1, double grade2, double grade3), such that: [4 Marks] Weighted average = grade1 * 0.5 + grade2 * 0.25 + grade3 * 0.25 The program in the main method will: o ask the user to enter the three grades o and then call the method that will calculate the weighted average o and finally display the...
CS 206 Visual Programming, Netbeans Problem: Write a Java program that displays all the leap years,...
CS 206 Visual Programming, Netbeans Problem: Write a Java program that displays all the leap years, 10 per line, from 1001 to 2100, separated by exactly one space. Also display the total number of leap years in this period. Hints: you need to use a loop ( for-loop is more suitable)
(Intro/Basic) JAVA Write a small program that gets some numbers as command-line arguments and finds the...
(Intro/Basic) JAVA Write a small program that gets some numbers as command-line arguments and finds the maximum of those numbers. You can assume that the user will provide some command-line arguments when running the program (so you don’t have to worry about what to do if the user doesn’t provide any arguments -- that won’t happen). Also, you can assume that all the command line arguments will be floating-point numbers, i.e., numbers with a decimal point, like 2.95.
This is an intro to java question. Please post with pseudocode and java code. Problem should...
This is an intro to java question. Please post with pseudocode and java code. Problem should be completed using repetition statements like while and selection statements. Geometry (10 points) Make API (API design) Java is an extensible language, which means you can expand the programming language with new functionality by adding new classes. You are tasked to implement a Geometry class for Java that includes the following API (Application Programming Interface): Geometry Method API: Modifier and Type Method and Description...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT