In: Computer Science
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be displayed with 4 digits of precision after decimal point, in bold underline is what your user types in when they run the program): Enter Value 1: 2.34567 Enter Value 2: 3.45678 Enter Value 3: 1.23456 Enter Value 4: 5.67891 Enter Value 5: 4.56789 The values entered are: 2.3457, 3.4568, 1.2346, 5.6789, 4.5679 The minimum value is 1.2346 and maximum value is 5.6789 The average of these five values are: 3.4568
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// Create a Scanner object
Scanner sc = new Scanner(System.in);
// Declare variables
float input1, input2, input3, input4, input5, average, min, max;
//Prompt for the first Floating Point Number
System.out.println("Enter Value 1: ");
input1 = sc.nextFloat();
//Prompt for the second Floating Point Number
System.out.println("Enter Value 2: ");
input2 = sc.nextFloat();
//Prompt for the third Floating Point Number
System.out.println("Enter Value 3: ");
input3 = sc.nextFloat();
//Prompt for the fourth Floating Point Number
System.out.println("Enter Value 4: ");
input4 = sc.nextFloat();
//Prompt for the fifth Floating Point Number
System.out.println("Enter Value 5: ");
input5 = sc.nextFloat();
//Prompt the user for values entered
System.out.print("The values entered are: ");
System.out.printf("%.4f ", input1);
System.out.printf("%.4f ", input2);
System.out.printf("%.4f ", input3);
System.out.printf("%.4f ", input4);
System.out.printf("%.4f\n", input5);
//calculate the max
float temp1 = input1 > input2 ? input1 : input2;
float temp2 = input3 > input4 ? input3 : input4;
float temp3 = temp1 > temp2 ? temp1 : temp2;
max = temp3 > input5 ? temp3 : input5;
//calculate the min
temp1 = input1 < input2 ? input1 : input2;
temp2 = input3 < input4 ? input3 : input4;
temp3 = temp1 < temp2 ? temp1 : temp2;
min = temp3 < input5 ? temp3 : input5;
//calculate the average
average = (input1 + input2 + input3 + input4 + input5)/5;
//Output the minimum and maximum
System.out.printf("The minimum value is %.4f\n", min);
System.out.printf("The maximum value is %.4f\n", max);
System.out.printf("The average of these five values are: %.4f\n", average);
}
}