Question: Can someone please check my code per the requirements below? I am able to print the integers, but not the count (or occurrences) for each.
Can someone please check my code per the requirements below? I am able to print the integers, but not the count (or occurrences) for each. Thanks.
Program 1: Design (pseudocode) and implement (source code) a program (name it Occurrences) that counts the occurrences of integers in an array. The program main method defines a single-dimensional array of size 10 elements and prompts the user to enter 10 integers to initialize the array. The main method then calls method Count()to printout the count of each value in the array. Method Count()takes an integer array, counts the occurrences of each value in the array, and prints out the results as shown below. Document your code and properly label the input prompts and the outputs as shown below.
import java.util.*;
public class Occurrences {
public static void main(String[] args) { Scanner input = new Scanner(System.in); int [] numbers = new int [10]; System.out.println("Enter 10 integers: "); for (int i = 0; i < numbers.length; i++) { numbers[i] = input.nextInt(); } Count(numbers); } // END METHOD Main public static void Count(int [] numbers) { int [] integers = new int [10]; int [] occurrences = new int [10]; int count = 0; for (int i = 0; i < integers.length; i++) { numbers[i] = integers[i]; count = 1; boolean unique = true; for (int k : numbers) { if (k == numbers[i]) { unique = false; break; } // END IF } // END FOR EACH if (unique == true) { for (int j = i + 1; j < numbers.length; j++) { if (integers[i] == numbers[j]) { count++; } // END IF occurrences[i] = count; } // END FOR } // END IF } // END OUTER FOR Loop for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i] + " occurred " + occurrences[i] + " time(s)"); } // END FOR } // END METHOD Count } // END class Occurrences
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
