Java Program for Loops in Eclipse.
Java assignment
Problem to Solve
This assignment is to solve problem using loops. Write a program to generate 100 random integers in the range of 0 and 99999, find the frequency count of each digit (0, 1, 2, . . . , 9) in these numbers, and print a frequency histogram.
How to Do It
The Overall program structure may look like this:
4028 4028 75 75 66 93540 94540
3. Print a horizontal bar. For each digit (say digit 5) with a frequency count (say 41), a bar is displayed with 41 asterisks like this
Problem to Solve
This assignment is to solve problem using loops. Write a program to generate 100 random integers in the range of 0 and 99999, find the frequency count of each digit (0, 1, 2, . . . , 9) in these numbers, and print a frequency histogram.
How to Do It
The Overall program structure may look like this:
- Use ten variables for the frequency counts for the ten digits 0, 1, 2, . . . , 9.
- Other variables are also needed. For example, a variable to hold a random number, a variable
for the digit, etc. - Repeat 100 times (a loop):
- Generate a random number.
- For each digit d in the number, increase the frequency count of d by 1. This is done
using a loop. Note: we do not know how many digits are in the number.
- For each digit from 0 to 9 (a loop):
• Store its frequency count into a variable.• Display the digit and the frequency count.• Display the horizontal frequency bar of asterisks using a loop.
- Generate random integers in the range [0, 99999]. Assume the variable is number:
number = (int)(Math.random() * 99999);
- Get the digits of the random number. Since we do not know how many digits in the number, you need to use a loop to “peel off” the rightmost digit one at a time using integer division (/) and modulus (%) operations. Here are some example of random numbers and their digits:
4028 4028 75 75 66 93540 94540
3. Print a horizontal bar. For each digit (say digit 5) with a frequency count (say 41), a bar is displayed with 41 asterisks like this
5 (41): *****************************************
The code used for java in Eclipse is
import java.util.Scanner;public class Enter Your Class Name Here {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);//Declare the Variablesint count = 0, count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;int digit1= 1, digit2 = 1, digit3 = 1, digit4 = 1, digit5 = 1;int num = 1;//Loopwhile (count<100){num = (int)(Math.random() * 99999); // to generate a random numbercount++;//Break Numbersdigit1 = num / 10;digit2 = (num % 10000) / 1000;digit3 = ((num % 10000) % 1000) / 100;digit4 = (((num % 10000) % 1000) % 100) / 10;digit5 = ((((num % 10000) % 1000) % 100) % 10);if(digit1 == 0 || digit2 == 0 || digit3 == 0 || digit4 == 0 || digit5 == 0){count0 = count0 +1;}if(digit1 == 1 || digit2 == 1 || digit3 == 1 || digit4 == 1 || digit5 == 1){count1 = count1 +1;}if(digit1 == 2 || digit2 == 2 || digit3 == 2 || digit4 == 2 || digit5 == 2){count2 = count2 +1;}if(digit1 == 3 || digit2 == 3 || digit3 == 3 || digit4 == 3 || digit5 == 3){count3 = count3 +1;}if(digit1 == 4 || digit2 == 4 || digit3 == 4 || digit4 == 4 || digit5 == 4){count4 = count4 +1;}if(digit1 == 5 || digit2 == 5 || digit3 == 5 || digit4 == 5 || digit5 == 5){count5 = count5 +1;}if(digit1 == 6 || digit2 == 6 || digit3 == 6 || digit4 == 6 || digit5 == 6){count6 = count6 +1;}if(digit1 == 7 || digit2 == 7 || digit3 == 7 || digit4 == 7 || digit5 == 7){count7 = count7 +1;}if(digit1 == 8 || digit2 == 8 || digit3 == 8 || digit4 == 8 || digit5 == 8){count8 = count8 +1;}if(digit1 == 9 || digit2 == 9 || digit3 == 9 || digit4 == 9 || digit5 == 9){count9 = count9 +1;}}System.out.println("\n0" + "(" + count0 + ")");for (int i = 0; i < count0; i++){System.out.print("*");}System.out.println("\n1" + "(" + count1 + ")");for (int i = 0; i < count1; i++){System.out.print("*");}System.out.println("\n2" + "(" + count2 + ")");for (int i = 0; i < count2; i++){System.out.print("*");}System.out.println("\n3" + "(" + count3 + ")");for (int i = 0; i < count3; i++){System.out.print("*");}System.out.println("\n4" + "(" + count4 + ")");for (int i = 0; i < count4; i++){System.out.print("*");}System.out.println("\n5" + "(" + count5 + ")");for (int i = 0; i < count5; i++){System.out.print("*");}System.out.println("\n6" + "(" + count6 + ")");for (int i = 0; i < count6; i++){System.out.print("*");}System.out.println("\n7" + "(" + count7 + ")");for (int i = 0; i < count7; i++){System.out.print("*");}System.out.println("\n8" + "(" + count8 + ")");for (int i = 0; i < count8; i++){System.out.print("*");}System.out.println("\n9" + "(" + count9 + ")");for (int i = 0; i < count9; i++){System.out.print("*");}}}