In Java: Write a program that reads at most 100 integers between 1 and 100 and c
ID: 3733969 • Letter: I
Question
In Java:
Write a program that reads at most 100 integers between 1 and 100 and counts the occurrence of each number. Assume that the input ends with 0. The following is the sample output from the program
You will write N which defines NumberCount. Your NumberCount class should implement the
Your class will have 2 instance variables; numbers (Array of ints) and reader(Scanner).
A constructor to initialize numbers and reader.
enterNumbers() method. This method is used to read numbers from the keyboard.
displayAll() method. This method displays the occurrence of the numbers that were entered by the user.
Driver Class Provided to test your number class count:
/****/public class Driver {
public static void main(String args[]) {
Driver.startApplication();
}
public static void startApplication() {
NumberCount app = new NumberCount();
app.enterNumbers();
app.displayAll();
}
}
Enter the integers between 1 and 100: 2 56543 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 timeExplanation / Answer
NumberCount.java
import java.util.Scanner;
public class NumberCount {
//Declaring instance variables
private int numbers[];
private Scanner sc;
//Static variable
private static int count;
//Zero argumented constructor
public NumberCount() {
this.numbers = new int[100];
this.sc = new Scanner(System.in);
this.count = 0;
}
/* This method will Get the inputs entered by the user
* and populate those values into array
*/
public void enterNumbers() {
int num;
System.out.print("Enter the integers between 1 and 100 :");
num = sc.nextInt();
while (num != 0) {
numbers[count] = num;
count++;
num = sc.nextInt();
}
}
//This method will display the number of times each number appeared in the array
public void displayAll() {
int counter;
for (int i = 0; i < count; i++) {
counter = 1;
for (int j = i + 1; j <= count - 1; j++) {
if (numbers[i] == numbers[j] && numbers[i] != '') {
counter++;
numbers[j] = '';
}
}
if (numbers[i] != '') {
System.out.println(numbers[i] + " occurs " + counter + " time(s)");
}
}
}
}
__________________
Driver.java
public class Driver {
public static void main(String args[]) {
Driver.startApplication();
}
public static void startApplication() {
NumberCount app = new NumberCount();
app.enterNumbers();
app.displayAll();
}
}
__________________
Output:
Enter the integers between 1 and 100 :2 5 6 5 4 3 23 43 2 0
2 occurs 2 time(s)
5 occurs 2 time(s)
6 occurs 1 time(s)
4 occurs 1 time(s)
3 occurs 1 time(s)
23 occurs 1 time(s)
43 occurs 1 time(s)
_____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.