473,569 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with Student class

15 New Member
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 4041
Ganon11
3,652 Recognized Expert Specialist
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
xxbabysue123xx
15 New Member
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 Recognized Expert Specialist
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
xxbabysue123xx
15 New Member
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 Recognized Expert Specialist
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 .equalsIgnoreCa se() 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.getYearNumb er().
Mar 20 '07 #6
xxbabysue123xx
15 New Member
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 Recognized Expert Specialist
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=ye ar;" 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
xxbabysue123xx
15 New Member
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 Recognized Expert Specialist
Beautiful! Only one thing should be changed:

Your tests in the constructor use the Strings freshman, sophomore, junior, and senior (in the .equalsIgnoreCa se() 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 .equalsIgnoreCa se, use Student.freshma n 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

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

Similar topics

1
2744
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) Problem 1: Create a Student Class (worth all 100 points) This problem will require you to create a Student class and a C++ main function to test it. The...
2
9684
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 constructor such that each test score is assumed to initially be zero. Provide a method called setTestScore that accepts two parameters: the test number (1...
3
1874
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, so please help me out. I only did one little bit of it, and im 100% sure that its wrong......please bear with me :( Here's how it goes: public...
4
2013
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, a random number of students arrive, each of which has completed a random number of lessons taken at random from the curriculum.
19
1943
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 percentage of the student by the formula (Ob_marks * 100 ) / Total_marks and Display() which show all information of the student. The class should also...
8
1955
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 realtively new at programming.The topic is "Using a database and SQL". The exercise supplies a table called tblStudent in a database called School and requires...
3
2386
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 class: (one I added all the asteriss to. // If we are at the end of input then NoSuchElement; // If there is still input left then...
4
2708
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 Dayton, Ohio 775665 M 2 25 2.5 ENGR
3
1747
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 ******************************************************************************* Welcome to PeopleSoft 2 MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it a Enter the student number...
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.