Write a program, named IncomeApp.java, which accepts non-negative a double type
ID: 3820476 • Letter: W
Question
Write a program, named IncomeApp.java, which accepts non-negative a double type of number as an income until a negative number is entered as the negative number ends the program, then it prints out the minimum, average, and maximum for the set of entered incomes (excluding the last negative number because it only indicates the end of user input). For all kind of incomes (including the average), you always display them with at most two decimal numbers. Assume the largest number can be entered by user is 1,000,000.00 for your program.
Explanation / Answer
import java.util.*;
public class IncomeApp{
public static void main(String[] args) {
double income;
int count = 0; // Number of input values.
double totalIncome = 0; // Total of all incomes.
double maxIncome = Double.MIN_VALUE; // Maximum income.
double minIncome = Double.MAX_VALUE; // Minimum income.
Scanner input = new Scanner(System.in);
//... Loop until Non negative number is encoutered.
System.out.println("Enter the incomes. Terminate With Negative Income number.");
//... Get the next value.
income = input.nextDouble();
while (income > 0 ) {
//... Compare this value to max and min. Replace if needed.
if (income > maxIncome) {
maxIncome = income;
}
if (income < minIncome) {
minIncome = income;
}
//... Keep track of these values for average calculation.
count++; // Count the number of data points.
totalIncome += income; // Keep a running total.
//... Get the next value.
income = input.nextDouble();
}
//... Be sure user entered at least one data point.
if (count > 0) { //Note 2
//... Display statistics
double average = totalIncome / count;
System.out.println("Number of values = " + count);
System.out.println("Maximum = " + maxIncome);
System.out.println("Minimum = " + minIncome);
System.out.println("Average = " + average);
} else {
System.out.println("Error: You entered no data!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.