Please help me code the following in JAVA! Full points will be rewarded, thanks
ID: 3877897 • Letter: P
Question
Please help me code the following in JAVA!
Full points will be rewarded, thanks in advance!
b. Write a program in main that asks the user of your program to first enter his or her name followed by 3 real numbers (that is, any number from-oo to Your program should sort the three numbers in descending order, then print to the screen a greeting to the person and the sorted numbers. For example (your input in blue), Please enter you name followed by three numbers (space separated) Leonardo-22.3 12.2 2.3 Hi there, Leonardo! Here are the numbers you entered in descending rder: 12.2 2.3 and-22.3 hank you for using the ehree-number-sorting ayetem! Good-bye. Note: You must use the Math.max and Math.min methods introduced above. The class has not covered conditionals, so please avoid using conditionals! Make sure to run your program several times and test it with several inputs of different orderings.Explanation / Answer
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Please enter your name followed by three numbers (space separated):");
Scanner sc = new Scanner(System.in);
// declaring variables for input
String name;
double a, b, c;
// declaring variables for output
double one, two, three;
// declaring variables that help
double minab, minac, maxab, maxac;
// taking user input
name = sc.next();
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
// finding maximum number
maxab = Math.max(a,b);
maxac = Math.max(a,c);
>
// finding minimum number
minab = Math.min(a,b);
minac = Math.min(a,c);
three = Math.min(minab,minac);
// finding the middle one
two = (a+b+c) - (one+three);
// printing output
System.out.println(" Hi there, "+name+"! Here are the numbers you entered in descending order:");
System.out.println(one+", "+two+" and "+ three);
}
}
/*
SAMPLE OUTPUT
Please enter your name followed by three numbers (space separated):
Leonard -22.1 12.3 2
Hi there, Leonard! Here are the numbers you entered in descending order:
12.3, 2.0 and -22.1
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.