95.0k views
0 votes
Define a method with the following header that takes three double inputs and displays those numbers in increasing order. Then test this new method inside the main method by prompting the user to enter three numbers.

public static void displaySortedNumbers(double num1, double num2, double num3)

1 Answer

4 votes

Final answer:

The question involves creating a Java method to sort and display three double inputs in increasing order. The method 'displaySortedNumbers' takes three double arguments, compares them with conditional statements, arranges them, and outputs in ascending order. To test the method, use Scanner in the main method to accept user inputs and call 'displaySortedNumbers'.

Step-by-step explanation:

The question requires defining a method in Java that takes three double inputs and displays the numbers in increasing order. To implement this functionality, you can use conditional statements to compare the numbers and then arrange them. Here's a simple implementation of the method:

public static void displaySortedNumbers(double num1, double num2, double num3) {
double temp;
if(num1 > num2) {
temp = num1;
num1 = num2;
num2 = temp;
}
if(num2 > num3) {
temp = num2;
num2 = num3;
num3 = temp;
}
if(num1 > num2) {
temp = num1;
num1 = num2;
num2 = temp;
}
System.out.println(num1 + " " + num2 + " " + num3);
}

To test this method in the main method, you can use a Scanner object to prompt the user to input three numbers and then call the displaySortedNumbers method with these inputs:

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three numbers: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
displaySortedNumbers(number1, number2, number3);
}

User Shawn Jacobson
by
7.1k points