4. Write a program named ArrayTester that must do the following: Prompt for and
ID: 3704188 • Letter: 4
Question
4. Write a program named ArrayTester that must do the following:
Prompt for and read in ten integers. Store them in an array named numbers (use a for loop to do this).
Use one enhanced for loop to find the largest number and the smallest number in the array. Do this AFTER the for loop.
Display the 10 numbers in the array, along with the array index of each number, by walking through the array again (use another for loop to do this). Be sure to use column headers. Columns must be right-aligned.
Display the largest and smallest values. All output must be clearly labeled.
Use the command prompt window (terminal) for all input and output.
Note: you MUST test your program with zero, positive and negative values to make sure that it is working correctly.
Your full name must appear in the output or 1 point will be deducted. You must follow the Programming Guidelines handout. All code must be correctly indented and commented. Your program must compile in order to be graded. Do NOT use Eclipse, NetBeans, or IntelliJ for any labs or assignments. Do NOT create packages or projects; otherwise I will not be able to grade your assignment.
Sample program run:
Explanation / Answer
ArrayTester.java
import java.util.Scanner;
public class ArrayTester {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int numbers[] = new int[10];
System.out.println("Enter "+numbers.length+" integers: ");
for(int i=0;i<numbers.length;i++) {
System.out.println("Number "+(i+1)+": ");
numbers[i]=scan.nextInt();
}
int min = numbers[0];
int max = numbers[0];
for(int n: numbers) {
if(max<n)
max = n;
if(min>n)
min = n;
}
System.out.println("Index Number");
System.out.println("---------------------");
for(int i=0;i<numbers.length;i++) {
System.out.println(i+" "+numbers[i]);
}
System.out.println("Smallest number: "+min);
System.out.println("Largest number: "+max);
System.out.println("Programmed by Suresh");
}
}
Output:
Enter 10 integers:
Number 1:
345
Number 2:
112
Number 3:
4
Number 4:
5
Number 5:
7
Number 6:
-1
Number 7:
3
Number 8:
88
Number 9:
99
Number 10:
77
Index Number
---------------------
0 345
1 112
2 4
3 5
4 7
5 -1
6 3
7 88
8 99
9 77
Smallest number: -1
Largest number: 345
Programmed by Suresh
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.