# Thread: Calculating Maximum and Minimum

1. Member
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?

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?

3. Member
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.

4. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
10
Java Code:
```int max = Integer.MIN_VALUE;
for (int i = 0; i < numbers.length; ++i)
max = (max >= numbers[i] ? max : numbers[i]);```
:)

5. Member
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!

6. Member
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;```

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 05:44 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•