# Calculating Maximum and Minimum

• 03-01-2010, 12:53 AM
Ejava
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, 03:10 AM
Onra
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, 03:58 AM
Ejava
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, 09:56 AM
Supamagier
Code:

```int max = Integer.MIN_VALUE; for (int i = 0; i < numbers.length; ++i)     max = (max >= numbers[i] ? max : numbers[i]);```
:)
• 03-01-2010, 03:15 PM
Ejava
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, 04:10 PM
Ejava
:), I was able to figure out, I used if statements and boxed the variables:
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, 05:36 PM
djr1
Sorting with arrays
Or if you put the numbers in an array. You can easily find the max value.

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.