473,324 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

help with Student class

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
11 4018
Ganon11
3,652 Expert 2GB
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
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
3,652 Expert 2GB
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
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
3,652 Expert 2GB
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
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
3,652 Expert 2GB
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
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
3,652 Expert 2GB
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
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
3,652 Expert 2GB
It looks good to me. Make a short test program, then see if it works as you want it to.
Mar 24 '07 #12

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

Similar topics

1
by: po3gurl | last post by:
Hello to all is there anyone who could help me figure out why my program will not compile. This is my assignment but I have also included my work and I am getting a error that says (C1083) ...
2
by: sallyk07 | last post by:
Modify the Student class so that each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the...
3
by: outofmymind | last post by:
Hi, i've been practicing again......my exam is tomorrow :( and this is another practice question that my instructor told us to practice on, its not that easy, it was from a past final exam paper,...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
19
by: mohammaditraders | last post by:
a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions Cal_percentage() which calculate the...
8
by: 08butoryr | last post by:
Hey guys I could really use your help with some very basic java programming. I know you programming fundis out there will find this child's play but I'm struggling with it a bit because I'm...
3
by: Dameon99 | last post by:
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the...
4
by: indiaj0nes | last post by:
Hi, I am Supposed to be reading from a student file and store it in array of pointers to student Objects(Objects of Student class). The file contains data like: 102022 Jonathan James L...
3
by: BlueroY | last post by:
hi, I'm working on an exercise, i did a lot of work already and i just can't figure where I'm going wrong, this is what I'm trying to achieve Sample IO...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.