-3

So I'm making a program for a Java class, and need to find the mean, mode, max, min, and median of a set of temperatures that I enter. Here's what I have so far:

import javax.swing.*;

public class Temps {
    private int temp[] = new int[5];
    String inputStr;
    int inputInt = 0;

    public void startApp()
    {
        for (int i = 0; i < temp.length; i++)
        {
            inputStr = JOptionPane.showInputDialog("Enter new temp.");
            inputInt = Integer.parseInt(inputStr);
            temp[i] = inputInt; 
        }

    }


    public static void main(String[] args)
    {
        Temps obj = new Temps();
        obj.startApp();     

    } 
}

Where and how do I start manipulating the values in the array? I have no idea where to start, and any help would be awesome.

beresfordt
  • 5,088
  • 10
  • 35
  • 43
  • Start by thinking about how you would calculate the mean/mode/max/min and create a method for each – beresfordt Feb 19 '15 at 02:00
  • would I put the method underneath public static void main or above it as part of startApp()? – James Rucksorf Feb 19 '15 at 02:02
  • 1
    The position of a method in a class isn't critical to the functioning of the method. I'd recommend one method for each thing you are trying to do eg; calculateMean, calculateMode, getMinimumTemp, getMaximumTemp – beresfordt Feb 19 '15 at 02:05
  • Position isnt important as beresfordt mentioned, but for readability/organization, above main. Each method should return the calculated value. You can then call them in main to output, for example. – D. Ben Knoble Feb 19 '15 at 02:14

2 Answers2

0

I have completed the mean method for you just so you can see how it should be done. I am guessing you can figure out the rest on your own based on my code. Note: it is not the most efficient or elegant way to do it, but it gets the job done. Good luck.

import javax.swing.*;

public class Temps {
    private static int temp[] = new int[5];
    String inputStr;
    int inputInt = 0;

    public void startApp() {
        for (int i = 0; i < temp.length; i++) {
            inputStr = JOptionPane.showInputDialog("Enter new temp.");
            inputInt = Integer.parseInt(inputStr);
            temp[i] = inputInt;
        }
    }

    public static int meanMethod(int temp[]) {

        int mean, sum;
        mean = 0;
        sum = 0;

        for (int i = 0; i < temp.length; i++) {
            sum += temp[i];
            if (i == (temp.length - 1)) {
                mean = sum / temp.length;
                return mean;
            }
        }
        return mean;
    }


    public static void main(String[] args) {
        Temps obj = new Temps();
        obj.startApp();
        System.out.println(obj.meanMethod(temp));

        /*

        You could do JOptionPane instead of System.out.println here. Just do:

        JOptionPane.showMessageDialog(null, obj.meanMethod(temp));

        */

    }
}
beresfordt
  • 5,088
  • 10
  • 35
  • 43
Jud
  • 1,324
  • 3
  • 24
  • 47
0

I'll give you one method to get you started. Suppose your array is int[] nums = {1,1,2,2,3};

We know the mean is calculated as (1+1+2+2+3)/5 = 9/5 = 1.8, and here's a method that does exactly that:

public double calculateMean(int[] nums) {
    double sum = 0; //<--if you use int here, your value would be 1.0 not 1.8
    for(int i = 0; i < nums.length; i++) {
        sum += nums[i];
    }
    return sum / nums.length;
}

You can use it in your main method:

int[] nums = {1,1,2,2,3};
double d = calculateMean(nums);
System.out.println(d); //the result is 1.8
ThisClark
  • 14,352
  • 10
  • 69
  • 100