Results 1 to 5 of 5
Thread: Calculating average
- 03-31-2010, 06:48 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
Calculating average
I tried this code but somehow on the cmd window the average pops out to be 0
please help::
//************************************************** ******************
// Student.java Author: Lewis/Loftus/Cocking
//
// Represents a college student.
//************************************************** ******************
public class Student
{
private String firstName, lastName;
private Address homeAddress, schoolAddress;
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;
homeAddress = home;
schoolAddress = school;
score1 = scoreOne;
score2 = scoreTwo;
score3 = scoreThree;
}
public Student (String first, String last, Address home, Address school)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
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 #2
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
bump.............
- 03-31-2010, 07:17 AM #3
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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 #4
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
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 #5
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Similar Threads
-
Need help in calculating average value on this format ....
By motress in forum New To JavaReplies: 1Last Post: 03-11-2010, 04:21 AM -
Help compeleting four average mark.
By lebron11 in forum New To JavaReplies: 7Last Post: 03-08-2010, 06:11 PM -
Test score average
By ryn21 in forum New To JavaReplies: 11Last Post: 10-17-2008, 05:49 AM -
Calculate Average
By sthack99 in forum New To JavaReplies: 4Last Post: 06-13-2008, 11:09 AM -
sum,product,average program
By chitwood in forum New To JavaReplies: 7Last Post: 01-24-2008, 05:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks