Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that reads an arbitrary number of integers that are in the

ID: 670544 • Letter: W

Question

Write a Java program that reads an arbitrary number of integers that are in the range of 1 to 50 inclusive and counts the number of occurrences of each entered. When user quits from the program, display each entered integer with the count of the occurrences of that integer. Also display the average of the occurrences at the end.

Requirements:

1) The program should include a loop which allows arbitrary input and ends whenever the user wants to. In the loop body, prompt the user for the input and validate the input value (must be numeric and 1-50 only), and record the occurrences of each input data. For the invalid input, the program should display an error message and continue asking for the next input.

2) Calculate the average of data

3) Only one int array should be used. (Hint: The input integers themselves do not need to be saved. Save the occurrences only.)

4) When displayed, only show the input data and their occurrences. The values that are not entered should not be displayed. For example, if ‘1’ is entered two times and ‘25’ one time, when the user quits, the display should be something like below:

Data entered         Occurrence

----------------             ---------------

   1                          2

25                               1

The average of occurrences is 1.5.

5) Use the comment lines to write the function description (your algorithm to solve the problem) of the program.

6) The submitted program should be executable (no syntax errors).

ALL REQUIREMENTS MUST BE MET, INCLUDING COMMENTS, PRINTING OF AVERAGE, AND DATA TYPES.

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
       int[] occurences = new int[50];
       int count=0,sum=0;
       double avg;
       Scanner in = new Scanner(System.in);
       int num,response;
       while(true)
       {
           System.out.println("Input the integer in the range of 1 to 50 : ");
           num = in.nextInt();
          
           if(num<1 || num>50)
               System.out.println("Integer should be in the range of 1 to 50 not considering this occurence. ");
           else
           {
               occurences[num-1] = occurences[num-1]+1;
           }
          
           System.out.println("Do you want to continue? Press(1 for yes/else any other number) ");
           response = in.nextInt();
          
           if(response != 1)
               break;
       }
      
       for(int i=0;i<50;i++)
       {
           if(occurences[i]!=0)
           {
               sum = sum+occurences[i];
               count++;
           }
       }
      
       avg = (sum*1.0)/count;
      
       System.out.println("Data entered Occurence");
       System.out.println("------------- --------");
      
       for(int i=0;i<50;i++)
       {
           if(occurences[i]!=0)
               System.out.println((i+1) + " " + occurences[i] );
       }
       System.out.println("The average of occurences is : "+avg);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote