Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//debug 3, chapter 11 //java programming, joycell farrell, 8th edi //fix and sho

ID: 3672480 • Letter: #

Question

//debug 3, chapter 11

//java programming, joycell farrell, 8th edi

//fix and show what right code

//output

1 // Two boats should be equal
2 // if they hold the same number of passengers
3 // and also have the same power source
4 public class DebugEleven3
5 {
6 public static void main(String args[])
7 {
8 DebugRowboat redBoat = new DebugRowboat;
9 DebugRowboat blueBoat = new DebugRowboat();
10 System.out.print("The two boats are");
11 if(redBoat == blueBoat)
12 System.out.println(" equal");
13 else
14 System.out.println(" not equal");
15 }
16 }
17

=============

1 public class DebugRowboat extends DebugBoat
2 {
3 public DebugRowboat()
4 {
5 super("row");
6 }
7 public void setPassengers()
8 {
9 super.passengers = 2;
10 }
11 public void setPower()
12 {
13 super.power = "oars";
14 }

===========

1 public abstract class DebugBoat
2 {
3 String boatType = new String();
4 int passengers;
5 String power = new String();
6 public DebugBoat(String bt)
7 {
8 boatType = bt;
9 }
10 // override equals() method to satisfy
11 // requirements of Debug Exercise 3.
12 public boolean equals(DebugBoat otherBoat)
13 {
14 boolean result;
15 if((passengers == otherBoat.passengers) && (power.equals(otherBoat.power)))
16 result = true;
17 else
18 result = false;
19 return result;
20 }
21 public String toString()
22 {
23 return("This " + boatType + "boat carries " + passengers +
24 " and is powered by " + power);
25 }
26 public abstract void setPower();
27 public abstract void setPassengers();
28 }

Explanation / Answer

Hi, I have fixed it.

Problem: to comapre two object you can not use == operator, it will not give you desire result.

So, use equal method.

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

abstract class DebugBoat
{
String boatType = new String();
int passengers;
String power = new String();
public DebugBoat(String bt)
{
boatType = bt;
}
// override equals() method to satisfy
// requirements of Debug Exercise 3.
public boolean equals(DebugBoat otherBoat)
{
boolean result;
if((passengers == otherBoat.passengers) && (power.equals(otherBoat.power)))
result = true;
else
result = false;
return result;
}
public String toString()
{
return("This " + boatType + "boat carries " + passengers +
" and is powered by " + power);
}
public abstract void setPower();
public abstract void setPassengers();
}

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

/*

Output:

The two boats are equal

*/