Exercise 4: Write a program called FindBiggest to create a 1D array with n integ
ID: 3594120 • Letter: E
Question
Exercise 4: Write a program called FindBiggest to create a 1D array with n integer variables. The program will continuously ask the user to input the integer number until the user's input is "0" (zero). After the array is initialised, write a method to find the biggest number among the entered integers. The output of the program should firstly display all integers, and then indicate the biggest integer among them as well as the index of the biggest integer in the 1D array. ArrayList should be used) CS0DExplanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
public class FindBiggest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> idList = new ArrayList<>();
System.out.println("Enter IDs (0 to stop): ");
int id = sc.nextInt();
while(id != 0) {
idList.add(id);
id = sc.nextInt();
}
sc.close();
if(idList.size() == 0) {
System.out.println("list is empty");
return;
}
// displaying ids
for(int i=0; i<idList.size(); i++)
System.out.print(idList.get(i)+" ");
System.out.println();
// finding biggest id
int large = idList.get(0); // initializing bid id with first element
for(int i=1; i<idList.size(); i++)
if(large < idList.get(i))
large = idList.get(i);
System.out.println("biggest id: "+large);
}
}
/*
Sample run:
Enter IDs (0 to stop):
34
12
98
56
345
12
0
34 12 98 56 345 12
biggest id: 345
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.