Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Super/subclasses Help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Krocker




PostPosted: Mon Jan 27, 2014 4:29 pm   Post subject: Super/subclasses Help

code:

                Ship s1 = new Ship();
                CruiseShip s2 = new CruiseShip();
                CargoShip s3 = new CargoShip();
                CruiseShip s4 = new CruiseShip();
                CargoShip s5 = new CargoShip();
               
                s1.setVariable(1701, "Queen Annes Revenge");
                s2.setData(2245, 2400, "USS Enterprise");
                s3.setData(1699, 50000, "Black Pearl");
                s4.setData(2371, 2800, "USS Voyager");
                s5.setData(1790, 31000, "The Victory");
               
                Ship[] shipLog = new Ship[5];
               
                shipLog[0] = s1;
                shipLog[1] = s2;
                shipLog[2] = s3;
                shipLog[3] = s4;
                shipLog[4] = s5;
               
                System.out.println ("Displaying Ships:\n");
                for(int i = 0; i < 5; i++){
                        System.out.println(shipLog[i].toString());
                }
               
                System.out.println("--------------------------\n\nChecking ship 2 (accessing):\n");
                System.out.println("Name: " + shipLog[2].getName());
                System.out.println("Year Built: " + shipLog[2].getYear());
                System.out.println("Cargo Tonnage: " + shipLog[2].getCargo());


Hi, so im having trouble trying to call the getCargo for the array shipLog[2] as im getting the error "The method getCargo() is undefined for the type Ship". I have one superclass called Ship and 2 subclasses, one called CargoShip and the other is CruiseShip. I realized that my array is initialized for the ship class and since getCargo is in the CargoShip subclass, it cant be found. How do i go about fixing this (as this is the 1st time im using super/subclasses)?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Jan 27, 2014 4:41 pm   Post subject: RE:Super/subclasses Help

You need to cast the object to a CargoShip before you can access CargoShip members.

code:
System.out.println("Cargo Tonnage: " + (CargoShip) shipLog[2].getCargo());
Krocker




PostPosted: Mon Jan 27, 2014 4:44 pm   Post subject: Re: RE:Super/subclasses Help

Insectoid @ Mon Jan 27, 2014 4:41 pm wrote:
You need to cast the object to a CargoShip before you can access CargoShip members.

code:
System.out.println("Cargo Tonnage: " + (CargoShip) shipLog[2].getCargo());


I tried that, but im still getting the same error (The method getCargo() is undefined for the type Ship)

Maybe if i show the other classes, it might help solve this, maybe i did something wrong there (I dont think so, but u never know)



CruiseShip.java
 Description:
Cruise Ship Subclass

Download
 Filename:  CruiseShip.java
 Filesize:  509 Bytes
 Downloaded:  175 Time(s)


CargoShip.java
 Description:
Cargo Ship Subclass

Download
 Filename:  CargoShip.java
 Filesize:  502 Bytes
 Downloaded:  203 Time(s)


Ship.java
 Description:
superclass

Download
 Filename:  Ship.java
 Filesize:  554 Bytes
 Downloaded:  201 Time(s)

Insectoid




PostPosted: Mon Jan 27, 2014 4:52 pm   Post subject: RE:Super/subclasses Help

Ah, try this:

((CargoShip)ShipLog[2]).getCargo();
Krocker




PostPosted: Mon Jan 27, 2014 5:17 pm   Post subject: Re: Super/subclasses Help

well ill be, thanks that worked. I have another quick question, in my ship superclass i have a method called equals that suppose to compare the ships and check if any two has the same name and date built. It goes like this:

code:

        public boolean equals(Object sp){
                Ship s = (Ship) sp;
                return shipName.equals(s.shipName) && year == s.year;
        }


I got this code out of my textbook, but it doesnt do a great job explaining how to use it. Could someone please explain to me how i would i use this properly an dhow i would pass the 2 ship objects to this method?
Insectoid




PostPosted: Mon Jan 27, 2014 5:37 pm   Post subject: RE:Super/subclasses Help

Java doesn't automatically know how to check for equality between objects of a user-defined class. So you have to describe what makes these objects equal. In your case, ship a == ship b if the names and years of the ships are the same. Your function takes an object as a parameter and converts that to a ship. I'm guessing the third line is what's confusing you. && is the AND operator. The line just checks if the shipnames are equal (shipName is a string, I'm guessing. equals() is already defined for strings), which returns either TRUE or FALSE. It then checks if the years are equal, which returns TRUE or FALSE. Finally, it ANDs those results together and returns the result.

This function should be a member of class Ship. It only takes one ship as an argument, and should be used as follows:

code:
Ship A;
Ship B;

Boolean result = A.equals(B);
System.Out.Println (result);
Raknarg




PostPosted: Mon Jan 27, 2014 5:50 pm   Post subject: RE:Super/subclasses Help

Is there a way to have java automatically use that as a comparison, like in Python? In python you can set the actual behaviours of an object, like how it handles comparisons and equalities.
Insectoid




PostPosted: Mon Jan 27, 2014 6:00 pm   Post subject: RE:Super/subclasses Help

Are you talking about overloading the basic operators? You can't do that in Java. Comparing objects with the = operator will just compare their addresses (I think), which will always return false unless you compare an object to itself (this does not apply to basic types ie int, etc).
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Mon Jan 27, 2014 7:19 pm   Post subject: RE:Super/subclasses Help

To clarify: Java does not support operator overloading (but it has a syntax special case for using the plus symbol (+) for string concatenation). Java's equality operator compares by object identity, which means "literally the same object" as opposed to "potentially different objects that describe the same data" (equality).

However, if you implement .equals(), then that tells users how to compare objects for equality. If you implement .hashcode(), then you can use your objects in a hash-based data structure, like a HashMap or HashSet. If your class implements Comparable<MyClass> and you implement the required .compare(MyClass other), then Java's standard collections will also know how to compare your objects and sort them.

Alternatively, you can sort in different ways by using instances of Comparator<T>.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: