Reputation: 123
right now I am working on a program that is printing out a students name, id number, scores on exams, average score, and grade. For some reason there is a problem with the method that computes the average score. I have tried adding parenthesis and that didn't change the result either. This ultimately messes up the grade because the grade is calculated with the average score. Any help is appreciated, thanks! Here is my code:
import java.util.Scanner;
public class Student {
private String fname;
private String lname;
private int id;
private int score1;
private int score2;
private int score3;
private double average;
private String grade;
public void readInfo()
{
Scanner k = null;
k = new Scanner (System.in);
System.out.println ("Please enter the first name: (Enter John as first name and Doe as last name to stop) ");
fname = k.next();
System.out.println("Please enter the last name: ");
lname = k.next();
if (fname.equalsIgnoreCase("John")&&lname.equalsIgnoreCase("Doe"))
System.exit(0);
System.out.println("Please enter the student ID: ");
id = k.nextInt();
System.out.println("Please enter the first score: ");
score1 = k.nextInt();
System.out.println("Please enter the second score: ");
score2 = k.nextInt();
System.out.println("Please enter the third score: ");
score3 = k.nextInt();
}
//The problem lies in here:
private void computeAverage()
{
average = score1+score2+score3/3.0;
}
private void computeGrade()
{
if (average>=90&&average<=100)
grade = "A";
else if (average>=80&&average<=89.9)
grade = "B";
else if (average>=70&&average<=79.9)
grade = "C";
else if (average>=60&&average<=69.9)
grade = "D";
else
grade = "F";
}
private String getName()
{
return fname + " " + lname;
}
private double getAverage()
{
return average;
}
public void printAll()
{
System.out.printf("%-9s%-10s %-9s %-3s %-3s %-3s %-5s %-5s", "LastName","FirstName","ID","S1","S2","S3","AVG","GRADE");
System.out.println();
System.out.println("-----------------------------------------------------");
System.out.printf("%-9s%-9s %-9d %-3d %-3d %-3d %-3.2f%-2s",lname,fname,id,score1,score2,score3,average,grade);
System.out.println();
System.out.println("-----------------------------------------------------");
}
}
Upvotes: 0
Views: 93
Reputation: 305
Upvotes: 1
Reputation: 7561
Order of operations. What you are doing is sum 1 + sum 2
plus the quotient of sum3 and 3. When you divide, you must divide by exactly what you need to be divided. You should be doing (sum 1+ sum 2+ sum 3)/3
If you don't want to divide by 2, you can also multiply by one half, that won't change anything, but it's just another option.
(sum 1+ sum 2+ sum 3)*1/2
Keep in mind what you want to divide. If it is more term, take advantage of grouping symbols!
{Rich}
Upvotes: 0