At line 12 in class proj5 -->
train.addCar(new Boxcar<Person>(), 2); //this is wrong
Just look at the constructor of Boxcar.
So modify the this line to
train.addCar(new Boxcar(), 2);// this is code asper your constructor declarationin Boxcar
Please make sure your Person class implements Comparable interface, if not this code at line 13 will not work
train.getTrain().get(0).load(new Person("1234", "Joe", 21));// make sure the Person class implents the Comparable interface.
Finally the above code can be simplified as
//**** the existing line 13 needs to be commented and modified like below********
List list=train.getTrain();
Boxcar boxcar=(Boxcar)list.get(0);
boxcar.load(new Person("1234", "Joe", 21));
Note:Please make sure your Person class implements the comparable interface , other wise this code will not compile
I have added a toString() method in Boxcar
I am also able to print the boxcar object at the end
like this
System.out.println(boxcar);
And here is the output
Boxcar [boxcar=[Person [age=21, name=Joe, id=1234]], maxItems=2, boxcarID=0]
subramanya.vl
Junior Poster in Training
81 posts since Oct 2012
Reputation Points: 0
Solved Threads: 10
Skill Endorsements: 0
@subramanya:
This is an exercise in the use of generics. All you have done is to remove the generics from his code.
JamesCherrill
... trying to help
8,494 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
Question Answered as of 4 Months Ago by
JamesCherrill
and
subramanya.vl