1 Replies - 553 Views - Last Post: 07 November 2010 - 05:25 PM Rate Topic: -----

#1 samuel17  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 209
  • Joined: 14-October 10

Boat comparison

Posted 07 November 2010 - 03:51 PM

Hi all,
Wondering if someone can help me again? I have a couple different classes, including an abstract and what im trying to make this do is compare an ocean liner to a row boat. If the amount of passengers and the power of the boat are different, then the message should display that "The two boats are equal" otherwise it should say "The two boats are equal" if they match. I get it to compile fine and the message shows up, but the message is always that they are equal, no matter what i change the passengers and power to. Any ideas. Here is all my code:

// Two boats should be equal
// if they hold the same number of passengers
// and have the same power source
public class DebugTwelve3
{
  public static void main(String args[])
  {
    DebugRowboat redBoat = new DebugRowboat();
    DebugRowboat blueBoat = new DebugRowboat();
    System.out.print("The two boats are");
    if(redBoat.passengers == (blueBoat.passengers)&& redBoat.power.equals(blueBoat.power))
	 
      System.out.println(" equal.");
    else
	  System.out.println(" not equal.");
  }
}


public class DebugRowboat extends DebugBoat
{
  public DebugRowboat()
  {
	super("row");
  }
  public void setPassengers()
  {
	super.passengers = 2;
  }
  public void setPower()
  {
    super.power = "ores";
  }
}


public class DebugOceanLiner extends DebugBoat
{
	public DebugOceanLiner()
	{
	  super("ocean liner ");
      setPassengers();
      setPower();
	}
	public void setPassengers()
	{
	 super.passengers= 2400;
	}
    public void setPower()
    {
      super.power ="four engines";
    }
}


public abstract class DebugBoat
{
		  String boatType = new String();
		  public abstract void setPower();
        public abstract void setPassengers();
        int passengers;
        String power = new String();
        public DebugBoat(String bt)
	{
		
	    boatType = bt;

	}
	public String toString()
	{
	   return("This " + boatType + "boat carries " + passengers + " and is powered by " + power);
	}
		// deleted curly braces and added ;
        
       

}

Also the last class here, the DebugBoat class, What is the purpose of it, it was included on the cd i got with my book, and the 2 classes extend DebugBoat. I dont see where they use it though, but my main problem is getting the 2 boats compared correctly, the DebugBoat is more so me trying to understand better. Thanks guys.

Sam

Is This A Good Question/Topic? 0
  • +

Replies To: Boat comparison

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,851
  • Joined: 18-April 07

Re: Boat comparison

Posted 07 November 2010 - 05:25 PM

Well of course they are going to come out equal, they are. They are because you create two DebugRowboat class instances which set the default passengers and power to the same values. Now if redBoat was a DebugOceanLiner and the blueBoat was a DebugRowboat, then they would be different. OR if both were DebugRowboat instance but that you set the passengers/power of the redBoat to be different than the blueBoat.

Your setPassengers method on DebugRowboat should probably take a parameter... the value to change it to. That and you actually use the incoming variable. Then you could do this...


public void setPassengers(int numPassengers)
{
	this.passengers = numPassengers;
}




DebugRowboat redBoat = new DebugRowboat();
DebugRowboat blueBoat = new DebugRowboat();

// Set redBoat to hold 100 passengers
redBoat.setPassengers(100);

System.out.print("The two boats are");

// Now they will show up different because the number of passengers won't be the same.
if(redBoat.passengers == (blueBoat.passengers)&& redBoat.power.equals(blueBoat.power))
	 
    System.out.println(" equal.");
else
    System.out.println(" not equal.");



But since you define both red and blue boats to be both of type DebugRowboat and then don't change the values of either one you are comparing based on their default values... which are the same. Makes sense right?

Hope this helps. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1