Question

java 8.1: Frequency Design and implement an application that reads an arbitrary number of integers that...

java

8.1: Frequency
Design and implement an application that reads an arbitrary number of integers that are in the range 0 to 50 inclusive and counts how many occurrences of each are entered. After all input has been processed, print all of the values (with the number of occurrences) that were entered one or more times. The output should be one frequency count per line with the following format:
3 occurrences of 2
7 occurrences of 5

SPECIFICATION OF NAMES: Please name your application class Frequency

Homework Answers

Answer #1

Here is the Java code. If you have any queries then please ask me in the comment section. Upvote if you like the answer.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Frequency 
{
        int returnCount(int n, int []list,int len)
        {
                int count=0,i=0;
                while(i<len)
                {
                        if(n==list[i])
                                count++;
                        i++;
                }
                return count;
        }
        
        public static void main(String []args) throws IOException
        {
                BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
                Frequency obj = new Frequency();
                int i,j,count;
                int num[] = new int[1000];
        String[] strNums;
        
        System.out.println("Enter integers between 0 to 50 separated by spaces : ");
        strNums = bi.readLine().split("\\s");
        for(i=0; i<strNums.length; i++) 
        {
            num[i] = Integer.parseInt(strNums[i]);
        }
        
        for(i=0; i<strNums.length; i++)
        {
                if(num[i]<0 || num[i]>50)
                {
                        System.out.println("\nEnter the numbers between 0 and 50(both inclusive) only");
                        return;
                }
        }
        for(i=0;i<strNums.length;i++)
        {
                for(j=0;j<i;j++)
                        if(num[i]==num[j])
                                break;
                if(i==j)
                {
                        count = obj.returnCount(num[i],num,strNums.length);
                if(count>0)
                {
                        System.out.printf("%d occurs %d times\n",num[i],count);
                }
                }
        }
        }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line,...
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line, for equality. Print any lines that are not equivalent indicating the line numbers in both files. The language of implementation is java 2 . Create a program that reads a string input from the user, then determines and prints how many of each lowercase vowels (a, e. i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also...
JAVA: Design and implement a recursive version of a binary search.  Instead of using a loop to...
JAVA: Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are passed to...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from a file. Specifically, you will implement the following function: /* * Reads one arithmetic "expression" at a time from a file stream, computes, then * returns the result. If there are additional expressions in the file, they are * read and computed by successive calls to “calculator”. * * “Expressions” are groups of operations (add, subtract, multiply, divide). Your * calculator will read and...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Using C# and Windows Forms (WinForms), design and implement a standalone desktop application that fulfils the...
Using C# and Windows Forms (WinForms), design and implement a standalone desktop application that fulfils the following requirements: 1. The user shall be able to enter the following values: a. Gross monthly income (before deductions). b. Estimated monthly tax deducted. c. Estimated monthly expenditures in each of the following categories: i. Groceries ii. Water and lights iii. Travel costs (including petrol) iv. Cell phoneand& telephone v. Other expenses 2. The user shall be able to choose between renting accommodation or...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads data from a file and performs regression analysis using polyfit and polyval. The function shall have the following features: The input arguments shall include the file name (string), a vector of integers for the degrees of polynomial fits to be determined, and an optional plot type specifier (‘m’ for multiple plots, ‘s’ for a single plot - default). The data files will be text...
Implement a singly linked list having all unique elements with the following operations.I 0 x –...
Implement a singly linked list having all unique elements with the following operations.I 0 x – Inserts element x at the end. I 1 y x – If the element y exists, then insert element x after the element y, else insert element y before the existing element x. Assuming either the element x or the element y exists. I 2 z y x – Inserts element x in the middle of the elements z and y. The element z...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT