# Calculating average

• 03-31-2010, 06:48 AM
clocksaysits9
Calculating average
I tried this code but somehow on the cmd window the average pops out to be 0

//************************************************** ******************
// Student.java Author: Lewis/Loftus/Cocking
//
// Represents a college student.
//************************************************** ******************

public class Student
{
private String firstName, lastName;
private int score1 ;
private int score2 ;
private int score3 ;
private int average1;

//-----------------------------------------------------------------
// Sets up this Student object with the specified initial values.
//-----------------------------------------------------------------
public Student (String first, String last, Address home,
Address school, int scoreOne, int scoreTwo, int scoreThree)
{
firstName = first;
lastName = last;
score1 = scoreOne;
score2 = scoreTwo;
score3 = scoreThree;
}

public Student (String first, String last, Address home, Address school)
{
firstName = first;
lastName = last;
score1 = 0;
score2 = 0;
score3 = 0;
}

public int setTestScore ( int testNumber, int score)
{
if (testNumber == 1)
{
score1 = score;
}
else if (testNumber == 2)
{
score2 = score;
}
else if (testNumber == 3)
{
score3 = score;
}

return score;

}

public int getTestScore(int testNumber)
{
int score = 0;

if (testNumber == 1)
{
score = score1;
}
else if(testNumber == 2 )
{
score = score2;
}
else if (testNumber == 3)
{
score = score3;
}

return score;
}
public int average()
{
int averageMethod;
int sum;

sum = score1 + score2+ score3;
averageMethod = (sum/3);
averageMethod = average1;

return average1;

}

//-----------------------------------------------------------------
// Returns this Student object as a string.
//-----------------------------------------------------------------
public String toString()
{
String result;

result = firstName + " " + lastName + "\n";
result += "Home Address:\n" + homeAddress + "\n";
result += "School Address:\n" + schoolAddress + "\n";
result += "Score1 = " + score1 + "\n";
result += "Score2 = " + score2 + "\n";
result += "Score3 = " + score3 + "\n";
result += "Average of the scores entered = " + average1 + "\n";

return result;
}
}
• 03-31-2010, 06:52 AM
clocksaysits9
bump.............
• 03-31-2010, 07:17 AM
collin389
What does your main method look like? Do you ever set the values to something other than 0? are the scores <1? because you are saying int = int/3 therefore, if the numerator is <3 then it will truncate the decimal, and return 0.
• 03-31-2010, 07:39 AM
clocksaysits9
i pass in three values for scores1-3...they are 99, 98, 100
they are not less than three neither are they negative
• 04-06-2010, 05:03 AM
collin389
So you make a new Student("name", "last", Address1, Address2, 99, 98, 100)?
Does the getTestScore() work?
Maybe show us your main() so we can see what you are doing?