8 Replies - 5637 Views - Last Post: 14 July 2015 - 12:24 PM Rate Topic: -----

#1 EsqPretending2Code   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 13-July 15

ArrayList and inheritance

Posted 13 July 2015 - 11:25 PM

Hi, I have a similar assignment as this guy:
http://www.dreaminco...goship-classes/

I, however, have less requirements. I just need to generate a ship superclass, and a cargo ship and cruise ship subclass, use 3-5 properties, and put them in an array to print out. I'm having 2 problems.

1: I think I'm missing something in my constructors or I've failed at ArrayList, because my ArrayList add method is kicking me back "Cannot invoke add(ship)on array type ArrayList<ship>[]." given that ArrayList itself is pretty simple, I think I've messed up ona constructor someplace, but I can't figure out where.

2: My print function is not playing nicely with ArrayList either.

I've done some serious Googling and am coming up empty. Help me Obi-Wan Codenobi, you're my only hope.

DRIVER
 
import java.util.ArrayList;
import java.util.Scanner;
public class Driver  {

	public static void main(String[] args)
	{
	ArrayList<ship>[] fleet1 = new ArrayList[6];
	
	ship ship1=new cruise_ship("Princess", "USA", 325, "Bermuda", 6398);
	ship ship2=new cruise_ship("Royal Carribean", "USA", 325, "Puerto Rico", 6823);
	ship ship3=new cruise_ship("Princess", "Cayman Islands", 225, "Greece", 4348);
	ship ship4=new cargo_ship("Slick Sailing", "Saudi Arabia", 375, "oil", 15000);
	ship ship5=new cargo_ship("Nissan", "Japan", 400, "cars", 67000);
	ship ship6=new cargo_ship("George of the Jungle", "Cambodia", 325, "zoo animals", 85);
	
	fleet1.add(ship1);
	fleet1.add(ship2);
	fleet1.add(ship3);
	fleet1.add(ship4);
	fleet1.add(ship5);
	fleet1.add(ship6);
	
	for(int i = 0;i>fleet1.length;i++) {
	System.out.println(toString(fleet1[i]));
	
	}
	
	}
	}



Superclass

public class ship {
	
	private String name;
	private String nation;
	private int length;

	public ship(String n, String nat, int a)
	{
	    name = n;
	    length = a;
	    nation = nat;}
	
	public String toString()
	{
	    return ("Name: " + getName() + "Country of registration: "+ getNation()+", Length: " + getLength() + " ");
	}

	public String getName() 
	{
	    return name;
	}

	public void setName(String newName)
	{
	    name = newName;
	}

	public int getLength() 
	{
	    return length;
	}

	public void setLength(int newLength)
	{
	    length = newLength;
	}
	public String getNation() 
	{
	    return nation;
	}

	public void setNation(String newNation)
	{
	    nation = newNation;
	}

}




Subclass (they are identical except the names of the variables)

	public class cruise_ship extends ship
	{
	private String destination;
	private int passengers;

	public cruise_ship(String theName, String theNat, int theAge, String theDest, int thePassengers)
	{
	    super(theName, theNat, theAge);
	    setDest(theDest);
	    setPass(thePassengers);
	}

	public String toString()
	{
	    return ("Name: " + getName() + ", Length in meters: " + getLength() + "is registered in: "+ getNation()+", Is headed to: " + destination + ", Number of Passengers: " + passengers);
	}

		public String getDest() 
	{
	    return destination;
	}

	public void setDest(String newDest)
	{
	    destination = newDest;
	}

	public double getPass() 
	{
	    return passengers;
	}

	public void setPass(int newPass)
	{
	    passengers = newPass;
	}

	}




Is This A Good Question/Topic? 1
  • +

Replies To: ArrayList and inheritance

#2 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 988
  • View blog
  • Posts: 3,887
  • Joined: 13-June 14

Re: ArrayList and inheritance

Posted 14 July 2015 - 12:07 AM

I haven't looked at all the code, but a few things:

1. Class names begin with uppercase letters in Java and we use camel case and not underscores (edit: you might want to look at Oracle's code conventions for Java so you can avoid other style issues).

2. Look at line 7 in Driver.java:

ArrayList<ship>[] fleet1 = new ArrayList[6];


Did you really want an array of ArrayLists?

3. Line 23:

for(int i = 0;i>fleet1.length;i++) {


Is the test condition in this for loop correct?

This post has been edited by ndc85430: 14 July 2015 - 12:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 EsqPretending2Code   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 13-July 15

Re: ArrayList and inheritance

Posted 14 July 2015 - 11:47 AM

1. New to Java, will work on it.
2. In the previously cited thread, they use:
ArrayList<Ship> list = new ArrayList<Ship>();



That's giving me errors:
"cannot create generic array of ArrayList<ship>" and
"Type mistamatch: cannot convert from ArrayList<ship> to ArrayList<ship>[]

3. That should work since arrays are numbered 1 less than their length because of starting at 0.
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 988
  • View blog
  • Posts: 3,887
  • Joined: 13-June 14

Re: ArrayList and inheritance

Posted 14 July 2015 - 11:49 AM

Can we see a small snippet of code that's causing the type mismatch error? Also, tell us what you think the error means (I and others will know, but it's useful for you to try and work things out).
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12680
  • View blog
  • Posts: 45,865
  • Joined: 27-December 08

Re: ArrayList and inheritance

Posted 14 July 2015 - 11:51 AM

2) Don't use arrays of Lists. It's generally bad practice. You really should just use ArrayList<ship>.

3) Check the condition. We have initially i = 0. Then we check if i > fleet1.length. If i = 0, will the condition i > fleet1.length evaluate to true? You can test this.
Was This Post Helpful? 0
  • +
  • -

#6 EsqPretending2Code   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 13-July 15

Re: ArrayList and inheritance

Posted 14 July 2015 - 12:14 PM

2.The line causing the mismatch was cited in my last response. The mismatch doesn't appear to be recognizing the square brackets, which makes me think the compiler is not reading the brackets as operators, but rather as part of the identifier. That in turn makes me think I've got the format incorrect.

What I want to be doing is create an ArrayList of ships. However, from what I've found, there appear to be conflicting means of doing that. There's the first reply post on this thread http://www.dreaminco...goship-classes/ (cited above) and Then this website http://www.tutorials...ylist_class.htm mentions entirely different ways.

Initially I started by using just arrays, but all the help I was finding on forums was directing other users to use ArrayList, so I switched to that.

3. Oh crap, the direction is wrong. Thanks for beating me over the head with that one.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12680
  • View blog
  • Posts: 45,865
  • Joined: 27-December 08

Re: ArrayList and inheritance

Posted 14 July 2015 - 12:17 PM

Quote

2.The line causing the mismatch was cited in my last response. The mismatch doesn't appear to be recognizing the square brackets, which makes me think the compiler is not reading the brackets as operators, but rather as part of the identifier. That in turn makes me think I've got the format incorrect.

So don't try to create an array of Lists. Just use ArrayList<ship>, NOT ArrayList<ship>[]. The brackets at the end indicate an array of ArrayLists, which you don't want to use. We have a tutorial on ArrayLists vs. static arrays which might be a worthwhile read.

Glad you got the third point figured out. :)
Was This Post Helpful? 0
  • +
  • -

#8 EsqPretending2Code   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 13-July 15

Re: ArrayList and inheritance

Posted 14 July 2015 - 12:23 PM

Cool, I blew out the square brackets and it worked. My print line still doesn't work, but I'll spend some time playing with it to see what I can do.

related: Is the guy in the other thread (Japanir) I cited just wrong, or is there a purpose for his suggestion?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12680
  • View blog
  • Posts: 45,865
  • Joined: 27-December 08

Re: ArrayList and inheritance

Posted 14 July 2015 - 12:24 PM

Java is case sensitive. So if you are naming your class ship by trying to instantiate ArrayList<Ship>, Java won't be able to find Ship due to the capitalized S.

japanir's first post in that thread is correct.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1