Results 1 to 7 of 7
  1. #1
    Ejava is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default 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. #2
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    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. #3
    Ejava is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    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. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    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.

  5. #5
    Ejava is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    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. #6
    Ejava is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    :), 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. #7
    djr1 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Thumbs down 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

  1. maximum view depth?
    By Bit2_Gosu in forum New To Java
    Replies: 0
    Last Post: 02-16-2009, 02:32 PM
  2. Help With Minimum Value
    By Bodomar in forum New To Java
    Replies: 1
    Last Post: 11-07-2008, 04:29 AM
  3. To find the Maximum and Minimum in an Array of Strings
    By luscious in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 07-31-2008, 01:51 PM
  4. get the average and maximum score
    By Eric in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 04:15 AM
  5. Maximum size of an array
    By Hasan in forum New To Java
    Replies: 1
    Last Post: 05-20-2007, 11:11 AM

Posting Permissions

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