Results 1 to 7 of 7
Thread: Calculating Maximum and Minimum
- 02-28-2010, 11:53 PM #1
Member
- Join Date
- Feb 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?
- 03-01-2010, 02:10 AM #2
Member
- 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?
- 03-01-2010, 02:58 AM #3
Member
- Join Date
- Feb 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.
- 03-01-2010, 08:56 AM #4
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
:)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.
- 03-01-2010, 02:15 PM #5
Member
- Join Date
- Feb 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!
- 03-01-2010, 03:10 PM #6
Member
- Join Date
- Feb 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;
- 03-01-2010, 04:36 PM #7
Member
- 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; 03-01-2010 at 04:44 PM.
Similar Threads
-
maximum view depth?
By Bit2_Gosu in forum New To JavaReplies: 0Last Post: 02-16-2009, 02:32 PM -
Help With Minimum Value
By Bodomar in forum New To JavaReplies: 1Last Post: 11-07-2008, 04:29 AM -
To find the Maximum and Minimum in an Array of Strings
By luscious in forum JavaServer Pages (JSP) and JSTLReplies: 9Last Post: 07-31-2008, 01:51 PM -
get the average and maximum score
By Eric in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 04:15 AM -
Maximum size of an array
By Hasan in forum New To JavaReplies: 1Last Post: 05-20-2007, 11:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks