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...
Please write in Java and have two methods: the main method and the reverse word Write...
Please write in Java and have two methods: the main method and the reverse word Write a method that reads a line and reverses the words in the line (not the characters) using a stack. For example, given the following input: The quick brown fox jumps over the lazy dog you should get the following output: dog lazy the over jumps fox brown quick The Then create a main method to prompt the user to enter a line of words...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
JAVA Implement a public class method named comparison on a public class Compare that accepts two...
JAVA Implement a public class method named comparison on a public class Compare that accepts two Object arguments. It should return 0 if both references are equal. 1 if both objects are equal. and -1 otherwise. (SUPER IMPORTANT) Either reference can be null, so you'll need to handle those cases carefully! Here is what I have so far: public class Compare { public static int comparison(Object a, Object b) {   if (a == null || b == null) {    return...
Create a class named TestLease whose main() method declares four Lease objects. Call a getData() method...
Create a class named TestLease whose main() method declares four Lease objects. Call a getData() method three times. Within the method, prompt a user for values for each field for a Lease, and return a Lease object to the main() method where it is assigned to one of main()’s Lease objects. Do not prompt the user for values for the fourth Lease object, but let it continue to hold the default values. Then, in main(), pass one of the Lease...
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...
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]); } } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT