Member Avatar for BobTheLob

Okay, so i've been trying to get this code to work for quite some time, and i'm about to go bonkers. Hopefully y'all can help me out with this problem because I need to hand this one in soon.

My problem is I need to make an array of objects. Check.
(Note that the classes i'm using extend a superclass, Ship).
Now I need to set information into those objects. Semi-check.

I can put all the information into the superclass (Ship), but when I try to put info into the sub classes (CruiseShip, CargoShip), I can't. I mean I can put in the name, because the name is from the super class. But I can't put in say the max number of passengers.

Here's my code for the Boats program that runs the thing:

import java.util.Scanner;

public class Boats
{	
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		
		String shipName, yearBuilt;
		int maxPassengers = 0, maxCargo = 0;
		
		Ship[] type = new Ship[2];
				
		
			//Finding Ship's info------------------------------
		type[0] = new Ship();
		System.out.println("Enter the ship's name: ");
		shipName = keyboard.nextLine();
		type[0].setName(shipName);

		
		System.out.println("Enter the year the ship was made: ");
		yearBuilt = keyboard.nextLine();		
		type[0].setYear(yearBuilt);
		
		
		
		
			//Finding CruiseShip's info------------------------
		type[1] = new CruiseShip();
		System.out.println("Enter the cruise ship's name: ");
		shipName = keyboard.nextLine();
		type[1].setName(shipName);
		
		System.out.println("Enter the maximum number of passangers: ");
		maxPassengers = keyboard.nextInt();
		keyboard.nextLine();		
		type[1].setMax(maxPassengers);
		
		
		
		
			//Finding CargoShip's info------------------------
		type[2] = new CargoShip();
		System.out.println("Enter the cargo ship's name: ");
		shipName = keyboard.nextLine();	
		type[2].setName(shipName);
		
		System.out.println("Enter the maximum cargo capacity: ");
		maxCargo = keyboard.nextInt();		
		type[2].setCargo(maxCargo);
		
				
		for (int i = 0; i<3; i++) {
			type[i].toString();
		}
	
		
		
	}
}

Here's the error I get though:

Boats.java:38: cannot find symbol
symbol  : method setMax(int)
location: class Ship
		type[1].setMax(maxPassengers);
		       ^
Boats.java:51: cannot find symbol
symbol  : method setCargo(int)
location: class Ship
		type[2].setCargo(maxCargo);
		       ^
2 errors

And here's the CruiseShip.java file:

public class CruiseShip extends Ship
{

	private int maxPass;
	
	public void setMax (int n)
	{
		maxPass = n;
	}
	
	public String toString ()
	{
		
		return "Name: " + shipName + "\nMax Number of Passengers: " + maxPass;
		
	}
	
}

I really feel like I'm missing something simple, but can't see it. I tell yah, i've shot my foot of less times with C++ than this :P

Thanks for the help!

Hi
Can you provide the code of the Ship class please?

Member Avatar for BobTheLob
public class Ship
{
	
	public String shipName;
	
	private String yearBuilt;
	
	public void setName (String n)
	{
		shipName = n;
		System.out.println("Inside the setName Super");
	}
	
	public void setYear (String y)
	{
		yearBuilt = y;
		System.out.println("Inside the setYear Super");

	}
	
	public String toString ()
	{
		return "Name: " +  shipName + "\nBuilt in: " + yearBuilt;
	}
}

Sorry, forgot to include that.

You have this declaration in the Boats calss

Ship[] type = new Ship[2];

and the Ship class have no methode with the setMax; in facte this method is declard in the CruiseShip Class.
So itis not permited to call it over the type[1] even if you do this instructio:

type[1] = new CruiseShip();

To avoid this move the function setMax with it's data member from CruiseShip class to Ship Calss.
Hope it helps

Another alternative is to declare the Ship class as abstract with some setMax as abstract function.
you need also to move the maxPass to the Ship class with the protected modifier.

the second proposition result in many changes

Member Avatar for BobTheLob

Excellent.
However, in the project description, I need to have a CruiseShip class that extends the Ship class. And then I need to demonstrate the multiple classes I made in a program with a Ship array. And then assign various Ship, CruiseShip, and CargoShip objects to the array elements.

So how would I go about doing this?

Other solutions are possible!

Well can you give the description of your super class Ship please:
What attributs it holds?
What methods it provides?

you can rename them by
(Profile p, Profile oldestStu) instead of first and second student

Sorry the obove message is not to you sorry

Member Avatar for BobTheLob

Uhh, not sure what you meant with your last post. But here's the answer to before.
The superclass Ship gets and holds the name of the ship. This is one of the attributes that needs to be shared with the other classes.
Ship also contains the setYear function which gets the year the ship was made (this one does not need to be shared with other classes).
And lastly, the Ship class has a toString method which outputs the ships name and year.
Each class has it's own toString method though which overrides the superclasses. (because each boat is going to output a name, and then something unique to that boat).

In the projects description, this is what it says:
Design a Ship class that has 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.

this one does not need to be shared with other classes? why?
Is there any subclass of the Ship that have not a year of build?

Member Avatar for BobTheLob

No it doesn't. The only thing that is shared with it's subclasses (at least used by its subclasses) is the name.
So the program is looking at the Ship class, and then looking at CruiseShip (which extends the Ship class so it can use the name). At least that's what it should be doing.

If you just want to avoid the error you should may be made this cast:
In the Boats calss

((CruiseShip)type[1]).setMax(maxPassengers);
commented: Brilliant help! +3

And also this

((CargoShip)type[2]).setCargo(maxCargo);
Member Avatar for BobTheLob

I think my main problem is this:
What I need to do is just get multiple objects into the array elements of the Ship array.
I would think i would do it the way I did in my code, but apparently not. :P

Have try to made the change I suggested above?

Member Avatar for BobTheLob

Brilliant. That took care of the errors.
However, couple things.
First, for the sake of learning here (cause i'm still a newbie Java guy), what exactly did putting the parentheses around the class name do?
Second, my only problem left is printing from the toString. I'm trying to do this:

for (int i = 0; i<3; i++) {
			type[i].toString();
		}

That should go through each object and go to the toString method. However, it doesn't do anything.

DO this:

for (int i = 0; i<3; i++) {
			System.out.println(type[i].toString());
		}

putting the parentheses around the class means to cast (convert) some type to another type potentialy convertisible to the type in the parentheses.

Member Avatar for BobTheLob

Dagnabit, my brain is on the failing end at this point. Totally forgot that I set that up as a return.
And I think I got what we did with the casting.
Thank you so much for all the help Moutanna! You've been tremendous!

Happy to help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.