Program 1 (30 points): Design and implement a Java program for programming exerc
ID: 3606611 • Letter: P
Question
Program 1 (30 points): Design and implement a Java program for programming exercise 7.3, page 277 (name it CountOccurrences), to read an unlimited series of integers, between 1 and 100, and count the occurrences of each integer. Follow the instructions in the problem statement and use the given sample run as a model for the output. Design the main method of your program to handle all input and output, to validate that the numbers fall within the specified range (and provide a meaningful message to the user when one or more entries do not fall within the specified range), and to allow the user to re-run the program with different sets of inputs. Document your code and organize the output using appropriate formatting techniques. No sorting is necessary for this programExplanation / Answer
import java.util.Scanner;
import java.util.Arrays;
class Main {
public static void main (String args[]){
//intial freq array to store the frequency of digits
int[] freq = new int[101];
// fill array with zeros
Arrays.fill(freq, 0);
Scanner sc = new Scanner(System.in);
//get user input
System.out.print("Enter the integers between 1 and 100:");
int n = sc.nextInt();
//loop until input not equal to zero
while(n!=0){
//increment the frequency of index i
freq[n]++;
//read next input
n = sc.nextInt();
}
//loop to print numbers
for(int i=1;i<101;i++){
if(freq[i]==1){
System.out.printf("%d occurs %d time ",i,freq[i]);
}
else if(freq[i]>1){
System.out.printf("%d occurs %d times ",i,freq[i]);
}
}
}
}
/*
sample output
Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.