Question: create and test a program called GradeList.java that puts integers into an ArrayList : Write a program that allows the user to enter an arbitrary
create and test a program called GradeList.java that puts integers into an ArrayList:
Write a program that allows the user to enter an arbitrary number of integer grades from the keyboard. Each grade should be added to an ArrayList. Stop inputting grades when 1 is entered.
The program should then iterate through the ArrayList, calculate the average grade, and output it along with the minimum and the maximum grade value. Be sure to test your program to check it.
Hints:
Start by copying ArrayListDemo.java from the Source Code folder to GradeList.java and change the class name. The logic structure is correct, but the ArrayList needs to be an ArrayList
Use keyboard.nextInt() to read the next integer from the user; test it to see if its -1 to end the loop.
You can either keep track of the minimum and maximum values as youre reading the integers, or else go through the ArrayList at the end to determine those values. Also determine the sum of all of the integers except the -1.
If the user enters -1 as their first integer the ArrayList will have size() == 0; in that case tell the user that there is no minimum or maximum, and that you cannot compute the average of no entries, then end the program.
Otherwise, print out the minimum and maximum and compute and print the average based on the total and size.
With grades 95, 99, 75, 83, 88, min==75, max==99, avg==88.
here is the ArrayListDemo.java code:
import java.util.ArrayList; import java.util.Scanner; public class ArrayListDemo { public static void main(String[] args) { ArrayList toDoList = new ArrayList(); System.out.println("Enter items for the list, when prompted."); boolean done = false; Scanner keyboard = new Scanner(System.in); while (!done) { System.out.println("Type an entry:"); String entry = keyboard.nextLine( ); toDoList.add(entry); System.out.print("More items for the list? "); String ans = keyboard.nextLine( ); if (!ans.equalsIgnoreCase("yes")) done = true; } System.out.println("The list contains:"); int listSize = toDoList.size( ); for (int position = 0; position < listSize; position++) System.out.println(toDoList.get(position)); /* Alternate code for displaying the list System.out.println("The list contains:"); for (String element : toDoList) System.out.println(element); */ } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
