Hi everyone, I have a project that creates a Train that pulls Boxcars. The boxcar is supposed to be generic type and have attributes such as load that adds only a specific type of object to the boxcar. The train is supposed to pull all boxcars created. I have written the following code but I get this error The method load(capture#1-of ?) in the type Boxcar<capture#1-of ?> is not applicable for the arguments (Person)

package proj5;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class Boxcar<T extends Comparable<T>> {

    private List<T> boxcar;
    private int maxItems;
    private int boxcarID;

    public Boxcar(){
        boxcar = new ArrayList<T>();

    public void load(T thing){
        if(!boxcar.contains(thing) && boxcar.size() < maxItems){


    public int getBoxcarId(){
        return boxcarID;

    public int getMaxItems(){
        return maxItems;

    public void setMaxItems(int i){
        maxItems = i;

    public void unload(T thing){

    public List<T> getBoxcar(){
        return boxcar;


package proj5;

import java.util.ArrayList;
import java.util.List;

public class Train {

    private List<Boxcar<?>> train;
    private int maxSpeed;
    private int minSpeed;
    private String position;
    private int numBoxcars;
    private int maxNumBoxcars;
    private int speed;
    private String destination;
    private boolean stopped = true;

    public Train(int maxSpeed, int minSpeed, int maxNumBoxcars, String position){
        train = new ArrayList<Boxcar<?>>();
        this.maxSpeed = maxSpeed;
        this.minSpeed = minSpeed;
        this.maxNumBoxcars = maxNumBoxcars;
        this.position = position;

    public int getMaxNumBoxcars(){
        return maxNumBoxcars;

    public int getSpeed(){
        return speed;

    public String getPosition(){
        return position;

    public int getMaxSpeed(){
        return maxSpeed;

    public int getNumBoxcars(){
        return numBoxcars;

    public List<Boxcar<?>> getTrain(){
        return train;

    public void depart(String destination){
        this.destination = destination;
        speed = minSpeed;
        stopped = false;

    public void arrive(){
        stopped = true;
        position = destination;

    public void addCar(Boxcar<?> boxcar, int i){

    public void removeCar(int i){

    public String toString(){
        String str = "";
        str += "Train Status" + '\n';
        str += "----------------" + '\n';
        str += "   Current Speed: " + speed + '\n';
        str += "   Minimum Speed: " + minSpeed + '\n';
        str += "   Maximum Speed: " + maxSpeed + '\n';
            str += "   Current Position:     Stopped in " + position + '\n';
            str += "   Current Position:     Traveling from " + position + " to " + destination; 
        str += "   Current Number of Boxcars: " + numBoxcars;
        str += "   Maximum Number of Boxcars: " + maxNumBoxcars;
        return str;


My main class where I try to add boxcars and load to the boxcars

package proj5;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Project5 {

    public static void main(String[] args) {
        Train train = new Train(50, 10, 4, "Cleveland");
        train.addCar(new Boxcar<Person>(), 2);
        train.getTrain().get(0).load(new Person("1234", "Joe", 21));


Am I declaring the Train list wrong? Am I not supposed to use the wildcard in boxcar in the Train List?
I apologize if all this is too long but I would really appreciate if anyone could help me out. Thank you

4 Years
Discussion Span
Last Post by JamesCherrill

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


And here is the output
Boxcar [boxcar=[Person [age=21, name=Joe, id=1234]], maxItems=2, boxcarID=0]

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.