Mon Nov 23, 2009 3:37 am
import javax.swing.* ;
public class Assignment4 {
public static void main(String[]args) {
double increment;
double check;
int numberOfBins;
int count = 0;
double maximum = -10;
double minimum = 10;
int max;
int size;
double range;
size = getIntFromInterval("Enter the size of the Histogram (1-200)", 1, 200);
max = getIntFromInterval("Enter the maximum value of the Histogram (1-200)", 1, 200);
double [] array1 = new double [size];
System.out.print("Random Values:");
for (int i = 0; i < size; i++){
count++;
array1[i] = Math.rint((Math.log(Math.random( ) * max))*10)/10;
if(i == 0){
if (array1[i] > 0){
System.out.println("\t " + array1[i]);
}
else {
System.out.println("\t" + array1[i]);
}
}
else{
if (array1[i] >= 0){
System.out.println("\t\t " + array1[i]);
}
else {
System.out.println("\t\t" + array1[i]);
}
if (array1[i] > maximum){
maximum = array1[i];
}
if (array1[i] < minimum){
minimum = array1[i];
}
}
}
System.out.println("-------------------------------------------------------------------");
System.out.println("Total Values: \t " + size);
range = (maximum - minimum);
if(maximum >= 0){
System.out.println("\nMaximum Value is:\t " + maximum);
}
else{
System.out.println("\nMaximum Value is:\t" + maximum);
}
if(minimum >= 0){
System.out.println("Minimum Value is:\t " + minimum);
}
else{
System.out.println("Minimum Value is:\t" + minimum);
}
System.out.println("Range of Values is:\t " + (Math.rint(range*10)/10));
System.out.println("\nHistogram of Values Starting at: ");
numberOfBins = 0;
numberOfBins = getIntFromInterval("Enter Number of Bins (2-40)", 2, 40);
int [] array2 = new int [numberOfBins];
for (int i = 0; i < size; i++) {
int binNumber = (int)Math.min((((array1[i] - minimum) * numberOfBins)/range) , (numberOfBins - 1));
array2[binNumber] = array2[binNumber]+1;
}
increment = range/numberOfBins;
check = minimum;
//this is the part that prints out my chart, i need to move it into a method somehow.
for (int i = 0; i < numberOfBins; i++){
if (check > 0){
System.out.print(" " + (Math.rint(check*10)/10) + " \t(" + array2[i] + ") \t");
}
else{
System.out.print((Math.rint(check*10)/10) + " \t(" + array2[i] + ") \t");
}
for(int j = 0; j < array2[i]; j++){
System.out.print("*");
}
check = (Math.rint((check + increment)*100)/100);
System.out.println();
}
}
/*-------------------------------------------------------------------------------------------------------------------------
Name: getIntFromInterval
Purpose: Asks the user for a value between the min and max value and returns the inputted number
@param: A preset maximum value (int)
A preset minimum value (int)
A preset prompt message (String)
@return: Returns the user inputted value between the two intervals (int)
-------------------------------------------------------------------------------------------------------------------------*/
public static int getIntFromInterval(String prompt, int minValue, int maxValue){
boolean inputOk;
String errorMsg;
String input;
int numTickets = 0;
inputOk = false ;
errorMsg = "" ;
while ( ! inputOk ) {
input = JOptionPane.showInputDialog(null, errorMsg + prompt) ;
numTickets = Integer.parseInt(input.trim()) ;
if ( numTickets >= minValue && numTickets <= maxValue)
inputOk = true ; // if number of tickets imputted is in the range the condition is satisfied
else {
errorMsg = "You can only select from " + minValue + " to " + maxValue
+ " not " + numTickets + " as you requested\n" ;
System.out.println( errorMsg ) ;
}
}//ends while when condition is satisfied
return numTickets;
}//ends getIntFromInterval
}
Fri Nov 27, 2009 2:30 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.