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){
boxcar.add(thing);
Collections.sort(boxcar);
}else{
}
}
public int getBoxcarId(){
return boxcarID;
}
public int getMaxItems(){
return maxItems;
}
public void setMaxItems(int i){
maxItems = i;
}
public void unload(T thing){
boxcar.remove(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){
if(stopped){
boxcar.setMaxItems(i);
train.add(boxcar);
}
}
public void removeCar(int i){
if(stopped){
train.remove(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';
if(stopped){
str += " Current Position: Stopped in " + position + '\n';
}else{
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