Results 1 to 7 of 7
Thread: Calculating Maximum and Minimum
 03012010, 12:53 AM #1Member
 Join Date
 Mar 2010
 Posts
 6
 Rep Power
 0
Calculating Maximum and Minimum
Hi guys, I have started doing programming at my college from since last semester. Last semester we did some very basic work in Visual Basic and this semester have started learning java using the netbeans IDE. The professor has given us an assignment to utilise 2 java classes for an app to calculate the maximum, the minimum and average of 3 integer numbers entered by the user. I have been able to get the formula for the average coded but am unable to get the max and min calculated. On the example he showed us he used some IF statements, however in that example he used 1 class for the entire app. He wants us to use a constructor and 2 classes, any tips on getting this done?
 03012010, 03:10 AM #2Member
 Join Date
 Nov 2009
 Posts
 41
 Rep Power
 0
What do you want to know?
I'd use one class for the main method and one class for the calculations.
An object of the calculation class has the 3 int values and now you have to write methods for it.
In the main method you read in 3 integers and assign them to variables.
Then create an object of the calculation class and give it as arguments your 3 variables.
And then you do
System.out.println(object.max());
System.out.println(object.min());
System.out.println(object.avg());
Like that?
 03012010, 03:58 AM #3Member
 Join Date
 Mar 2010
 Posts
 6
 Rep Power
 0
well currently i have the 2 classes like how you suggested.
For the average calculation i use:
/** this calculates the average*/
public float calculateAvg(){
float avg = 0.0f;
avg = (num1 + num2 + num3)/ 3.0f;
return avg;
Using JOptionPane.showMessageDialog to output the result in a message box.
I'm trying to find a way to calculate the minimum and maximum.
 03012010, 09:56 AM #4Senior Member
 Join Date
 Aug 2008
 Posts
 384
 Rep Power
 7
Java Code:int max = Integer.MIN_VALUE; for (int i = 0; i < numbers.length; ++i) max = (max >= numbers[i] ? max : numbers[i]);
I die a little on the inside...
Every time I get shot.
 03012010, 03:15 PM #5Member
 Join Date
 Mar 2010
 Posts
 6
 Rep Power
 0
That code calculates the maximum? I inputted it into my code but now I need to get the "numbers" variable declared. Thanks for the help on this guys, still new to java so its a bit of a headache for me :(, but i will learn it!
 03012010, 04:10 PM #6Member
 Join Date
 Mar 2010
 Posts
 6
 Rep Power
 0
:), I was able to figure out, I used if statements and boxed the variables:
Java Code:/** this calculates the average*/ public float calculateAvg(){ float avg = 0.0f; avg = (num1 + num2 + num3)/ 3.0f; return avg; } /** this calculates the minimum*/ public int calculateMin(){ Integer x,y,z; x = num1; y = num2; z = num3; int min = 0; if((x < y) && (x < z)){ min = x; } else if((y < x) && (y < z)){ min = y; } else if ((z < x) && (z < y)){ min = z; } return min; } /** this calcuulates the maximum*/ public int calculateMax(){ Integer x,y,z; x = num1; y = num2; z = num3; int max = 0; if((x > y) && (x > z)){ max = x; } else if((y > x) && (y > z)){ max = y; } else if ((z > x) && (z > y)){ max = z; } return max;
 03012010, 05:36 PM #7Member
 Join Date
 Feb 2010
 Posts
 5
 Rep Power
 0
Sorting with arrays
Or if you put the numbers in an array. You can easily find the max value.
Java Code://Let you choose part of the array. public static int max(int[] a, int from, int too) { if (from == too) throw new NoSuchElementException("Empty!"); int m = from; int max = a[from]; for (int i = from + 1; i < too; i++) if (a[i] > max) { m = i; max = a[m]; } return max; //will return the max value } //Calls the above method, for the whole array.. public static int max(int[] a) { return max(a, 0, a.length); }
This will work for any int array with more than one value.Last edited by djr1; 03012010 at 05:44 PM.
Similar Threads

maximum view depth?
By Bit2_Gosu in forum New To JavaReplies: 0Last Post: 02162009, 03:32 PM 
Help With Minimum Value
By Bodomar in forum New To JavaReplies: 1Last Post: 11072008, 05:29 AM 
To find the Maximum and Minimum in an Array of Strings
By luscious in forum JavaServer Pages (JSP) and JSTLReplies: 9Last Post: 07312008, 02:51 PM 
get the average and maximum score
By Eric in forum Advanced JavaReplies: 2Last Post: 07012007, 05:15 AM 
Maximum size of an array
By Hasan in forum New To JavaReplies: 1Last Post: 05202007, 12:11 PM
Bookmarks