8 Replies - 393 Views - Last Post: 06 December 2012 - 09:53 PM Rate Topic: -----

#1 tingwong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-December 12

(capture#1-of ?) error?

Posted 06 December 2012 - 07:31 PM

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 String getPosition(){
		return position;
	}
	
	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 void speedUp(int i){
		speed = speed + 15;
		if(speed > maxSpeed){
			System.out.println("cant do bud");
			speed = speed - i;
		}
	}
	
	public void slowDown(int i){
		speed = speed - i;
		if(speed < minSpeed){
			System.out.println("cant do bud");
			speed = speed + 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;
	}
	
}



And finally my main code:
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 do not understand why I am getting that error message. This is my first post here so I apologize if I am doing something wrong but I would really appreciate if anyone could help me out. Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: (capture#1-of ?) error?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: (capture#1-of ?) error?

Posted 06 December 2012 - 08:33 PM

And where is your Person class ?

Also seems that you have a lot of List at many places
I can understand that a Train class has a List<Boxcar>
but I have problem to understand whow a Boxcar can have a List<T>
Was This Post Helpful? 0
  • +
  • -

#3 tingwong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-December 12

Re: (capture#1-of ?) error?

Posted 06 December 2012 - 08:40 PM

A boxcar has to be able to hold multiple Person or whatever objects so I made it a List. As for the Person class I did not include it because I thought that wasnt the problem but nonetheless here it is.

package proj5;

public class Person implements Comparable<Person>{

	private String id;
	private String name;
	private int age;
	
	public Person(String id, String name, int age){
		this.id = id;
		this.name = name;
		this.age = age;
	}
	
	public String getId(){
		return id;
	}
	
	public int getAge(){
		return age;
	}
	
	public String getName(){
		return name;
	}
	
	public String toString(){
		String str = "ID: " + id + "," + " Name: " + name + "," + " Age: " + age;
		return str;
	}
	
	public int compareTo(Person person) {
		int result = this.getId().compareTo(person.getId());
		return result;
	}
	
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: (capture#1-of ?) error?

Posted 06 December 2012 - 08:50 PM

You have too many indirection here :)/> Good try

train = new ArrayList<Boxcar<?>>();

your train ArrayList is built as a Collection of Boxcar<?> may be you should make if of <Person> rather than <?> because... when you do

train.getTrain().get(0).load(new Person(....

who tells you that the Boxcar at get(0) is a Boxcar of Person ? May be you have Boxcar<Animal> and the object in the arrayList of Boxcar at index 0 is a Boxcar<Animal> so you just can't add a Person to it

This post has been edited by pbl: 06 December 2012 - 08:55 PM

Was This Post Helpful? 0
  • +
  • -

#5 tingwong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-December 12

Re: (capture#1-of ?) error?

Posted 06 December 2012 - 08:58 PM

Ah ok. But isnt line 12 of my main file creating a Boxcar of type Person?

 train.addCar(new Boxcar<Person>(), 2);


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: (capture#1-of ?) error?

Posted 06 December 2012 - 09:09 PM

Yes... THAT Boxcar is a Boxcar of Person

But your ArrayList is an ArrayList<Boxcar<?>> not an ArrayList<Boxcar<Person>>
so maybe the:
Boxcar at index 0 is a Boxcar<String>
Boxcar at index 1 is a Boxcar<Person>
Boxcar at index 2 is a Boxcar<Integer>

so you can't

arrayList.get(0).load(new Person(... you might have to load it with String

As I said you are doing to much generic here, even me have problem to follow you... not that I say that I am an expert but ... I woldn't be the one who will have to support your code
Keep it simple is the first rule of good programming

Make a class Boxcar that has an ArrayList<Something>
and have Person, Cow, Pid to extend Something

And if a Person has to sleep with a Cow, give shit to the programmer that put a Person in a Boxcar that contains Cow... not to Java generic
Was This Post Helpful? 0
  • +
  • -

#7 tingwong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-December 12

Re: (capture#1-of ?) error?

Posted 06 December 2012 - 09:25 PM

Haha alright so it would be just to have a superclass for everything and to use that type as parameter?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: (capture#1-of ?) error?

Posted 06 December 2012 - 09:35 PM

You can have Boxcar<Object> to :)

I would rather have different Boxcar classes (BoxcarPerson, BoxcarCow, BoxcarCocain, ...) extending the abtsract class Boxcar the ony difference being the type of the ArrayList
Was This Post Helpful? 0
  • +
  • -

#9 tingwong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-December 12

Re: (capture#1-of ?) error?

Posted 06 December 2012 - 09:53 PM

Hahaha BoxcarCocain :P. But thing is the project prompt says that we have to keep Boxcar generic. I will experiment with a couple things. Thanks for the help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1