• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble implementing add() method to immutable class  RSS feed

 
Kevin Bridge
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, first post here. Thanks in advance! I'm not going to lie, this is for a class. I do not need the answer spelled out or anything, if you could just point me in the right direction, I would be very greatful. Although I have done a fair amount of programming, I have a somewhat limited knowledge of Java since the class I am in simply expects you to know Java from the first day.
Class RoadSegment is immutable. Class Route is also immutable. The underlying representation of Route is an ArrayList called segments, containing RoadSegments. Route has a single constructor, Route(RoadSegment segment). I need to implement Route.addSegment(RoadSegment seg), which returns a new Route object with seg added to the end of the Route. I am having difficulty here because the constructor only allows one segment. If I call addSegment() within addSegment, I will just get an endless loop. I am only allowed to create private methods. I tried creating a private constructor, but could not get it to correctly add another RoadSegment. Thanks for the help everyone!
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kevin,
It seems u r dealing a program related to LinkedLists! Are you? State the question clearly with code u have and point where exactly u want the guidance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!