The following is a program that creates a histogram for a frequency distrubution using java.
Solution
//**************************************
// Histogram.java
// CS151A, Assignment 8
// Creates a Histogram for a frequency distrubution
//********************************************
import java.util.*;
public class Histogram
{
public static void main (String[] args)
{
Random generator = new Random();
int num; //the individual integers//
int setamount; //how many integers there will be//
int count = 0;
String answer; //decides whether to use user input or computer generated integers//
Scanner keyboard = new Scanner(System.in);
//String array list for range data//
String [] range =
{
"1 - 10 |",
"11 - 20 |",
"21 - 30 |",
"31 - 40 |",
"41 - 50 |",
"51 - 60 |",
"61 - 70 |",
"71 - 80 |",
"81 - 90 |",
"91 - 100 |",
};
//choice between user generated and computer generated numbers//
System.out.println ("Would you like to imput numbers, or have the computer generate a list?");
System.out.println ("Type me to enter the integers yourself, or computer to have the computer generate them.");
answer = keyboard.next();
//Manually generated list//
if (answer.equals("me"))
{
System.out.println("How many numbers are you going to enter");
setamount = keyboard.nextInt();
while (count < setamount)
{
count ++;
System.out.println("please enter a between 1 and 100");
num = keyboard.nextInt();
if (num > 0 && num <= 10)//placing integers into ranges//
{
range[0] += "*";
}
else if (num >= 10 && num <= 20)
{
range[1] += "*";
}
else if (num >= 20 && num <= 30)
{
range[2] += "*";
}
else if (num >= 30 && num <= 40)
{
range[3] += "*";
}
else if (num >= 40 && num <= 50)
{
range[4] += "*";
}
else if (num >= 50 && num <= 60)
{
range[5] += "*";
}
else if (num >= 60 && num <= 70)
{
range[6] += "*";
}
else if (num >= 70 && num <= 80)
{
range[7] += "*";
}
else if (num >= 80 && num <= 90)
{
range[8] += "*";
}
else if (num >= 90 && num <= 100)
{
range[9] += "*";
}
}
//Prints out the beginning of each row//
System.out.println(range[0]);
System.out.println(range[1]);
System.out.println(range[2]);
System.out.println(range[3]);
System.out.println(range[4]);
System.out.println(range[5]);
System.out.println(range[6]);
System.out.println(range[7]);
System.out.println(range[8]);
System.out.println(range[9]);
}
//Computer genetated list//
else if (answer.equals("computer"))
{
setamount = generator.nextInt(50); //Max amount is 50 to keep number//
//of integers easily readable //
while (count < setamount)
{
num = generator.nextInt(99)+1; //generates the specific integer//
count++;
if (num >= 0 && num <= 10)//placing integers into ranges//
{
range[0] += "*";
}
else if (num >= 10 && num <= 20)
{
range[1] += "*";
}
else if (num >= 20 && num <= 30)
{
range[2] += "*";
}
else if (num >= 30 && num <= 40)
{
range[3] += "*";
}
else if (num >= 40 && num <= 50)
{
range[4] += "*";
}
else if (num >= 50 && num <= 60)
{
range[5] += "*";
}
else if (num >= 60 && num <= 70)
{
range[6] += "*";
}
else if (num >= 70 && num <= 80)
{
range[7] += "*";
}
else if (num >= 80 && num <= 90)
{
range[8] += "*";
}
else if (num >= 90 && num <= 100)
{
range[9] += "*";
}
}
//Prints out the beginning of each row//
System.out.println(range[0]);
System.out.println(range[1]);
System.out.println(range[2]);
System.out.println(range[3]);
System.out.println(range[4]);
System.out.println(range[5]);
System.out.println(range[6]);
System.out.println(range[7]);
System.out.println(range[8]);
System.out.println(range[9]);
}
//Asks for a proper answer//
else System.out.println("Not a valid entry, please type *me* or *computer*");
}
}
No comments:
Post a Comment