Results 1 to 5 of 5
  1. #1
    clocksaysits9 is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default 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;
    }
    }

  2. #2
    clocksaysits9 is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    bump.............

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    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.

  4. #4
    clocksaysits9 is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    i pass in three values for scores1-3...they are 99, 98, 100
    they are not less than three neither are they negative

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    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?

Similar Threads

  1. Replies: 1
    Last Post: 03-11-2010, 05:21 AM
  2. Help compeleting four average mark.
    By lebron11 in forum New To Java
    Replies: 7
    Last Post: 03-08-2010, 07:11 PM
  3. Test score average
    By ryn21 in forum New To Java
    Replies: 11
    Last Post: 10-17-2008, 06:49 AM
  4. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 12:09 PM
  5. sum,product,average program
    By chitwood in forum New To Java
    Replies: 7
    Last Post: 01-24-2008, 06:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •