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...
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...
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...
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...
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...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
JAVA Bike shares are becoming increasingly common in cities in the United States. Commuters have been...
JAVA Bike shares are becoming increasingly common in cities in the United States. Commuters have been using bike sharing as a method of transportation for the flexibility, cost savings, exercise, and a myriad of other benefits. While being quite successful, many bike shares are still relatively new and have room for optimization. To help with the optimization process, we want to analyze bike share data from the city of Los Angeles. The first step of the process is to collect...
***Programming language is Java. After looking at this scenario please look over the requirements at the...
***Programming language is Java. After looking at this scenario please look over the requirements at the bottom (in bold) THIS IS ALL THAT WAS PROVIDED. PLEASE SPECIFY ANY QUESTIONS IF THIS IS NOT CLEAR (don't just say more info, be specific)*** GMU in partnership with a local sports camp is offering a swimming camp for ages 10-18. GMU plans to make it a regular event, possibly once a quarter. You have been tasked to create an object-oriented solution to register,...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
INTELLECTUAL PROPERTY  Industrial property forms part of the broader concept of "intellectual property."  The...
INTELLECTUAL PROPERTY  Industrial property forms part of the broader concept of "intellectual property."  The objects of intellectual property are the creations of the human mind, the human intellect hence the expression "intellectual" property.  In a somewhat simplified way, one can state that intellectual property relates to pieces of information which can be incorporated in tangible objects at the same time in an unlimited number of copies at different locations anywhere in the world.  The property is...