Calculate Average Java

62.8k views Asked by At

I'm trying to calculate the average of the student scores and as it stands I'm struggling with the calculation part.

In my assignment I was asked to caulculate the avg age of three students and avg. heigth of three students.

Could someone please guide me in the last line of this code, I apologize for the newbie question, really stuck and cannot come up with any solution to why it's not calculating my numbers.

public static void main(String[] args) {
// this method will display the scores of students 

//variable declaration
int JasperAge = 20; 
int JasperHeigth = (int) 175.5;
int PaulaAge = 25; 
int PaulaHeigth = (int) 190.5;
int NicoleAge = 18;
int NicoleHeigth = (int) 165;

//output
Scanner output = new Scanner (System.in);


System.out.println("Name\t "+ " Age\t " + " Height (cm)\t");
System.out.println("\n");
System.out.println("Jasper\t  "+JasperAge+"  \t    "+JasperHeigth);
System.out.println("Paula\t  "+PaulaAge+"  \t    "+PaulaHeigth);
System.out.println("Nicole\t  "+NicoleAge+" \t    "+NicoleHeigth);
System.out.println("Average\t ((20 + 25 + 18) /3) \t ((175.5 + 190.5 + 165) /3)");
}

}

4

There are 4 answers

4
mattias On BEST ANSWER

Your last line should probably be something like:

System.out.println("Average age: " + ((JasperAge + PaulaAge + NicoleAge) /3) + ". Average height: " +  ((JasperHeigth + PaulaHeigth + NicoleHeigth) /3) ".");

Mind my calculations, but you get the idea.

4
NerosE On

You just have to make your last print like this:

System.out.println("Average\t  " + ((20 + 25 + 18) /3) + "\t    " + ((175.5 + 190.5 + 165) /3));

or even better use your variables:

System.out.println("Average\t  " + ((JasperAge + PaulaAge + NicoleAge) /3) + "\t    " + ((JasperHeigth + PaulaHeigth + NicoleHeigth) /3));
1
Christian Wilkie On

There are a few things wrong:

int JasperHeigth = (int) 175.5;
int PaulaHeigth = (int) 190.5;
int NicoleHeigth = (int) 165;

Given that these appear to be heights with decimal values, it is likely that you would want to store these as doubles instead of ints. When you declare a value like 175.5 as an integer, it is actually truncated to instead be 175. To store the full value of these numbers, you should instead define them as:

    double JasperHeigth = 175.5;
    double PaulaHeigth = 190.5;
    double NicoleHeigth = 165;

Side note: the reason you had to cast those numbers using (int) was because 175.5 is actually a double literal instead of an int, which was what you declared the variable as before.

Next, this scanner definition line is never used:

Scanner output = new Scanner (System.in);

You would use the Scanner class to get input from the user. For instance, if you wanted the user to enter in some names or numbers. In this case, it doesn't look like you need to request any input from the user so this line can probably be deleted.

And lastly, the problem with displaying your output is in this line:

System.out.println("Average\t ((20 + 25 + 18) /3) \t ((175.5 + 190.5 + 165) /3)");

The problem is that by enclosing the numbers within quotation marks, your expected arithmetic will not be evaluated and instead just displayed to the user as character data. If you wanted to evaluate those expressions you could instead pull the math operations out of the quotation marks and concatenate them to the String data using the + operator:

System.out.println("Average\t" +  ((20 + 25 + 18) /3) + "\t" + ((175.5 + 190.5 + 165) /3));

However, there are still a few things wrong with this. First, ((20 + 25 + 18) /3) will evaluate as integer division. It will reduce to 63/3 which is 21. However, it would also display 21 if you had 64/3 or 65/3, because integer division truncates the part of the number past the decimal point. To prevent truncation of your desired result, you can cast either one of the numbers in the numerator or denominator to double, or divide by a double literal such as 3.0. So something like this:

System.out.println("Average\t" +  ((20 + 25 + 19) /3.0) + "\t" + ((175.5 + 190.5 + 165) /3.0));

Then finally, none of these numbers are actually using the variables you defined earlier, they are completely separate. If you want to actually average the variables you will need to substitute them into the expression like this:

System.out.println("Average\t" +  ((JasperAge + PaulaAge + NicoleAge) /3.0) + "\t" + ((JasperHeigth + PaulaHeigth + NicoleHeigth) /3.0));

Summary

Here is a program with all my suggested edits:

public static void main(String[] args) {
    // this method will display the scores of students

    //variable declaration
    int JasperAge = 20;
    double JasperHeigth = 175.5;
    int PaulaAge = 25;
    double PaulaHeigth = 190.5;
    int NicoleAge = 18;
    double NicoleHeigth = 165;

    System.out.println("Name\t "+ " Age\t " + " Height (cm)\t");
    System.out.println("\n");
    System.out.println("Jasper\t  "+JasperAge+"  \t    "+JasperHeigth);
    System.out.println("Paula\t  "+PaulaAge+"  \t    "+PaulaHeigth);
    System.out.println("Nicole\t  "+NicoleAge+" \t    "+NicoleHeigth);
    System.out.println("Average\t" +  ((JasperAge + PaulaAge + NicoleAge) /3.0) + "\t" + ((JasperHeigth + PaulaHeigth + NicoleHeigth) /3.0));
}
1
duffymo On

Java's an object-oriented language. You might just be starting, but it's never too soon to learn about encapsulation:

public class Student {
    private final String name;
    private final int age;  // bad idea - why?
    private final double heightInCm;

    public Student(String n, int a, double h) {
        this.name = n;
        this.age = a;
        this.heightInCm = h;
    }

    public String getName() { return this.name; }
    public int getAge() { return this.age; }
    public double getHeightInCm() { return this.heightInCm; }

    public String toString() {
        return String.format("name: '%s' age: %d height: %10.2f (cm)", this.name, this.age, this.heightInCm);
    }
}