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!