• Post Reply Bookmark Topic Watch Topic
  • New Topic

List<Object> question?  RSS feed

 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am supposed to read in a multiple line file that contains a names, the persons phone numbers(John,418-1324) into a List<Person>. however the Person only has the constructor with name and i cannot modify this.
i am only allowed to use list. is there a way to separate the name and number so that i can call it separately later? ty
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post Person class?
Does it have a method like setNumber?
 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Can you post Person class?
Does it have a method like setNumber?

It does not =[
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider another class with Person and PhoneNumber fields.
 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Consider another class with Person and PhoneNumber fields.


there's no other way to do it without creating another constructor? i dont think im suppose to modify it
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you have overloaded constructor?
 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Can you have overloaded constructor?

i'm not allowed to modify anything in that class
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, in different words, you have what you have, and you cannot touch it. So, the answer is no, as this class cannot define any additional functionality.
Likely you have to follow Campbell's advice and create another class then, as you cannot modify current Person class.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm.. Is it assignment or something? If yes, could you show us please the part where those requirements are specified.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like either the assignment is whacked or you are not understanding it correctly.
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have to use a List? If you are stuck using that Person class and have to read in names and phone numbers, you could try a Map.

I'll echo the other commenters, though: what exactly is the assignment?
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Lonsky wrote:i'm not allowed to modify anything in that class

Are you allowed to extend the Person class to a PersonWithNumber class?
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Lonsky wrote:. . . i'm not allowed to modify anything in that class
My suggestion does not require any modification to the person class.
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I take back my previous suggestion to use a Map. I like Campbell's idea much better: create a new class that composes the existing Person class and a phone number, and store objects of that type in your List.

The "right" solution to your problem really does depend on what your instructor hopes to teach you with this assignment, though.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Bullers wrote:. . . I like Campbell's idea much better . . .
Thank you. It comes under the heading of “favour composition over inheritance”; you simply need a field for the Person and one for the phone number reference. For getXXX methods (or even setXXX) sue the enclosing class as a delegate. Not setPerson but setLastName. People do after all change their last name from time to time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!