Results 1 to 4 of 4
  1. #1
    ALH813 is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default need help with implementing class

    I have a project where I need to have 3 quiz scores for 3 students. I need to print out the total score and the average score. I need to store the number of quizzes taken so it can compute the average score but I don't know how to do this. Please help me. :o Thanks!!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    One class per student with with three variables, a name, a count, and a total, a constructor that takes a name as an argument, and four methods, getName, getTotal, getAverage, and addScore.

  3. #3
    ALH813 is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    I did all that and it has printed the total score and the name. Now I need to store the number of quizzes taken so that the program will compute the average score. This is what I have:

    public class Student {

    private double totalscore;
    private String studentName;
    private double average;

    public Student(){
    totalscore = 0;
    average = totalscore/3;
    }

    public Student (String name){
    studentName = name;
    }


    public void addQuiz(int score){
    totalscore = totalscore + score;
    }

    public void AverageScore(){
    average = totalscore/3;
    }


    public double getAverageScore(){
    return average;
    }

    public double getTotalScore(){
    return totalscore;
    }

    public String getName(){
    return studentName;
    }
    (this is the Student Class)


    public class StudentTester {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Student Amanda = new Student("Amanda Hunter");
    Amanda.addQuiz(90);
    Amanda.addQuiz(95);
    Amanda.addQuiz(87);
    Amanda.getTotalScore();
    Amanda.getName();
    Amanda.getAverageScore();
    System.out.println(Amanda.getName());
    System.out.println(Amanda.getTotalScore());
    System.out.println(Amanda.getAverageScore());
    System.out.println();

    Student Brian = new Student("Brian Wyman");
    Brian.addQuiz(100);
    Brian.addQuiz(90);
    Brian.addQuiz(97);
    Brian.getTotalScore();
    Brian.getName();
    Brian.getAverageScore();
    System.out.println(Brian.getName());
    System.out.println(Brian.getTotalScore());
    System.out.println(Brian.getAverageScore());
    System.out.println();

    Student Ben = new Student("Ben Hunter");
    Ben.addQuiz(85);
    Ben.addQuiz(96);
    Ben.addQuiz(92);
    Ben.getTotalScore();
    Ben.getName();
    Ben.getAverageScore();
    System.out.println(Ben.getName());
    System.out.println(Ben.getTotalScore());
    System.out.println(Ben.getAverageScore());
    System.out.println();
    }
    (this is the StudentTester Class)

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    In the spot where you add a score increment a variable that is keeping the "count" as well as adding that score to the total. You do not need a variable to keep the average (never said you did), you need variables to keep the total and the count. For the average score, simply do the division in the "getAverageScore" method and you can remove the "AverageScore" method. And, you can shorten this sort of thing
    Java Code:
    totalscore = totalscore + score;
    to
    Java Code:
    totalscore += score;
    (including /= -= *= operations)

Similar Threads

  1. Implementing Iterator
    By Harb in forum New To Java
    Replies: 13
    Last Post: 11-21-2009, 07:25 PM
  2. Help implementing a method
    By VinceGuad in forum New To Java
    Replies: 3
    Last Post: 03-16-2009, 07:39 AM
  3. help! implementing an interface
    By manda147 in forum New To Java
    Replies: 28
    Last Post: 11-17-2008, 04:27 AM
  4. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 06:42 PM
  5. Help implementing JDBC
    By mooey1232003 in forum New To Java
    Replies: 6
    Last Post: 07-11-2007, 10:15 PM

Posting Permissions

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