Hello everyone. I have to write a program that needs to use generics. I'm basically just hoping someone can guide me in the right direction here. Lets say I need a program that loads different crates onto a boat. I have to have a boat class (generic class), then other classes for each type of crate.
To keep this simple lets just say I have the boat generic class, and a small crate class (would be numerous crate classes). The ultimate goal is to load the crate onto the boat and ship it off, I would have to print to the screen the initial status of the boat (no crate loaded yet), then after the crate is loaded I have to print the status of the boat again, this time with crate details (size, weight, whats inside). Would the small crate class simply contain a constructor for the attributes and maybe an accessor here and there?
Now the big one, this is typed up off the top of my head, so excuse any minor mistakes. But how exactly would I go about applying generics to the boat class? Is something like this remotely close?:
public class Boat<Crate>
private String destination;
private int height;
private int weight;
private Crate data;
public Boat(){
//basic constructor with no params
}
public Boat(String destination, int height, int weight){
//basic constructor setting data
}
public void loadCrate(Crate newData){
data = newData;
}
// basic mutators for destination, height, weight, etc...
Is this remotely correct? I have a Small Crate class, Medium and Large, all with almost identical instance variables but each one with a unique one. Would the loadCrate method work? Am I going about this the wrong way? The program itself is more complex but I figured a basic example like this would make things easier as far as explanations go. Thanks alot guys, appreciate it!