In: Computer Science
Write a public class call CountInts. class CountInts has a main method. CountInts must have two methods: count and displayResults. 1. method count takes an array, aa, of ints and counts how many times each integer appears in aa. The integers can only be from 0 to 100 inclusive. 2. method display results displays the results of the count 3. use the template provided. insert your code in the places indicated and don't change anything else.
Examples
% java CountInts 0 1 2 3 4 5 2 3 4 5 4 5 5 [0, 1, 2, 3, 4, 5, 2, 3, 4, 5, 4, 5, 5] 0 occurs ONE time 1 occurs ONE time 2 occurs 2 times 3 occurs 2 times 4 occurs 3 times 5 occurs 4 times % java CountInts 0 1 2 1000 -3 2 [0, 1, 2, 1000, -3, 2] 0 occurs ONE time 1 occurs ONE time 2 occurs 2 times
Template:
import java.util.*;
public class CountInts {
public static void count(int [] aa){
/*insert your code here */
}
public static void displayResults(){
/*insert your code here */
}
public static int [] readArray(){
Scanner input = new Scanner(System.in);
String ss = input.nextLine();
String [] aa = ss.split("[, ]");
if(aa[0].length() == 0)
aa = new String[0];
//System.out.println(Arrays.toString(aa));
int [] dd = new int[aa.length];
int ii = 0;
for(String s : aa){
dd[ii++] = Integer.parseInt(s);
}
return dd;
}
public static void main (String[] args) {
int [] arr = readArray();
System.out.println(Arrays.toString(arr));
count(arr);
displayResults();
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: I have created a static array in CountInts to store the count of each values from 0 to 100. We cannot avoid this as there has to be some way of passing result from count method to displayResults. Either this, or we need to change the signatures of count and displayResults methods.
// CountInts.java
import java.util.*;
public class CountInts {
// creating a global array to store the counts of each values from 0 to 100
// count of each value is stored in its own index. for example, count of 5
// is stored in countsArr[5], count of 0 is stored in countsArr[0] etc
static int countsArr[] = new int[101];
public static void count(int[] aa) {
// re initializing counts array
countsArr = new int[101];
// looping through each element in aa
for (int i = 0; i < aa.length; i++) {
// checking if value is within 0-100
if (aa[i] >= 0 && aa[i] <= 100) {
// incrmenting counter at aa[i] index
countsArr[aa[i]]++;
}
}
}
public static void displayResults() {
// looping through countsArr
for (int i = 0; i <= 100; i++) {
// proceeding only if count is non zero
if (countsArr[i] > 0) {
// if count is 1, displaying "...ONE time, otherwise "..n times"
if (countsArr[i] == 1) {
System.out.println(i + " occurs ONE time");
} else {
System.out
.println(i + " occurs " + countsArr[i] + " times");
}
}
}
}
public static int[] readArray() {
Scanner input = new Scanner(System.in);
String ss = input.nextLine();
String[] aa = ss.split("[, ]");
if (aa[0].length() == 0)
aa = new String[0];
// System.out.println(Arrays.toString(aa));
int[] dd = new int[aa.length];
int ii = 0;
for (String s : aa) {
dd[ii++] = Integer.parseInt(s);
}
return dd;
}
public static void main(String[] args) {
int[] arr = readArray();
System.out.println(Arrays.toString(arr));
count(arr);
displayResults();
}
}
/*INPUT & OUTPUT*/
0 1 2 3 4 5 2 3 4 5 4 5 5 -1 1000 -3
[0, 1, 2, 3, 4, 5, 2, 3, 4, 5, 4, 5, 5, -1, 1000, -3]
0 occurs ONE time
1 occurs ONE time
2 occurs 2 times
3 occurs 2 times
4 occurs 3 times
5 occurs 4 times