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();

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

Here's the error I get though:

Boats.java:38: cannot find symbol
symbol  : method setMax(int)
location: class Ship
Boats.java:51: cannot find symbol
symbol  : method setCargo(int)
location: class Ship
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!

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

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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.

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?

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

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?

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


Edited 3 Years Ago by mike_2000_17: Fixed formatting

Brilliant help!

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

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++) {

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++) {

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

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!

This question has already been answered. Start a new discussion instead.