Results 1 to 4 of 4
  1. #1
    jtyler is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default Simple program help

    Stuck on the last part of my assignment..not sure how to finish the last 2 methods ...
    Java Code:
        public boolean equals(StudentRecord other) 
        {
            // here
        }
    
        public String toString() 
        {
            // here
        }
    (at the bottom of the first code)

    The program should read in the studentís scores and output the studentís grade record, which consists of three quiz scores and two exam scores as well as the studentís overall numeric score for the entire course and final letter grade. Define and use a class for the student record. The class should have instant variables for the quizzes, midterm, final, overall numeric score for the course, and final letter grade. The overall numeric score is a number in the range of 0 to 100, which represents the weighted average of the studentís work. The class should have methods to compute the overall numeric grade and the final letter grade. These last methods should be void methods that set the appropriate instance variables. Your class should have a reasonable set of accessor and mutator methods, an equals method, and a toString method, whether or not your program uses them. You may add other methods if you wish.

    Java Code:
    public class StudentRecord 
    {
    
        private int quiz1;
        private int quiz2;
        private int quiz3;
        private int midterm;
        private int finalExam;
    
        private double numericScore;
        private char letterGrade;
    
        private final int A_GRADE = 90;
        private final int B_GRADE = 80;
        private final int C_GRADE = 70;
        private final int D_GRADE = 60;
    
        public StudentRecord (int quiz1, int quiz2, int quiz3, 
            int midterm, int finalExam)
        {
            this.quiz1 = quiz1;
            this.quiz2 = quiz2;
            this.quiz3 = quiz3;
            this.midterm = midterm;
            this.finalExam = finalExam;
        }
        
        public void computeNumericScore() 
        {
            numericScore=(finalExam*0.4)+(((quiz1+quiz2+quiz3)*10)*0.25)+(midterm*0.35)
        }
                
    
        public void computeLetterGrade()
        {
            computeNumericScore();
            
            if (numericScore >= 90)
            numericScore = "A";
            
            else if ((numericScore >= 80) & (numericScore < 90))
            numericScore = "B";
            
            else if ((numericScore >= 70) & (numericScore < 80))
            numericScore = "C";
            
            else if ((numericScore >= 60) & (numericScore < 70))
            numericScore = "D";
            
            else if (numericScore < 60)
            numericScore = "F";
            
            return numericScore;
        }
    
        public boolean equals(StudentRecord other) 
        {
            // here
        }
    
        public String toString() 
        {
            // here
        }
    
    }// StudentRecord
    Java Code:
    import java.util.Scanner;
    
    public class StudentRecordDemo
    {
        public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
    
            StudentRecord studentA = new StudentRecord(8, 9, 7, 86, 91);
    
            System.out.println("Enter the student's score on the first quiz (0 - 10): ");
            int quiz1 = keyboard.nextInt();
    
            System.out.println("Enter the student's score on the second quiz (0 - 10): ");
            int quiz2 = keyboard.nextInt();
    
            System.out.println("Enter the student's score on the third quiz (0 - 10): ");
            int quiz3 = keyboard.nextInt();
    
            System.out.println("Enter the student's score on the midterm (0 - 100): ");
            int midterm = keyboard.nextInt();
    
            System.out.println("Enter the student's score on the final (0 - 100): " );
            int finalExam = keyboard.nextInt();
    
            StudentRecord studentB =
                new StudentRecord(quiz1, quiz2, quiz3, midterm, finalExam);
            
            System.out.println("Student A's record: " + studentA); 
            System.out.println("Student B's record: " + studentB); 
    
            if (studentA.equals(studentB))
                System.out.println("Student A has a same record as Student B");
            else    
                System.out.println("Student A has a different record than Student B");
    
        }
    } // StudentRecordDemo

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    In the equals() function, you'll want to compare things like quiz1, quiz2, and so on, to the identical values in the other variable, then return true or false depending on the result. Depending on how you want the toString() method to output, it will vary. Perhaps you just want it to return the grade letter, in which case you could return the letterGrade variable.

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    Usually a toString() method returns some human readible helpful information about what the object values are, in this case we might want to make it display the values of the student record member variables
    Java Code:
    public String toString() {
      return "Student Record: (" + quiz1 + ", " + quiz2 + ", " + quiz3 + ", " + midterm + ", " + finalExam + ")");
    }
    Where it just prints all the fields here in simple comma separated in their order they are declared.


    the equals() method works to return true if and only if the parameter (another instance of this object type, has exactly the same values as this one.
    Java Code:
      public boolean equals(Object other) {
        if (!(other instanceof StudentRecord)) {
          return false;
        }
      StudentRecord o = (StudentRecord) o;
      return (o.quiz1 == this.quiz1)
        && (o.quiz2 == this.quiz2)
        // and so on, continue to compare all of the fields here 
        ;
      }
    The equals method is always harder to implement, and depends on the fields in your class and what you want to consider the identity fields to compare to other object instances.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    And go through the contract for equals(...) and hashCode() defined in the API for Object. Briefly, if you override equals(...) it is generally necessary to also override hashCode() to return the same hash for instances that you determine to be equal.

    db

Similar Threads

  1. simple program
    By blastoff in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 11:25 PM
  2. My Simple Hashing Program
    By Simplev_v in forum New To Java
    Replies: 4
    Last Post: 09-07-2009, 06:10 PM
  3. simple chat program
    By munishmhr in forum Networking
    Replies: 2
    Last Post: 03-25-2009, 04:00 PM
  4. Simple Program
    By TheRocket in forum Advanced Java
    Replies: 15
    Last Post: 12-30-2008, 02:35 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:40 AM

Posting Permissions

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