//java programming, joyce farraell, 8th ed //error when running, need to fix //d
ID: 3672482 • Letter: #
Question
//java programming, joyce farraell, 8th ed
//error when running, need to fix
//debug 4, Chapter 11
//show the correction code and output
1 // Creates and displays an array of boats -
2 // some are rowboats; some are ocean liners
3 import javax.swing.*;
4 public class DebugEleven4
5 {
6 static DebugBoat[] boatArray = new DebugBoat[5];
7 public static void main(String[] args)
8 {
9 buildArray();
10 displayArray();
11 }
12 public static void buildArray()
13 {
14 //declare x as int in for loop
15 char boatType;
16 for(int x = 0; x < boatArray.length; ++x)
17 {
18 boatType = getBoat();
19 if(boatType =='r')
20 boatArray[x] = DebugRowboat();
21 else
22 boatArray[x] = DebugOceanLiner();
23 }
24 }
25 public static void getBoat()
26 {
27 String boatType;
28 boatType = JOptionPane.showInputDialog(null,
29 "Enter r for rowboat; o for ocean liner ");
30 return boatType.charAt(0);
31 }
32 public static void displayArray()
33 {
34 for(int x = 0; x < boatArray.length; --x)
35 JOptionPane.showMessageDialog(null, "Boat #" + (x + 1) +
36 boatArray[x].toString);
37 }
38 }
39
===============
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 }
================
1 public class DebugOceanLiner extends DebugBoat
2 {
3 public DebugOceanLiner()
4 {
5 Debug("ocean liner ");
6 setPassengers();
7 setPower();
8 }
9 public void setPassengers()
10 {
11 super.passengers = 2400;
12 }
13 public void power()
14 {
15 super.power = "four engines";
16 }
17 }
18
==============
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 }
15 }
Explanation / Answer
Hi, I have corrected issues. You can run now
// Creates and displays an array of boats -
// some are rowboats; some are ocean liners
import javax.swing.*;
class DebugEleven4
{
static DebugBoat[] boatArray = new DebugBoat[5];
public static void main(String[] args)
{
buildArray();
displayArray();
}
public static void buildArray()
{
//declare x as int in for loop
char boatType;
for(int x = 0; x < boatArray.length; ++x)
{
boatType = getBoat();
if(boatType =='r')
boatArray[x] = new DebugRowboat();
else
boatArray[x] = new DebugOceanLiner();
}
}
public static char getBoat()
{
String boatType;
boatType = JOptionPane.showInputDialog(null,
"Enter r for rowboat; o for ocean liner ");
return boatType.charAt(0);
}
public static void displayArray()
{
for(int x = 0; x < boatArray.length; ++x)
JOptionPane.showMessageDialog(null, "Boat #" + (x + 1) +
boatArray[x].toString());
}
}
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 DebugOceanLiner extends DebugBoat
{
public DebugOceanLiner()
{
super("ocean liner ");
setPassengers();
setPower();
}
public void setPassengers()
{
super.passengers = 2400;
}
public void power()
{
super.power = "four engines";
}
@Override
public void setPower() {
super.power = "oceanLinear";
}
}
class DebugRowboat extends DebugBoat
{
public DebugRowboat()
{
super("row");
}
public void setPassengers()
{
super.passengers = 2;
}
public void setPower()
{
super.power = "oars";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.