Total members 11893 |It is currently Tue Nov 05, 2024 12:36 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Draw a chart for ranges of random numbers:
I have finished writing a program that asks users for how many numbers they want, and the maximum number size, the program then creates that amount of random numbers. I store the numbers in and array which must then be put into another array depending if they fall within a calculated range. I then have to print out a chart the shows how many numbers are in each range. I have completed the program but now i need to transfer the chart print out to a separate method and i'm struggling with that.

Heres my code...
Code:


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(== 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


} 





Attachments:
File comment: The code again
Assignment4.java [3.84 KiB]
Downloaded 864 times
Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

why you are creating the histogram in the main method .
Make a method for class Assignment that create Histogram and return array , and create another method that take the array returned and draw the histogram .

And in the main method create instance of class Assignment4 and call this methods .

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Print a chart that shows how many number in each range
 Draw pie chart php     -  
 How to generate dynamic 3d pie chart in php???     -  
 range of the short type     -  
 range of data type     -  
 range of the char type     -  
 make Dynamic Bar Chart by php     -  
 SQL Limit query for a range of records     -  
 Chart enhancement (upgrading to 3D preferable)     -  
 convert integer number to octal,hexadecimal number systems     -  
 convert octal number to decimal number     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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