11 Replies - 696 Views - Last Post: 13 February 2012 - 10:58 PM Rate Topic: -----

#1 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

overriding methods

Posted 13 February 2012 - 05:10 AM

modify the Auto class below to override the equals method of the Vehicle class to test that the VIN and license plate number are identical. I can't figure out how to compare the vins. I got the licenses plates, but have not idea how to do the vins.

Auto.java
/**
   Represents an automobile.
*/

public class Auto extends Vehicle
{
   private String licensePlate;
   
   public Auto(String vin, String plate)
   {
      super(vin);
      licensePlate = plate;
   }

   // TODO: Override the equals method of the Object class to
   // test that VIN and license plate number are identical.

   public boolean equals(Object otherObject)
   {
      Auto other = (Auto) otherObject;
      return licensePlate.equals(other.licensePlate);
      
      
   }
   
}


Vehicle.java
/**
   Represents a vehicle of any type.
*/
public class Vehicle
{
   private String id;

   public Vehicle(String anId)
   {
      id = anId;
   }
   
   public boolean equals(Object otherObject)
   {
      Vehicle other = (Vehicle) otherObject;
      return id.equals(other.id);
   }
}


AutoTester.java
public class AutoTester
{
   public static void main(String[] args)
   {
      Vehicle auto1 = new Auto("1234567890", "A141G3");
      Vehicle auto2 = new Auto("1234567890", "5ZSN090");
      Vehicle auto3 = new Auto("14916253649", "A141G3");
      Vehicle auto4 = new Auto("1234567890", "A141G3");
      System.out.println(auto1.equals(auto1));
      System.out.println("Expected: true");
      System.out.println(auto1.equals(auto2));
      System.out.println("Expected: false");
      System.out.println(auto1.equals(auto3));
      System.out.println("Expected: false");
      System.out.println(auto1.equals(auto4));
      System.out.println("Expected: true");
   }
}


Is This A Good Question/Topic? 0
  • +

Replies To: overriding methods

#2 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: overriding methods

Posted 13 February 2012 - 05:27 AM

Look at the equals method in the Vehicle class, that is how you compare them. vin is an input which is assigned to the ID of the car.

Here you can use boolean logic in your return statement

return firstcondition && secondcondition; // will return true if both conditions are correct, otherwise false



You alredy are comparing numberplates as a condition, you simply need to change the variable which you are comparing

Also, shouldn't numberplate be in the Vehicle class anyway? (Or maybe not all vehicles have numberplates, i'm not sure)
Was This Post Helpful? 0
  • +
  • -

#3 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: overriding methods

Posted 13 February 2012 - 05:34 AM

I understand how to add a second condition to compare. I don't know what variable to use to compare the vins, because it comes from the superclass. I am not allowed to alter the Vehicle class.
   public boolean equals(Object otherObject)
   {
      Auto other = (Auto) otherObject;
      return licensePlate.equals(other.licensePlate) && ... ;
      
      
   }


what would I use in place of licensePlate?

This post has been edited by swim_5318: 13 February 2012 - 05:35 AM

Was This Post Helpful? 0
  • +
  • -

#4 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: overriding methods

Posted 13 February 2012 - 05:44 AM

Lets look at what 'vin' is, vin is a parameter for your Auto class constructor, this constructor then calls super(vin) which is the Vehicle class constructor. This value is then assigned to 'id'. In short vin is assigned to 'id', so you want to be talking about ID instead.

Since Auto inherits from Vehicle, it also has an ID variable itself, you just can't see it, but it exists just as much as licencePlate does.

So you do not want to anything in place of licencePlate, but you want to add a second condition to your return statement checking if 'id' is equals. Remember, it is accessible in the same way licensePlate is, there's nothing special you need to do.

Edit: Correction by blackcompe, ID is private, sorry. If you can modify the Vehicle class, then his suggestion will be fine.

This post has been edited by Mylo: 13 February 2012 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

Re: overriding methods

Posted 13 February 2012 - 06:00 AM

Quote

Since Auto inherits from Vehicle, it also has an ID variable itself, you just can't see it, but it exists just as much as licencePlate does.


Subclasses don't inherit private instance members. The proper thing to do would have been to make Vehicle.id protected, so that Auto could use it. For whatever reason it isn't. Call super.equals(otherObject) to ensure the ids are equal and then compare the plate numbers.
Was This Post Helpful? 2
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,181
  • Joined: 20-September 08

Re: overriding methods

Posted 13 February 2012 - 06:06 AM

Quote

I don't know what variable to use to compare the vins, because it comes from the superclass.


Yes, you've got a problem, which is not because it comes from the superclass, but because the vin is inaccessible. A clue: 'accessor'

blackcompe beat me to it ;)

Quote

Call super.equals(otherObject) to ensure the ids are equal and then compare the plate numbers.


That's very nice. But - it doesn't really mean you can't consider the limiatations you'll be imposing on yourself by not following standard practice of using accessor/mutators where they are required.

Quote

The proper thing to do would have been to make Vehicle.id protected


That's often debated. I'm not 100% sure what my position on it is.

This post has been edited by g00se: 13 February 2012 - 06:08 AM

Was This Post Helpful? 2
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

Re: overriding methods

Posted 13 February 2012 - 06:15 AM

Quote

That's very nice. But - it doesn't really mean you can't consider the limiatations you'll be imposing on yourself by not following standard practice of using accessor/mutators where they are required.


Yeah, protected access wouldn't be appropriate here.
Was This Post Helpful? 1
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,181
  • Joined: 20-September 08

Re: overriding methods

Posted 13 February 2012 - 10:03 AM

In this case, whether or not you are going to use protected members in the superclass, blackcompe's suggestion of calling super.equals first is an elegant one.
Was This Post Helpful? 1
  • +
  • -

#9 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: overriding methods

Posted 13 February 2012 - 10:34 PM

I tried using the super.equal(otherObject) like blackcompe said but it is saying they equal when they don't. I am not getting the right output.
/**
   Represents an automobile.
*/

public class Auto extends Vehicle
{
   private String licensePlate;
   
   public Auto(String vin, String plate)
   {
      super(vin);
      licensePlate = plate;
   }

   // TODO: Override the equals method of the Object class to
   // test that VIN and license plate number are identical.

   public boolean equals(Object otherObject)
   {
      super.equals(otherObject);
      Auto other = (Auto) otherObject;
      return licensePlate.equals(other.licensePlate);
      
      
   }
   
}

Was This Post Helpful? 0
  • +
  • -

#10 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: overriding methods

Posted 13 February 2012 - 10:52 PM

Don't forget that both conditions have to be true. You are only returning whether or not the license plate is the same.
return licensePlate.equals(other.licensePlate) && super.equals(otherObject);
Was This Post Helpful? 2
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: overriding methods

Posted 13 February 2012 - 10:57 PM

Except you missed something, you didnt take the value returned from the super equals() method. You have to check what the super returned and then add it to the return, something like:
   public boolean equals(Object otherObject)
   {
      boolean s = super.equals(otherObject);
      Auto other = (Auto) otherObject;
      return licensePlate.equals(other.licensePlate) && s;
      
      
   }

Was This Post Helpful? 1
  • +
  • -

#12 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: overriding methods

Posted 13 February 2012 - 10:58 PM

Oh. Got it. Thank you so much everyone!

This post has been edited by swim_5318: 13 February 2012 - 10:59 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1