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 - public class Student implements Comparable
-
{
-
private String studentYear, firstName,lastName ;
-
private int studentNumber;
-
public static final int FRESHMAN;
-
public static final int JUNIOR;
-
public static final int SENIOR;
-
public static final int SOPHOMORE;
-
-
-
public Student (String first, String last, String year, int number)
-
{
-
firstName= first;// first name of Student
-
lastName=last;//last name of Student
-
studentYear=year;// class of student ( junior, sophmore, senior,freshman)
-
studentNumber=number;// student number
-
}
-
-
public String getFirst()
-
{
-
return firstName; //returns first name
-
}
-
public String getLast()
-
{
-
return lastName; //returns last name
-
}
-
public String getYear()
-
{
-
return studentYear;//returns year
-
}
-
-
public int getNumber()
-
{
-
return studentNumber;//returns student number
-
}
-
-
public int compareTo(Object obj)
-
{
-
// i am having trouble with this part
-
// 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
-
}
-
-
public String toString()
-
{
-
String result;
-
-
result= firstName+ " " +lastName+"\n";
-
result+="Student class:\n" +studentYear+ "\n";
-
result+="Student number:\n" +studentNumber+ "\n";
-
-
return result;
-
}
-
}
-
11 3812
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.
i am still having trouble but tried what you sugested...is this right? -
public class Student implements Comparable
-
{
-
private String firstName,lastName ;
-
private int studentYear,studentNumber;
-
public static final int FRESHMAN=1;
-
public static final int JUNIOR=2;
-
public static final int SENIOR=3;
-
public static final int SOPHOMORE=4;
-
-
-
public Student (String first, String last, int year, int number)
-
{
-
firstName= first;
-
lastName=last;
-
studentYear=year;
-
studentNumber=number;
-
}
-
-
public String getFirst()
-
{
-
return firstName;
-
-
}
-
public String getLast()
-
{
-
return lastName;
-
}
-
-
public int getYear()
-
{
-
return studentYear;
-
}
-
-
public int getNumber()
-
{
-
return studentNumber;
-
}
-
-
public int compareTo(Object obj1, Object obj2)
-
{
-
Student a = (Student) obj;
-
Student b = (Student) obj;
-
if(a.getYear().compareTo(b.getYear())<0)
-
{
-
return -1;
-
}
-
else if (a.getYear().compareTo(b.getYear())>0)
-
{
-
return 1;
-
}
-
else
-
{
-
return 0;
-
}
-
}
-
-
public String toString()
-
{
-
String result;
-
-
result= firstName+ " " +lastName+"\n";
-
result+="Student class:\n" +studentYear+ "\n";
-
result+="Student number:\n" +studentNumber+ "\n";
-
-
return result;
-
}
-
-
}
-
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: - 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.
i fixed the compareTo method.... i don't know how to use the static variables in this program... -
public class Student implements Comparable
-
{
-
private String firstName,lastName, studentYear ;
-
private int yearNumber,studentNumber;
-
public static final int FRESHMAN=1;
-
public static final int JUNIOR=2;
-
public static final int SENIOR=3;
-
public static final int SOPHOMORE=4;
-
-
-
public Student (String first, String last, String year, int number)
-
{
-
firstName= first;
-
lastName=last;
-
studentYear=year;
-
studentNumber=number;
-
}
-
-
-
public String getFirst()
-
{
-
return firstName;
-
}
-
public String getLast()
-
{
-
return lastName;
-
}
-
public String getYear()
-
{
-
return studentYear;
-
-
}
-
-
public int getNumber()
-
{
-
return studentNumber;
-
}
-
-
public int compareTo(Object obj)
-
{
-
Student a = (Student) obj;
-
if(getYear().compareTo(a.getYear())<0)
-
{
-
return -1;
-
}
-
else if (getYear().compareTo(a.getYear())>0)
-
{
-
return 1;
-
}
-
else
-
{
-
return 0;
-
}
-
}
-
-
public String toString()
-
{
-
String result;
-
-
result= firstName+ " " +lastName+"\n";
-
result+="Student class:\n" +studentYear+ "\n";
-
result+="Student number:\n" +studentNumber+ "\n";
-
-
return result;
-
}
-
}
-
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: - yearNumber = Student.FRESHMAN;
Add cases for sophomore, junior, and senior.
2) Add a method:
that will return yearNumber.
3) In compareTo, don't bother with the Strings. Instead, compare yearNumber with obj.getYearNumber().
i still am having trouble this is what i did (i know parts of it are wrong) -
public class Student implements Comparable
-
{
-
private String firstName,lastName, studentYear ;
-
private int yearNumber,studentNumber;
-
String freshman, junior, senior, sophomore;
-
public static final int FRESHMAN=1;
-
public static final int JUNIOR=2;
-
public static final int SENIOR=3;
-
public static final int SOPHOMORE=4;
-
-
public Student (String first, String last, String year, int number)
-
{
-
firstName= first;
-
lastName=last;
-
studentYear=year;
-
studentNumber=number;
-
}
-
-
public String getFirst()
-
{
-
return firstName;
-
}
-
public String getLast()
-
{
-
return lastName;
-
}
-
public String getYear()
-
{
-
if(studentYear.equalsIgnoreCase(freshman))
-
{
-
yearNumber = Student.FRESHMAN;
-
}
-
else if(studentYear.equalsIgnoreCase(sophomore))
-
{
-
yearNumber = Student.SOPHOMORE;
-
}
-
else if(studentYear.equalsIgnoreCase(senior))
-
{
-
yearNumber = Student.SENIOR;
-
}
-
else
-
{
-
yearNumber = Student.JUNIOR;
-
}
-
return studentYear;
-
-
}
-
-
public int getYearNum()
-
{
-
if(studentYear.equalsIgnoreCase(senior))
-
return 4;
-
else if(studentYear.equalsIgnoreCase(junior))
-
return 3;
-
else if(studentYear.equalsIgnoreCase(freshman))
-
return 1;
-
else
-
return 2;
-
}
-
-
public int getNumber()
-
{
-
return studentNumber;
-
}
-
-
public int compareTo(Object obj)
-
{
-
Student a = (Student) obj;
-
if(yearNumber().compareTo(a.getYearNum())<0)
-
{
-
return -1;
-
}
-
else if (yearNumber().compareTo(a.getYearNum())>0)
-
{
-
return 1;
-
}
-
else
-
{
-
return 0;
-
}
-
}
-
-
public String toString()
-
{
-
String result;
-
-
result= firstName+ " " +lastName+"\n";
-
result+="Student class:\n" +studentYear+ "\n";
-
result+="Student number:\n" +studentNumber+ "\n";
-
-
return result;
-
}
-
}
-
-
public class Student implements Comparable
-
{
-
private String firstName,lastName, studentYear ;
-
private int yearNumber,studentNumber;
-
String freshman, junior, senior, sophomore;
-
public static final int FRESHMAN=1;
-
public static final int JUNIOR=2;
-
public static final int SENIOR=3;
-
public static final int SOPHOMORE=4;
-
-
public Student (String first, String last, String year, int number)
-
{
-
firstName= first;
-
lastName=last;
-
studentYear=year;
-
studentNumber=number;
-
}
-
-
public String getFirst()
-
{
-
return firstName;
-
}
-
public String getLast()
-
{
-
return lastName;
-
}
-
public String getYear()
-
{
-
if(studentYear.equalsIgnoreCase(freshman))
-
{
-
yearNumber = Student.FRESHMAN;
-
}
-
else if(studentYear.equalsIgnoreCase(sophomore))
-
{
-
yearNumber = Student.SOPHOMORE;
-
}
-
else if(studentYear.equalsIgnoreCase(senior))
-
{
-
yearNumber = Student.SENIOR;
-
}
-
else
-
{
-
yearNumber = Student.JUNIOR;
-
}
-
return studentYear;
-
-
}
-
-
public int getYearNum()
-
{
-
if(studentYear.equalsIgnoreCase(senior))
-
return 4;
-
else if(studentYear.equalsIgnoreCase(junior))
-
return 3;
-
else if(studentYear.equalsIgnoreCase(freshman))
-
return 1;
-
else
-
return 2;
-
}
-
-
public int getNumber()
-
{
-
return studentNumber;
-
}
-
-
public int compareTo(Object obj)
-
{
-
Student a = (Student) obj;
-
if(yearNumber().compareTo(a.getYearNum())<0)
-
{
-
return -1;
-
}
-
else if (yearNumber().compareTo(a.getYearNum())>0)
-
{
-
return 1;
-
}
-
else
-
{
-
return 0;
-
}
-
}
-
-
public String toString()
-
{
-
String result;
-
-
result= firstName+ " " +lastName+"\n";
-
result+="Student class:\n" +studentYear+ "\n";
-
result+="Student number:\n" +studentNumber+ "\n";
-
-
return result;
-
}
-
}
-
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.
is this better? -
public class Student implements Comparable
-
{
-
private String firstName,lastName, studentYear ;
-
private int yearNumber,studentNumber;
-
String freshman, junior, senior, sophomore;
-
public static final int FRESHMAN=1;
-
public static final int JUNIOR=2;
-
public static final int SENIOR=3;
-
public static final int SOPHOMORE=4;
-
-
public Student (String first, String last, String year, int number)
-
{
-
firstName= first;
-
lastName=last;
-
studentYear=year;
-
if(studentYear.equalsIgnoreCase(freshman))
-
{
-
yearNumber = Student.FRESHMAN;
-
}
-
else if(studentYear.equalsIgnoreCase(sophomore))
-
{
-
yearNumber = Student.SOPHOMORE;
-
}
-
else if(studentYear.equalsIgnoreCase(senior))
-
{
-
yearNumber = Student.SENIOR;
-
}
-
else
-
{
-
yearNumber = Student.JUNIOR;
-
}
-
studentNumber=number;
-
}
-
-
public String getFirst()
-
{
-
return firstName;
-
}
-
public String getLast()
-
{
-
return lastName;
-
}
-
public String getYear()
-
{
-
return studentYear;
-
}
-
-
public int getYearNum()
-
{
-
if(studentYear.equalsIgnoreCase(senior))
-
return 4;
-
else if(studentYear.equalsIgnoreCase(junior))
-
return 3;
-
else if(studentYear.equalsIgnoreCase(freshman))
-
return 1;
-
else
-
return 2;
-
}
-
-
public int getNumber()
-
{
-
return studentNumber;
-
}
-
-
public int compareTo(Object obj)
-
{
-
Student a = (Student) obj;
-
if(yearNumber<a.getYearNum())
-
{
-
return -1;
-
}
-
else if (yearNumber>a.getYearNum())
-
{
-
return 1;
-
}
-
else
-
{
-
return 0;
-
}
-
}
-
-
public String toString()
-
{
-
String result;
-
-
result= firstName+ " " +lastName+"\n";
-
result+="Student class:\n" +studentYear+ "\n";
-
result+="Student number:\n" +studentNumber+ "\n";
-
-
return result;
-
}
-
}
-
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 - 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.
like this? -
public class Student implements Comparable
-
{
-
private String firstName,lastName, studentYear ;
-
private int yearNumber,studentNumber;
-
public static final String freshman = "freshman";
-
public static final String senior = "senior";
-
public static final String junior = "junior";
-
public static final String sophomore = "sophomore";
-
public static final int FRESHMAN=1;
-
public static final int JUNIOR=2;
-
public static final int SENIOR=3;
-
public static final int SOPHOMORE=4;
-
-
public Student (String first, String last, String year, int number)
-
{
-
firstName= first;
-
lastName=last;
-
studentYear=year;
-
if(studentYear.equalsIgnoreCase(Student.freshman))
-
{
-
yearNumber = Student.FRESHMAN;
-
}
-
else if(studentYear.equalsIgnoreCase(Student.sophomore))
-
{
-
yearNumber = Student.SOPHOMORE;
-
}
-
else if(studentYear.equalsIgnoreCase(Student.senior))
-
{
-
yearNumber = Student.SENIOR;
-
}
-
else
-
{
-
yearNumber = Student.JUNIOR;
-
}
-
studentNumber=number;
-
}
-
-
public String getFirst()
-
{
-
return firstName;
-
}
-
public String getLast()
-
{
-
return lastName;
-
}
-
public String getYear()
-
{
-
return studentYear;
-
}
-
-
public int getYearNum()
-
{
-
if(studentYear.equalsIgnoreCase(senior))
-
return 4;
-
else if(studentYear.equalsIgnoreCase(junior))
-
return 3;
-
else if(studentYear.equalsIgnoreCase(freshman))
-
return 1;
-
else
-
return 2;
-
}
-
-
public int getNumber()
-
{
-
return studentNumber;
-
}
-
-
public int compareTo(Object obj)
-
{
-
Student a = (Student) obj;
-
if(yearNumber<a.getYearNum())
-
{
-
return -1;
-
}
-
else if (yearNumber>a.getYearNum())
-
{
-
return 1;
-
}
-
else
-
{
-
return 0;
-
}
-
}
-
-
public String toString()
-
{
-
String result;
-
-
result= firstName+ " " +lastName+"\n";
-
result+="Student class:\n" +studentYear+ "\n";
-
result+="Student number:\n" +studentNumber+ "\n";
-
-
return result;
-
}
-
}
-
It looks good to me. Make a short test program, then see if it works as you want it to.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics | | |
4 posts
views
Thread by n |
last post: by
|
19 posts
views
Thread by mohammaditraders |
last post: by
| | | | | | | | | | | | | | |