By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,609 Members | 3,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,609 IT Pros & Developers. It's quick & easy.

help with Student class

P: 15
Heres the problem:

Create a class Student with instance data name, studentNumber, class (where class is a String containing one of the following: “Freshman”, “Sophomore”, “Junior”, “Senior”.
Make the class implement the Comparable interface. Include a toString method.
Write a driver program to demonstrate your work.
Instantiate several objects of the Student class.
Call your methods several times to show that class is ordered.

Heres what i have done so far: (i am having trouble writing the compareTo method) Thanks for any help

Expand|Select|Wrap|Line Numbers
  1.  public class Student implements Comparable 
  2. {
  3. private String studentYear, firstName,lastName ;
  4. private int studentNumber;
  5. public static final int FRESHMAN;
  6. public static final int JUNIOR;
  7. public static final int SENIOR;
  8. public static final int SOPHOMORE;
  9.  
  10.  
  11. public Student (String first, String last, String year, int number)
  12. {
  13. firstName= first;// first name of Student
  14. lastName=last;//last name of Student
  15. studentYear=year;// class of student ( junior, sophmore, senior,freshman)
  16. studentNumber=number;// student number
  17. }
  18.  
  19. public String getFirst()
  20. {
  21. return firstName; //returns first name
  22. }
  23. public String getLast()
  24. {
  25. return lastName; //returns last name
  26. }
  27. public String getYear()
  28. {
  29. return studentYear;//returns year
  30. }
  31.  
  32. public int getNumber()
  33. {
  34. return studentNumber;//returns student number
  35. }
  36.  
  37. public int compareTo(Object obj)
  38. {
  39. // i am having trouble with this part
  40. // i have to say that freshman comes before sophmore , sophmore comes after freshman but before junior and senior, junior comes after sophmore and before senior... and so on
  41. }
  42.  
  43. public String toString()
  44. {
  45. String result;
  46.  
  47. result= firstName+ " " +lastName+"\n";
  48. result+="Student class:\n" +studentYear+ "\n";
  49. result+="Student number:\n" +studentNumber+ "\n";
  50.  
  51. return result;
  52. }
  53. }
  54.  
Mar 19 '07 #1
Share this Question
Share on Google+
11 Replies


Ganon11
Expert 2.5K+
P: 3,652
1) Please use the [code] tags around your code to make it easier to read.
2) You declare 4 static integers, but don't assign them any value - maybe this is where you should start?
3) Remember that the compareTo method will return -1 if the current object is less than the given object, 1 if the current object is greater than the given object, and 0 if they are equal. So if two students are in the same year, return 0. Otherwise, you will have to perform some additional checks.

Alternatively, you could add an instance variable indicating what year they are in - in the constructor, if the String given is Freshman, then the Student is in his/her 1st year, and the new variable would hold 1. A sophomore would have 2, a junior 3, and a senior 4. Then you can just compare this integer rather than trying to compare Strings.
Mar 19 '07 #2

P: 15
i am still having trouble but tried what you sugested...is this right?


Expand|Select|Wrap|Line Numbers
  1. public class Student implements Comparable
  2.    {
  3.       private String  firstName,lastName ;
  4.       private int studentYear,studentNumber;
  5.       public static final int FRESHMAN=1;
  6.       public static final int JUNIOR=2;
  7.       public static final int SENIOR=3;
  8.       public static final int SOPHOMORE=4;
  9.  
  10.  
  11. public Student (String first, String last, int year, int number)
  12.       {
  13.          firstName= first;
  14.          lastName=last;
  15.          studentYear=year;
  16.          studentNumber=number;
  17.       }
  18.  
  19.        public String getFirst()
  20.       {
  21.          return firstName;
  22.  
  23.       }
  24.        public String getLast()
  25.       {
  26.          return lastName;
  27.       }
  28.  
  29.        public int getYear()
  30.       {
  31.          return studentYear;
  32.       }
  33.  
  34.        public int getNumber()
  35.       {
  36.          return studentNumber;
  37.       }
  38.  
  39.        public int compareTo(Object obj1, Object obj2)
  40.       {
  41.          Student a = (Student) obj;
  42.          Student b = (Student) obj;
  43.          if(a.getYear().compareTo(b.getYear())<0)
  44.          {
  45.             return -1;
  46.          }
  47.          else if (a.getYear().compareTo(b.getYear())>0)
  48.          {
  49.             return 1;
  50.          }
  51.          else 
  52.          {
  53.             return 0;
  54.          }
  55.       }
  56.  
  57.        public String toString()
  58.       {
  59.          String result;
  60.  
  61.          result= firstName+ " " +lastName+"\n";
  62.          result+="Student class:\n" +studentYear+ "\n";
  63.          result+="Student number:\n" +studentNumber+ "\n";
  64.  
  65.          return result;
  66.       }
  67.  
  68.    }
  69.  
Mar 20 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
1) The static variables look good, but now I must ask if you ever use them?
2) Your problem specification states that studentYear must be a String. This means you have to keep it as a String, not as an int. My suggestion was to add an additional variable - since you have a String studentYear, how about an int yearNumber?

3) The compareTo object takes only 1 object argument. The signature is:

Expand|Select|Wrap|Line Numbers
  1. public int compareTo(Object obj)
You will be comparing the year of obj to the year within the object (in other words, compare studentYear with obj.getYear()). You should not use the compareTo method of Strings! This states that senior comes before sophomore, and junior comes before senior, since Strings are compared alphabetically. The point is to make your own compareTo method, not rely on predefined compareTo methods.
Mar 20 '07 #4

P: 15
i fixed the compareTo method.... i don't know how to use the static variables in this program...
Expand|Select|Wrap|Line Numbers
  1.  public class Student implements Comparable
  2.    {
  3.       private String  firstName,lastName, studentYear ;
  4.       private int yearNumber,studentNumber;
  5.       public static final int FRESHMAN=1;
  6.       public static final int JUNIOR=2;
  7.       public static final int SENIOR=3;
  8.       public static final int SOPHOMORE=4;
  9.  
  10.  
  11.        public Student (String first, String last, String year, int number)
  12.       {
  13.          firstName= first;
  14.          lastName=last;
  15.          studentYear=year;
  16.          studentNumber=number;
  17.       }
  18.  
  19.  
  20.        public String getFirst()
  21.       {
  22.          return firstName;
  23.       }
  24.        public String getLast()
  25.       {
  26.          return lastName;
  27.       }
  28.        public String getYear()
  29.       {
  30.          return studentYear;
  31.  
  32.       }
  33.  
  34.        public int getNumber()
  35.       {
  36.          return studentNumber;
  37.       }
  38.  
  39.        public int compareTo(Object obj)
  40.       {
  41.          Student a = (Student) obj;
  42.          if(getYear().compareTo(a.getYear())<0)
  43.          {
  44.             return -1;
  45.          }
  46.          else if (getYear().compareTo(a.getYear())>0)
  47.          {
  48.             return 1;
  49.          }
  50.          else 
  51.          {
  52.             return 0;
  53.          }
  54.       }
  55.  
  56.        public String toString()
  57.       {
  58.          String result;
  59.  
  60.          result= firstName+ " " +lastName+"\n";
  61.          result+="Student class:\n" +studentYear+ "\n";
  62.          result+="Student number:\n" +studentNumber+ "\n";
  63.  
  64.          return result;
  65.       }
  66.    }
  67.  
Mar 20 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
Your compareTo method will still not work as expected.

Freshman is less than sophomore, which is less than junior, which is less than senior, correct?

But the compareTo method used in Strings (which you are comparing with getYear()) compares strings alphabetically, meaning the order will be freshmen, junior, senior, sophomore.

You will not use the compareTo method of the String class - you must write your own. To do that, try the following steps:

1) In your constructor:

You shouldn't have the int parameter. Instead, once you have the studentYear String, use the String .equalsIgnoreCase() to determine if freshmen, sophomore, junior, or senior was given - if it was freshmen, assign the FRESHMAN value to yearNumber, like this:

Expand|Select|Wrap|Line Numbers
  1. yearNumber = Student.FRESHMAN;
Add cases for sophomore, junior, and senior.

2) Add a method:

Expand|Select|Wrap|Line Numbers
  1. public int getYearNum()
that will return yearNumber.

3) In compareTo, don't bother with the Strings. Instead, compare yearNumber with obj.getYearNumber().
Mar 20 '07 #6

P: 15
i still am having trouble this is what i did (i know parts of it are wrong)

Expand|Select|Wrap|Line Numbers
  1.   public class Student implements Comparable
  2.    {
  3.       private String  firstName,lastName, studentYear ;
  4.       private int yearNumber,studentNumber;
  5.       String freshman, junior, senior, sophomore;
  6.       public static final int FRESHMAN=1;
  7.       public static final int JUNIOR=2;
  8.       public static final int SENIOR=3;
  9.       public static final int SOPHOMORE=4;
  10.  
  11.        public Student (String first, String last, String year, int number)
  12.       {
  13.          firstName= first;
  14.          lastName=last;
  15.          studentYear=year;
  16.          studentNumber=number;
  17.       }
  18.  
  19.        public String getFirst()
  20.       {
  21.          return firstName;
  22.       }
  23.        public String getLast()
  24.       {
  25.          return lastName;
  26.       }
  27.        public String getYear()
  28.       {
  29.          if(studentYear.equalsIgnoreCase(freshman))
  30.          {
  31.             yearNumber = Student.FRESHMAN;
  32.          }
  33.          else if(studentYear.equalsIgnoreCase(sophomore))
  34.          {
  35.             yearNumber = Student.SOPHOMORE;
  36.          }
  37.          else if(studentYear.equalsIgnoreCase(senior))
  38.          {
  39.             yearNumber = Student.SENIOR;
  40.          }
  41.          else 
  42.          {
  43.             yearNumber = Student.JUNIOR;
  44.          }
  45.          return studentYear;
  46.  
  47.       }
  48.  
  49.        public int getYearNum()
  50.       {
  51.          if(studentYear.equalsIgnoreCase(senior))
  52.             return 4;
  53.          else if(studentYear.equalsIgnoreCase(junior))
  54.             return 3;
  55.          else if(studentYear.equalsIgnoreCase(freshman))
  56.             return 1;
  57.          else
  58.             return 2;
  59.       }
  60.  
  61.        public int getNumber()
  62.       {
  63.          return studentNumber;
  64.       }
  65.  
  66.        public int compareTo(Object obj)
  67.       {
  68.        Student a = (Student) obj; 
  69.          if(yearNumber().compareTo(a.getYearNum())<0)
  70.          {
  71.             return -1;
  72.          }
  73.          else if (yearNumber().compareTo(a.getYearNum())>0)
  74.          {
  75.             return 1;
  76.          }
  77.          else 
  78.          {
  79.             return 0;
  80.          }
  81.       }
  82.  
  83.        public String toString()
  84.       {
  85.          String result;
  86.  
  87.          result= firstName+ " " +lastName+"\n";
  88.          result+="Student class:\n" +studentYear+ "\n";
  89.          result+="Student number:\n" +studentNumber+ "\n";
  90.  
  91.          return result;
  92.       }
  93.    }
  94.  
Mar 20 '07 #7

Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1.   public class Student implements Comparable
  2.    {
  3.       private String  firstName,lastName, studentYear ;
  4.       private int yearNumber,studentNumber;
  5.       String freshman, junior, senior, sophomore;
  6.       public static final int FRESHMAN=1;
  7.       public static final int JUNIOR=2;
  8.       public static final int SENIOR=3;
  9.       public static final int SOPHOMORE=4;
  10.  
  11.        public Student (String first, String last, String year, int number)
  12.       {
  13.          firstName= first;
  14.          lastName=last;
  15.          studentYear=year;
  16.          studentNumber=number;
  17.       }
  18.  
  19.        public String getFirst()
  20.       {
  21.          return firstName;
  22.       }
  23.        public String getLast()
  24.       {
  25.          return lastName;
  26.       }
  27.        public String getYear()
  28.       {
  29.          if(studentYear.equalsIgnoreCase(freshman))
  30.          {
  31.             yearNumber = Student.FRESHMAN;
  32.          }
  33.          else if(studentYear.equalsIgnoreCase(sophomore))
  34.          {
  35.             yearNumber = Student.SOPHOMORE;
  36.          }
  37.          else if(studentYear.equalsIgnoreCase(senior))
  38.          {
  39.             yearNumber = Student.SENIOR;
  40.          }
  41.          else 
  42.          {
  43.             yearNumber = Student.JUNIOR;
  44.          }
  45.          return studentYear;
  46.  
  47.       }
  48.  
  49.        public int getYearNum()
  50.       {
  51.          if(studentYear.equalsIgnoreCase(senior))
  52.             return 4;
  53.          else if(studentYear.equalsIgnoreCase(junior))
  54.             return 3;
  55.          else if(studentYear.equalsIgnoreCase(freshman))
  56.             return 1;
  57.          else
  58.             return 2;
  59.       }
  60.  
  61.        public int getNumber()
  62.       {
  63.          return studentNumber;
  64.       }
  65.  
  66.        public int compareTo(Object obj)
  67.       {
  68.        Student a = (Student) obj; 
  69.          if(yearNumber().compareTo(a.getYearNum())<0)
  70.          {
  71.             return -1;
  72.          }
  73.          else if (yearNumber().compareTo(a.getYearNum())>0)
  74.          {
  75.             return 1;
  76.          }
  77.          else 
  78.          {
  79.             return 0;
  80.          }
  81.       }
  82.  
  83.        public String toString()
  84.       {
  85.          String result;
  86.  
  87.          result= firstName+ " " +lastName+"\n";
  88.          result+="Student class:\n" +studentYear+ "\n";
  89.          result+="Student number:\n" +studentNumber+ "\n";
  90.  
  91.          return result;
  92.       }
  93.    }
  94.  
Things are starting to look better here...

1) You are assigning values to yearNumber within the getYear() function - this task should be done in the constructor, right after the statement "studentYear=year;" so that the yearNumber variable will be set as soon as the object is created.

2) In getYearNum(), you need only return yearNum instead of checking once again which year the Student is. You can assume that, since the tests are in the constructor, the value of yearNumber has been properly set.

3) In the compareTo() method, you are still using the compareTo method of other objects. This will not work, since yearNumber and a.getYearNum() are both int data types - since these are not objects, they cannot have methods. You should use < or > to compare these two values, not compareTo.

4) In compareTo(), you write yearNumber() instead of yearNumber. Your compiler will be looking for a yearNumber() method rather than the instance variable.
Mar 21 '07 #8

P: 15
is this better?

Expand|Select|Wrap|Line Numbers
  1.  public class Student implements Comparable
  2.    {
  3.       private String  firstName,lastName, studentYear ;
  4.       private int yearNumber,studentNumber;
  5.       String freshman, junior, senior, sophomore;
  6.       public static final int FRESHMAN=1;
  7.       public static final int JUNIOR=2;
  8.       public static final int SENIOR=3;
  9.       public static final int SOPHOMORE=4;
  10.  
  11.        public Student (String first, String last, String year, int number)
  12.       {
  13.          firstName= first;
  14.          lastName=last;
  15.          studentYear=year;
  16.          if(studentYear.equalsIgnoreCase(freshman))
  17.          {
  18.             yearNumber = Student.FRESHMAN;
  19.          }
  20.          else if(studentYear.equalsIgnoreCase(sophomore))
  21.          {
  22.             yearNumber = Student.SOPHOMORE;
  23.          }
  24.          else if(studentYear.equalsIgnoreCase(senior))
  25.          {
  26.             yearNumber = Student.SENIOR;
  27.          }
  28.          else 
  29.          {
  30.             yearNumber = Student.JUNIOR;
  31.          }
  32.       studentNumber=number;
  33.       }
  34.  
  35.        public String getFirst()
  36.       {
  37.          return firstName;
  38.       }
  39.        public String getLast()
  40.       {
  41.          return lastName;
  42.       }
  43.        public String getYear()
  44.       {
  45.          return studentYear;
  46.       }
  47.  
  48.        public int getYearNum()
  49.       {
  50.          if(studentYear.equalsIgnoreCase(senior))
  51.             return 4;
  52.          else if(studentYear.equalsIgnoreCase(junior))
  53.             return 3;
  54.          else if(studentYear.equalsIgnoreCase(freshman))
  55.             return 1;
  56.          else
  57.             return 2;
  58.       }
  59.  
  60.        public int getNumber()
  61.       {
  62.          return studentNumber;
  63.       }
  64.  
  65.        public int compareTo(Object obj)
  66.       {
  67.          Student a = (Student) obj; 
  68.          if(yearNumber<a.getYearNum())
  69.          {
  70.             return -1;
  71.          }
  72.          else if (yearNumber>a.getYearNum())
  73.          {
  74.             return 1;
  75.          }
  76.          else 
  77.          {
  78.             return 0;
  79.          }
  80.       }
  81.  
  82.        public String toString()
  83.       {
  84.          String result;
  85.  
  86.          result= firstName+ " " +lastName+"\n";
  87.          result+="Student class:\n" +studentYear+ "\n";
  88.          result+="Student number:\n" +studentNumber+ "\n";
  89.  
  90.          return result;
  91.       }
  92.    }
  93.  
Mar 22 '07 #9

Ganon11
Expert 2.5K+
P: 3,652
Beautiful! Only one thing should be changed:

Your tests in the constructor use the Strings freshman, sophomore, junior, and senior (in the .equalsIgnoreCase() method calls), but these are still undefined. The best solution would be to make these into static final variables like the FRESHMAN, SOPHOMORE, etc. int variables. You can say

Expand|Select|Wrap|Line Numbers
  1. public static final String freshman = "freshman";
and then in the .equalsIgnoreCase, use Student.freshman rather than freshman.

Other than this, the code should work - make the change, compile, and see if your program is functioning properly.
Mar 22 '07 #10

P: 15
like this?

Expand|Select|Wrap|Line Numbers
  1.   public class Student implements Comparable
  2.    {
  3.       private String  firstName,lastName, studentYear ;
  4.       private int yearNumber,studentNumber;
  5.       public static final String freshman = "freshman";
  6.       public static final String senior = "senior";
  7.       public static final String junior = "junior";
  8.       public static final String sophomore = "sophomore";
  9.       public static final int FRESHMAN=1;
  10.       public static final int JUNIOR=2;
  11.       public static final int SENIOR=3;
  12.       public static final int SOPHOMORE=4;
  13.  
  14.        public Student (String first, String last, String year, int number)
  15.       {
  16.          firstName= first;
  17.          lastName=last;
  18.          studentYear=year;
  19.          if(studentYear.equalsIgnoreCase(Student.freshman))
  20.          {
  21.             yearNumber = Student.FRESHMAN;
  22.          }
  23.          else if(studentYear.equalsIgnoreCase(Student.sophomore))
  24.          {
  25.             yearNumber = Student.SOPHOMORE;
  26.          }
  27.          else if(studentYear.equalsIgnoreCase(Student.senior))
  28.          {
  29.             yearNumber = Student.SENIOR;
  30.          }
  31.          else 
  32.          {
  33.             yearNumber = Student.JUNIOR;
  34.          }
  35.          studentNumber=number;
  36.       }
  37.  
  38.        public String getFirst()
  39.       {
  40.          return firstName;
  41.       }
  42.        public String getLast()
  43.       {
  44.          return lastName;
  45.       }
  46.        public String getYear()
  47.       {
  48.          return studentYear;
  49.       }
  50.  
  51.        public int getYearNum()
  52.       {
  53.          if(studentYear.equalsIgnoreCase(senior))
  54.             return 4;
  55.          else if(studentYear.equalsIgnoreCase(junior))
  56.             return 3;
  57.          else if(studentYear.equalsIgnoreCase(freshman))
  58.             return 1;
  59.          else
  60.             return 2;
  61.       }
  62.  
  63.        public int getNumber()
  64.       {
  65.          return studentNumber;
  66.       }
  67.  
  68.        public int compareTo(Object obj)
  69.       {
  70.          Student a = (Student) obj; 
  71.          if(yearNumber<a.getYearNum())
  72.          {
  73.             return -1;
  74.          }
  75.          else if (yearNumber>a.getYearNum())
  76.          {
  77.             return 1;
  78.          }
  79.          else 
  80.          {
  81.             return 0;
  82.          }
  83.       }
  84.  
  85.        public String toString()
  86.       {
  87.          String result;
  88.  
  89.          result= firstName+ " " +lastName+"\n";
  90.          result+="Student class:\n" +studentYear+ "\n";
  91.          result+="Student number:\n" +studentNumber+ "\n";
  92.  
  93.          return result;
  94.       }
  95.    }
  96.  
Mar 22 '07 #11

Ganon11
Expert 2.5K+
P: 3,652
It looks good to me. Make a short test program, then see if it works as you want it to.
Mar 24 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.