creating classes, trying to compare 2 objects

Discussion in 'JavaScript' started by aperez053054, Jul 23, 2010.

  1. #1
    We're learning about creating classes, so i have my class titled Student.java and my 'driver' called grades.java. I'm trying to create a method in Student.java that will compare 2 students in Grades. I'm pretty new to programming so forgive my crappy comments and terminology. here's my codes:

    
    /*
     Student.java
    
     Defines a student class that stores name, score on test 1, and
     score on test 2. Methods prompt for and read in grades,
     compute the average, and return a string containing student’s info.
     */
    
    public class Student
    {
        private String name;
        private int testOne;
        private int testTwo;
    
    
        /*
         Constructs a student with a given name and default test scores.
         */
        public Student(String studentName)
        {
            name = studentName;
            testOne = 0;
            testTwo = 0;
        }
            /*
            Returns the student's name
             */
            public String getName()
            {
                return name;
            }
                /*
                 Changes the student's name
                 */
                public void setName(String newName)
                {
                    name = newName;
                }
                    /*
                     Returns the student's first score
                     */
                    public int getTestOne()
                    {
                        return testOne;
                    }
                        /*
                         Changes the student's first score
                         */
                        public void setTestOne(int newScore)
                        {
                            testOne = newScore;
                        }
                            /*
                             Returns the student's second score
                             */
                            public int getTestTwo()
                            {
                                return testTwo;
                            }
                                /*
                                 Changes the student's second score
                                 */
                                public void setTestTwo(int newScore)
                                {
                                    testTwo = newScore;
                                }
                                    /*
                                     Computes and returns the student's test average
                                     */
                                    public int getAverage()
                                    {
                                        return ((testOne + testTwo)/2);
    
                                    }
                                        /*
                                         Returns a string of the student's name, test score #1, testscore #2,
                                         e.g., Name:Joe Test1:84 Test2:100
                                         */
                                        public String toString()
                                        {
                                            return ("Name:" + name + " Test1:" + testOne + " Test2:" + testTwo);
                                        }
                                            /*
                                            Returns true if this student is the same as the other one
                                            false otherwise. It is a comparison of all instance variables.
                                             */
    /*
                                            public boolean equals(Object obj);
                                                {
                                                    if  ( obj == null || ! ( obj instanceof Student )  )
                                                    {
                                                        return false;
                                                        }
                                                        if  ( name != name )
                                                        {   // and so on
                                                        return false;
                                                    }
                                                    // ASSERT o.hashCode (  )  == hashCode (  )
                                                    return true;
                                                }
    */
    }
    
    Code (markup):

    
    // ****************************************************************
    // Grades.java
    //
    // Use Student class to get test grades for two students
    // and compute averages
    //
    // ****************************************************************
    import java.util.Scanner;
    public class Grades
    {
        public static void main(String[] args)
        {
            String name, name1, name2;
            Object test;
            int testOne, testTwo, average;
    
            Scanner scan = new Scanner (System.in);
    
    //student 1 with hardcoded scores and name
            Student student1 = new Student( "Billy" );
            name1 = student1.getName();
            student1.setTestOne(30);
            student1.setTestTwo(25);
            System.out.println(student1.toString());
            System.out.println(student1.getAverage());
    
    
    /*
    //commented out to test equal method
    //student 2 with hardcoded scores and name
                Student student2 = new Student("Mandy");
                name2 = student2.getName();
                student2.setTestOne(100);
                student2.setTestTwo(101);
                System.out.println(student2.toString());
                System.out.println(student2.getAverage());
    */
    
    
    //student 2(duplicate) with hardcoded scores and name
                Student student2 = new Student("Billy");
                name2 = student2.getName();
                student2.setTestOne(30);
                student2.setTestTwo(25);
                System.out.println(student2.toString());
                System.out.println(student2.getAverage());
    
    //compare to see if students are the same; will print either true(if same) or false
    
                    System.out.println(student1.equals(student2));
    
        }
    }
    
    Code (markup):

     
    aperez053054, Jul 23, 2010 IP
  2. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    When do they teach you that Java and JavaScript are not the same thing?
     
    Deacalion, Jul 24, 2010 IP
  3. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nope, obviously the marketing department thought it was a good idea. Loads of people make the same mistake.
    Good luck on your finals! delete the first post. (Click 'Edit Post' for the delete option)
     
    Deacalion, Jul 24, 2010 IP