• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with arraylist, multiple classes, tostring method, and add/remove method

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need some serious help with one of my assignments. The assignment is to make a Course Roster list, be able to add and remove a student, be able to use the Comparable<person>interface, and override tostring methods and return results of another classes tostring method. A lot of this stuff isn't even covered in our book, i've been looking online for days and still need quit a bit of help tying all these classes together. I have all the requirements for each class in the code but i'll list the parts that I need help with. I understand this is a lot so any bit of help or direction with any of these issues would be greatly appreciated! Thanks so much

Person abstract class:
"Implements the comparable<person>interface. This class must override the "int compareTo(Person)"method. This should order the person objects according to the order of their last names and if their last names are the same then use the order of their first names"


Instructor class extends Person abstract class:
"Overrides the toString method from the Person, returning result of Persons toString method (super.toString()) with 2 additional instance variables appended to the end, also separated by tab characters"


Student class extends Person abstract class:
Overrides the toString method from person, returning result of it's Person toString method (super.toString()) with 2 additional instance variables appended to the end also separated by the tab characters.


Course class:
"A final List<Student>object named "roster" which represents the list of students.
A public void method named addStudent which takes a Student as a parameter and adds it to the roster. Should be sorted alphabetically by students last name then first name

A public void method named removeStudent which takes a String named personId as a parameter and removes any student from the roster who has that personId instance variable.

A public“toString” method with no parameters, returning a String which contains all of the following, each on its own line (use “\n”) in the given order:
The Course ID
The Course Name
TheCourse Code
A blank line
The String “Instructor”
The String “-------------------------“
The results of the instructor’s toString method. <---need help with this
A blank line
The String “Student Roster”
The String “-------------------------"
A separate line for each Student object in the roster showing the output of its toString method <---- need help with this "



Course App:
The teacher provided the code that I have within this class but it's telling me "Instructor cannot be converted to a string"


 
Kaitlin Barone
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my post in pastebin if it's easier to read
https://pastebin.com/VKYuVFep
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"i2" is an Instructor, Course#setInstructor( String instructor ), takes a String.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I hope the problem with <b></b> is corrected soon, because it takes so long to remove those tags. Sorry for that error.

I would query the use of Comparabe<Person> because Comparable implies that there is a natural ordering of the classes it is implemented in. I don't think there is a natural ordering for Persons. You can order theem by age name, last name, and several other ways. But you ahve been told so to do so you must bite the bullet and decide what you are going to regard as natural ordering for people. You appear to be doing it by name; if you are using Strings go through the String class first and see what sort of interfaces it already implements.You then have the task of working out such an ordering. I recommend you read the Java™ Tutorials section. You will find that Comparable is used as a mixin interface, i.e. you make an existing class implement it, and you then have to implement the solitary method from Comparable in Person. You have to consider that for all instances x and y, either x is greater than y, or y is greater than x, or x and y count as the same amount. So you end up withwhich returns a positive number if x is greater than y, a negative number y is greater than x, or 0 if x and y count as the same amount. If you can't calculate such numbers, use −1, (+)1 and 0.
Divide and rule: I suggest you implement that and get it working, and then see what your next problem is. You haven't implemented the right method, I am afraid. I think you have found Comparator instead of Comparable.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kaitlin Barone wrote:Person abstract class:
"Implements the comparable<person>interface. This class must override the "int compareTo(Person)"method. This should order the person objects according to the order of their last names and if their last names are the same then use the order of their first names"

This seems like a pretty clear statement as to what the natural order should be.

Edit: Ah, I see this is not what the OP implemented.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did notice that bit eventually.
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lol... Are you in the same course as the poster of this question?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Lol... Are you in the same course as the poster of this question?

It looks like a different question to me.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah when I read it a bit better I realized it myself, apparently a popular subject though...
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

@OP
I don’t like this code construct.

1. Comment mentions precondition student not equal to null so you could do something with it. Now your code decides to go different route by checking if it is null, and in case it is - it does nothing. THINK, how you can improve that part?

2. Parameter oldStudent. Please explain, what this “old” adds to the name? Read your comment about what method does? Does it mention something about old? It is important to have storyline in sync. If you were have 2 parameters, and both were students, then in particular context would make sense to have oldStudent, newStudent, so you could differentiate them

3. Instance variable removeStudent. Why is that name? Does it read fluently to you “removeStudent.remove...”? Personally to me doesn’t make sense. Variable names are important part of software engineering, and so not easy to get right all the time, but once you figure out that’s the case, try to spend more time on it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic