• 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

List<Object> question?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Can you post Person class?
Does it have a method like setNumber?


It does not =[
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider another class with Person and PhoneNumber fields.
 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 8857
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
Can you have overloaded constructor?
 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 8857
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
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
Marshal
Posts: 8857
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
Hm.. Is it assignment or something? If yes, could you show us please the part where those requirements are specified.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like either the assignment is whacked or you are not understanding it correctly.
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic