0

Would someone please show me how i could make min and max into methods ? I really have no idea and i tried lots of things but i failed I am only a beginner .................................................................. Here is my code now

class ArrayCalculator
{
public static void main(String arng[])
{
    int max=0;      // sum of maximum number in array
    int min=0;      // sum of minimum numbers in array
    int total=0;    // sum of total nnymbers in array
    int average=0;  // sum of average number in array

    System.out.println("");
    System.out.println("*****************WELCOME TO ARRAY CALCULATOR****************"); // Introducton to calculater
    System.out.println("****************-----------------------------***************");
    System.out.println("*****************---------------------------****************"); 
    System.out.println("How Many Numbers Would You Like To Calculate?"); // read in numbers entered by user
    int Items = EasyIn.getInt();
    int[] numbers = new int[Items]; // array for numbers entered

    for(int i = 0; i< Items; i++)
    {
        System.out.println("Enter number " + (i+1)); // read in each figure entered by user
        numbers[i]=EasyIn.getInt();
        total=numbers[i];
        total = total + numbers[i];
        min=numbers[0];
        max=numbers[0];

        if (numbers[i] > max) // calculations for maximum number
        {
            max = numbers[i];
        }

        else if (numbers[i] < min) // calculations for minimum number
        {
            min = numbers[i];
        }
    }

    average = total/Items; // calculations for average number
    System.out.println("*********************************************");
    System.out.println("You have entered " +numbers.length+" numbers"); // display count of numbers entered
    System.out.println("Largest number in array is " +max); // display maximum number
    System.out.println("Smallest number in array is " +min); // display minimum number
    System.out.println("Total Numbers is " +total); // display total sum of numbers
    System.out.println("Average is " +average); // display average number
    System.out.println("Thank You");
    System.out.println("*********************************************");
}
}

Editors note: The above code is duplicated below with Scanner instead of EasyIn

import  java.util.Scanner;
    public class ArrayCalculator  {
    public static void main(String arng[])
    {
        Scanner scanr = new Scanner(System.in);
        int max=0;      // sum of maximum number in array
        int min=0;      // sum of minimum numbers in array
        int total=0;    // sum of total nnymbers in array
        int average=0;  // sum of average number in array

    System.out.println("");
    System.out.println("*****************WELCOME TO ARRAY CALCULATOR****************"); // Introducton to calculater
    System.out.println("****************-----------------------------***************");
    System.out.println("*****************---------------------------****************");

        System.out.println("How Many Numbers Would You Like To Calculate?"); // read in numbers entered by user
        int Items = scanr.nextInt();
        scanr.nextLine();
        int[] numbers = new int[Items]; // array for numbers entered

        for(int i = 0; i< Items; i++)
        {
            System.out.println("Enter number " + (i+1)); // read in each figure entered by user
            numbers[i]=scanr.nextInt();
            scanr.nextLine();
            total=numbers[i];
            total = total + numbers[i];
            min=numbers[0];
            max=numbers[0];
            if (numbers[i] > max) // calculations for maximum number
            {
                max = numbers[i];
            }
            else if (numbers[i] < min) // calculations for minimum number
            {
                min = numbers[i];
            }
        }

        average = total/Items; // calculations for average number
        System.out.println("*********************************************");
        System.out.println("You have entered " +numbers.length+" numbers"); // display count of numbers entered
    System.out.println("Largest number in array is " +max); // display maximum number
    System.out.println("Smallest number in array is " +min); // display minimum number
    System.out.println("Total Numbers is " +total); // display total sum of numbers
        System.out.println("Average is " +average); // display average number
        System.out.println("Thank You");
        System.out.println("*********************************************");
    }
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52

3 Answers3

0

You should delete those lines. Otherwise you reset the total, max and min each time you set a number.

total=numbers[i];
min=numbers[0];
max=numbers[0];

And instead of

else if (numbers[i] < min)

You can simply write

else
s.alem
  • 12,579
  • 9
  • 44
  • 72
0

Another approach to this can be done as follows:

Once numbers is populated,

List<Integer> list = Arrays.asList(numbers);
int max = Collections.max(list);
int min = Collections.min(list);

Just to show that there is a lot of stuff that java does for you that you don't have to re-invent.

There are many more functions in Collections See http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

user2573153
  • 254
  • 2
  • 14
  • In an educational context, this is great information. In the real world, I'd avoid translating arrays to a collections, given how frequently it's done in this application. – aliteralmind Apr 03 '14 at 01:31
0

Here is a function to get the maximum integer from an array. With one easy change, you can also make it also into a min function.

public static final int getMaxNum(int[] numbers)  {
   int max = Integer.MIN_VALUE;
   for(int i : numbers)  {
      if(i > max)  {
         max = i;
      }
   }
   return  max;
}

Call it with

int max = getMaxNum(numbers);
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
  • THANK YOU !! also do you have any idea how to get the median number ? – user3491682 Apr 03 '14 at 01:21
  • I'll leave that one up to you, but [this question](http://stackoverflow.com/questions/3691940/finding-the-median-value-of-an-array) should help. Hint: Use your new fangled `getMaxNum` and `getMinNum` to assist you. :) – aliteralmind Apr 03 '14 at 01:23