Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that: 1. Performs these steps until the user enters a 0. a.

ID: 3824048 • Letter: W

Question

Write a Java program that:

1.   Performs these steps until the user enters a 0.

   a.   Read an integer value from the console.

   b.   Add the value to an ArrayList.

2.   After the user enters a 0, processes the ArrayList as follows:

   a.   Calculate the total of the values entered

   b.   Calculate the average of the values entered. The “0” that ends the input does not count in the number of entries. Ensure you handle the case of no entries correctly.

   c.   Output to the console, with appropriate messages, the number of values read, the total and the average.

Before you run your program to create the screen print, clear the screen print area and show all the dialog from the first entry to the last message. Your dialog should show:

a.   A program run with at least five numbers entered followed by a 0.

b.   A program run with only a 0 entered, meaning there are no numbers in the list to process.

c.   The average formatted to two decimal places.

Write the program as one main method or use a few methods, at your discretion.

Explanation / Answer

ArrayListNumbers.java

import java.util.ArrayList;
import java.util.Scanner;


public class ArrayListNumbers {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       ArrayList<Integer> list = new ArrayList<Integer>();
       System.out.println("Enter the numbers end with 0: ");
       int n = scan.nextInt();
       while(n!=0){
           list.add(n);
           n = scan.nextInt();
       }
       int total = 0;
       for(int i=0; i<list.size(); i++){
           total = total + list.get(i);
       }
       double average = total/(double)list.size();
       System.out.println("Total: "+total);
       System.out.println("Average: "+average);
   }

}

Output:

Enter the numbers end with 0:
1 2 3 4 5 6 7 8 9 10 0
Total: 55
Average: 5.5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote