Java program ZOOM + COMP B11 Create class CountNumbers CountNumbers should: Coun
ID: 3589534 • Letter: J
Question
Java program ZOOM + COMP B11 Create class CountNumbers CountNumbers should: CountNumbers In Class Program Assignment .Prompt the user to enter a sequence of numbers, ending with 'Q to quit. (Use the Scanner method hasNextDouble0 to control your loop.) . Read the numbers, keeping a separate sum of negative and positive numbers Report on separate lines, in the format shown in the example below, the two sums. Show the sums with 4 digits precision after the decimal point. Be sure your indentation and documentation are correct Example (input in green) Next PreviousExplanation / Answer
CountNumbers.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class CountNumbers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter numbers (Q to quit): ");
double posTotal = 0, negTotal = 0;
while(scan.hasNextDouble()) {
double n = scan.nextDouble();
if(n > 0) {
posTotal+=n;
} else {
negTotal += n;
}
}
DecimalFormat df = new DecimalFormat("0.0000");
System.out.println("Total of positive numbers is "+df.format(posTotal));
System.out.println("Total of negative numbers is "+df.format(negTotal));
}
}
Output:
Enter numbers (Q to quit):
4.2 3.3 -5.6 -110 -1.1 6.5 q
Total of positive numbers is 14.0000
Total of negative numbers is -116.7000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.