Question

In: Computer Science

[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...

[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

Solutions

Expert Solution

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);

}

}


Related Solutions

JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in LiveExample 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is...
JAVA Write a Java console application that prompts the user to enter the radius of a...
JAVA Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Programmer Notes Write and document your program per class coding conventions. Add an instance variable double radius. Generate its get/set methods. Manually type the methods getDiameter(), getCircumference(), and getArea()....
JAVA Write a program that will compare two names. The program prompts the user to enter...
JAVA Write a program that will compare two names. The program prompts the user to enter two names for a comparison. If the names are same, the program states that. If the names are different, the program converts both names to UPPERCASE, and compares then again. If they are equal, the programs displays a message stating that names are equal if CASE is ignored. Otherwise, the program prints names with a message that names are not equal.  Check also if there...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
JAVA Write a program that prompts the user to enter a matrix number of rows and...
JAVA Write a program that prompts the user to enter a matrix number of rows and number of columns. In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it. Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use global variables. Here...
Write a Java program to do the following: Specifics: Write an application that prompts a user...
Write a Java program to do the following: Specifics: Write an application that prompts a user for two integers and displays every integer between them. Display a message if there are no integers between the entered values. Make sure the program works regardless of which entered value is larger. Save the file as InBetween.java. Don’t forget to create the application/project  InBetweenTest.java Class that has the main method and an object to use the InBetween class.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT