In: Computer Science
I was wondering is someone could tell me why my code isn't compiling - Java
------------------------------------------------------------------------------------------------------------
class Robot{
int serialNumber;
boolean flies,autonomous,teleoperated;
public void setCapabilities(int serialNumber, boolean flies,
boolean autonomous, boolean teleoperated){
this.serialNumber = serialNumber;
this.flies = flies;
this.autonomous = autonomous;
this.teleoperated = teleoperated;
}
public int getSerialNumber(){
return this.serialNumber;
}
public boolean canFly(){
return this.flies;
}
public boolean isAutonomous(){
return this.autonomous;
}
public boolean isTeleoperated(){
return this.teleoperated;
}
public String getCapabilities(){
StringBuilder str = new StringBuilder();
if(this.flies){str.append("canFly");str.append(" ");}
if(this.autonomous){str.append("autonomous");str.append("
");}
if(this.teleoperated){str.append("teleoperated");}
return str.toString();
}
public String toString(){
return "ID: "+ this.serialNumber+", Capabilities: " +
getCapabilities(this.flies,this.autonomous,this.teleoperated);
}
}
//Drone class
class Drone extends Robot{
public void Drone(int serialNumber){
this.serialNumber = serialNumber;
this.flies = true;
this.autonomous = false;
this.teleoperated = true;
}
public boolean canFly(){
return true;
}
public boolean isAutonomous(){
return false;
}
public boolean isTeleoperated(){
return true;
}
}
//Roomba class
class Roomba extends Robot{
boolean canClean = true;
public void Drone(int serialNumber){
this.serialNumber = serialNumber;
this.flies = false;
this.autonomous = true;
this.teleoperated = false;
canClean = true;
}
public boolean canClean(){
return true;
}
public boolean canFly(){
return false;
}
public boolean isAutonomous(){
return true;
}
public boolean isTeleoperated(){
return false;
}
public String getCapabilities(){
StringBuilder str=new StringBuilder();
if (this.flies){str.append("canFly");str.append(" ");}
if (this.autonomous){str.append("autonomous");str.append("
");}
if (this.teleoperated){str.append("teleoperated");str.append("
");}
if (this.canClean){str.append("canClean");}
return str.toString();
}
}
//MovieRobot class
class MovieRobot extends Robot{
String catchphrase;
public void setCapabilities(int serialNumber, boolean flies,
boolean autonomous, boolean teleoperated, String
catchphrase){
this.serialNumber = serialNumber;
this.flies = flies;
this.autonomous = autonomous;
this.teleoperated = teleoperated;
this.catchphrase = catchphrase;
}
public boolean canFly(){
return this.flies;
}
public boolean isAutonomous(){
return this.autonomous;
}
public boolean isTeleoperated(){
return this.teleoperated;
}
public String canSpeak(){
return this.catchphrase;
}
public String getCapabilities(){
StringBuilder str = new StringBuilder();
if (this.flies){str.append("canFly"); str.append(" ");}
if (this.autonomous){str.append("autonomous"); str.append("
");}
if (this.teleoperated){str.append("teleoperated"); str.append("
");}
if (!(this.catchphrase).equals("")){str.append(catchphrase);}
return str.toString();
}
public String toString(){
return "ID: " + this.serialNumber + ", Capabilities: " +
this.getCapabilities(this.flies, this.autonomous,
this.teleoperated);
}
}
class HAL9000 extends MovieRobot{
String catchphrase="I can't let you do that Dave.";
}
class Terminator extends MovieRobot{
boolean canTimeTravel = true;
String catchphrase = "I'll be back.";
public void setCapabilities(boolean canTimeTravel, int
serialNumber, boolean flies, boolean autonomous, boolean
teleoperated, String catchphrase){
this.serialNumber = serialNumber;
this.flies = flies;
this.autonomous = autonomous;
this.teleoperated = teleoperated;
this.canTimeTravel = canTimeTravel;
this.catchphrase=catchphrase;
}
public boolean canTimeTravel(){
return this.canTimeTravel;
}
}
class WALL_E extends MovieRobot{
boolean canClean = true;
public void setCapabilities(boolean canClean, int serialNumber,
boolean flies, boolean autonomous, boolean teleoperated){
this.serialNumber=serialNumber;
this.flies = flies;
this.autonomous = autonomous;
this.teleoperated = teleoperated;
this.canClean = canClean;
this.catchphrase = catchphrase;
}
public boolean canClean(){
return true;
}
}
Here is the solution to your
question. I tried my best to solve your doubt, however, if you find
it is not as good as expected by you. Please do write your further
doubts regarding this question in the comment section, I will try
to resolve your doubts regarding the submitted solution as soon as
possible.
Please give proper indentation as shown in the screenshot
If you think, the solution provided by me is helpful to you please
do an upvote.
Your code looks fine and it will get compiled if you change some code
1) In ToString() method of Robot.java , remove passed arguments of getCapabilities() as it does not require any parameter.
2) Simillarly in ToString() method of MovieRobot.java , remove passed arguments of getCapabilities() as it does not require any parameter.
Rest of the code is fine. Below is the code with proper changes that needs to be applied
|