1.11M Members

Inheritance homework problem

 
0
 

I am doing a homework problem and am having a hard time understanding exactly how I should be doing this. Any nudge in the right direction would be helpful. :)

The problem is as follows:

Ship, CruiseShip, and CargoShip Classes Design a Ship class that the following members:
• A field for the name of the ship (a string).
• A field for the year that the ship was built (a string).
• A constructor and appropriate accessors and mutators.
• A toString method that displays the ship’s name and the year it was built.

Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members:
• A field for the maximum number of passengers (an int).
• A constructor and appropriate accessors and mutators.
• A toString method that overrides the toString method in the base class.

The CruiseShip class’s toString method should display only the ship’s name and the maximum number of passengers.

Design a CargoShip class that extends the Ship class. The CargoShip class should have the following members:
• A field for the cargo capacity in tonnage (an int).
• A constructor and appropriate accessors and mutators.
• A toString method that overrides the toString method in the base class.

The CargoShip class’s toString method should display only the ship’s name and the ship’s cargo capacity.

Demonstrate the classes in a program that has a Shiparray. Assign various Ship, CruiseShip, and CargoShip objects to the array elements. The program should then step through the array, calling each object’s toString method.

This is what I have for the Ship Class so far:

public class Ship {

	private String name;
	private String builtDate;
	
	public Ship(String n, String date)
	{
		name = n;
		builtDate = date;
	}
	
	public Ship()
	{
		name = "";
		builtDate = "";
	}
	
	public void setName(String n)
	{
		name = n;
	}
	
	public void setBuiltDate(String b)
	{
		builtDate = b;
	}
	
	public String getName()
	{
		return name;
	}
	
	public String getBuiltDate()
	{
		return builtDate;
	}
	
	public String toString()
	{
		String str = "Name: " + name;
		
		str += ("\nBuiltDate: " + builtDate);
		return str;
	}
}

Here is what I have for the CruiseShip Class so far:

public class CruiseShip extends Ship
{

	private int maxPass;
	
	public CruiseShip(String n, String b)
	{
		super(n, b);
	}
	
	public void setMaxPass(int mp)
	{
		maxPass = mp;
	}
	
	public int getMaxPass()
	{
		return maxPass;
	}
	
	public String toString()
	{
		String str;
		
		str = super.toString() +
		"\nShip's Name: " + n +
		"\nMaximum Passengers: " + maxPass;
		
		return str;
	}
}

Here is what I have for the CargoShip class so far:

public class CargoShip extends Ship
{

	private int cargoCap;
	
	public CargoShip(String n, String b)
	{
		super(n, b);
	}
	
	public void setCargoCap(int cc)
	{
		cargoCap = cc;
	}
	
	public int getCargoCap()
	{
		return cargoCap;
	}
	
	public String toString()
	{
		String str;
		
		str = super.toString() +
		"\nShip's Name: " + n +
		"\nCargo Capacity; " + cargoCap;
		
		return str;
	}
	
}

and here is my ShipDemo class so far:

public class ShipDemo 
{
	public static void main(String[] args)
	{
		//Create an array of Ship References.
		Ship[] types = new Ship[6];
		
		types[0] = new Ship();
		types[0].setName("Carnival");
		((CruiseShip) types[0]).setMaxPass(500);
		
		types[1] = new Ship();
		types[1].setName("Royal Caribbean");
		((CruiseShip) types[1]).setMaxPass(1000);
		
		types[2] = new Ship();
		types[2].setName("Sony");
		((CargoShip) types[2]).setCargoCap(10000);
		
		types[3] = new Ship();
		types[3].setName("Playskool");
		((CargoShip) types[2]).setCargoCap(25000);
		
		
		
		for (int i=0; i < types.length; i++)
		{
			System.out.println("Ship " + (i + 1) + ": ");
			//Need something here to call the toString methods 
			
		}
	}
}

I could use your feedback on how I have done so far, if I have made errors any where, and what my next step toward completing this may be. Thanks for looking!

 
0
 

Assign various Ship, CruiseShip, and CargoShip objects to the array elements.

Your program isn't correct; the teacher is trying to demonstrate a concept called Polymorphism to you. Essentially you should be creating Ships like this:

#
Ship[] types = new Ship[6];
types[0] = new CargoShip();
types[1] = new CruiseShip();
types[2] = new Ship();
types[3] = new OtherKindOfShip();

Also, I noticed you did a nice job with overriding the toString method in Ship and providing a generic implementation (generic in the sense that it just returns something generic) then you also correctly overrode the toString method in each class that extends Ship. Good work. You should also be calling the toString method in your for loop though. For example:

for (int i=0; i < types.length; i++)
{
System.out.println("Ship " + (i + 1) + ": ");
//Need something here to call the toString method
System.out.println(types[i].toString());
}
 
0
 

A brief explanation and example of polymorphism (definition from wikipedia, which btw, seems to only have part of the definition correct, and seems to combine the definition of 'inheritance' with polymorphism. But I think this bit is particularly good):

The primary usage of polymorphism is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run time (this is called late binding or dynamic binding).

This just says that you can make an array of Ships, and put anything that extends Ship into that array. Later on in your program, you can say myShipArray[anyIndex].anyMethodShipHas() and it will, with late binding, determine which class's method to call (so it will call CargoShip's method if the Ship is a CargoShip).


hope that is helpful.

 
0
 

It seems that I should re-read the whole polymorphism section again. :D Thanks for your help! I don't quite have it worked all the way out yet...but that gets me closer!

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article