Question subject: Help putting something into a Method
Posted: Mon Nov 23, 2009 3:37 am
Joined: Mon Nov 23, 2009 3:26 am Posts: 1 Has thanked: 0 time Have thanks: 0 time
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...
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]; } } }
/*------------------------------------------------------------------------------------------------------------------------- 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;
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 88 times
msi_333
Question subject: Re: Help putting something into a Method
Posted: Fri Nov 27, 2009 2:30 pm
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2103 Location: Earth Has thanked: 39 time Have thanks: 56 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 .
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )