Question

In: Computer Science

Write a public class call CountInts. class CountInts has a main method. CountInts must have two...

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();

}

}

Solutions

Expert Solution

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


Related Solutions

Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for a number (it must work if user enters numbers with decimal point of not). Then your program must print number is negative -- if humber is negative number in [0,10) -- if it is between 0 and 10, not including the 10 number in [10,100) -- if it is between 10 and 100, not including the 100 number in [100,1000) -- if it is...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
Given the main method of a driver class, write a Fraction class. Include the following instance...
Given the main method of a driver class, write a Fraction class. Include the following instance methods: add, multiply, print, printAsDouble, and a separate accessor method for each instance variable. Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print ─ This method prints the...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
public class AddValueToArray { // You must define the addValueTo method, which will add // a...
public class AddValueToArray { // You must define the addValueTo method, which will add // a given value to each element of the given array. // // TODO - define your code below this comment // // DO NOT MODIFY main! public static void main(String[] args) { int[] array = new int[]{3, 8, 6, 4}; int valueToAdd = Integer.parseInt(args[0]); addValueTo(valueToAdd, array); for (int index = 0; index < array.length; index++) { System.out.println(array[index]); } } }
Part I: Have a program class named TestArrays This class should have a main() method that...
Part I: Have a program class named TestArrays This class should have a main() method that behaves as follows: Have an integer array of size 10. Using a loop, fill the elements with increments of 5, starting with 5 in the first element. Using the array elements, sum the second, fifth and seventh elements. Display the sum with a label. Change the fourth element to 86. Subtract 9 from the ninth element. Using a loop, display the elements of the...
Question - Write a Client class with a main method that tests the data structures as...
Question - Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue, LinkedQueue, and ArrayList: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add and remove N Integers from the structure. N should vary from 10 to 1,000,000,000 increasing N by a factor of 10 for each test. Depending on your system you may run out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT