Total members 9951 | Gratitudes |It is currently Sat Feb 11, 2012 10:33 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Question
 Question subject: Help putting something into a Method
PostPosted: Mon Nov 23, 2009 3:37 am 
Offline
Newbie
User avatar

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];
}
}
}

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 88 times
TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Help putting something into a Method
PostPosted: Fri Nov 27, 2009 2:30 pm 
Offline
Mastermind
User avatar

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 )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 problem with JcheckBox method  Java  Anonymous  1
 What's your favourite eMarketing method?  General Discussion  elumchaitu  8
 is there any method to remove such watermark?  General Discussion  lovelyviki  0
 6 Approaches to Putting PowerPoint Presentation on Web  Macromedia Flash tutorials  kesole  1
 calling method to form- displaying textbox data in messagbox  C#  jackjones2  1

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: Google Adsense [Bot] and 3 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team