LEARNING to Program with Robots (JAVA) The link to the textbook is: http://www.l
ID: 3866820 • Letter: L
Question
LEARNING to Program with Robots (JAVA)
The link to the textbook is: http://www.learningwithrobots.com/textbook/PDFs/WholeThing.pdf
Part A:
Write a class named BustedBot that extends RobotSE. A BustedBot is unreliable, occasionally turning either right or left (apparently at random) before moving. The probabilities of turning are given as two values (the probability of turning left and the probability of turning right) when the BustedBot is constructed. Write a main method that demonstrates your class.
Part B:
Run the following main method. Describe what happens. Based on what you know about the range of the type byte, why do you think this occurs?
public static void main(String[] args)
{ for(byte b=0; b<=128; b+=1)
{ System.out.println(b);
Utilities.sleep(30);
} }
Explanation / Answer
import becker.robots.City; import becker.robots.Direction; import becker.robots.Robot; /** * A Robot that is powered with an initial maximum power level (200 units). * Activities like "move", "turnLeft, "pickThing" and "putThing" consume power; * RobotEx records distance travelled. * Distance increments with each move * @author romona.cummins * @since 2009 */ public class RobotEx extends Robot { /* Implement a class RobotEx that extends class Robot (Robot comes from becker.jar under package becker.robots)RobotEx is a powered robot with an initial maximum power level of 200 units. Activities like "move", "turnLeft, "pickThing" and "putThing" consume power. RobotEx records the distance (along streets and avenues) it travels (using 'clicks'). */ //protected data member clicks //protected data member powerLeve(default 200) protected int clicks; protected int powerLevel=200; /**The RobotEx has one Overloaded constructor that passes the arguments to the constructor of the parent class * @param city City * @param street int * @param avenue int * @param direction Direction */ public RobotEx (City city, int street, int avenue, Direction direction) { setCity(city); setStreet(street); setAvenue(avenue); setDirection(direction); } /* To decrease powerLevel you must implement the logic as shown in the pseudocode if powerLevel > 0 then decrease powerLevel by 1 else call breakRobot("Out of Power"); end if PS: The 'breakRobot' is a method inherited from Robot (parent class) see javadocs given earlier */ //implement get methods here public int getClicks(){return clicks;} public int getPowerLevel() {return powerLevel;} //For example while overriding move() you must attempt to decrease //powerLevel (as shown in pseudocode). //Following the logic of decreasing powerLevel you must call //super.move() to make the robot move(). //Following super.move() you must increment the 'clicks' //Remember only the move() method updates clicks /* Override the following object level methods (from class Robot) in class RobotEx such that they decrement the powerLevel and, if applicable, increment clicks * move() * turnLeft() * pickThing() * putThing() */ //override move() to decrease power and update clicks protected void move(){ super.move(); if (powerLevel > 0){ robotEx.move(); powerLevel--; clicks++; }else{ breakRobot("Out of Power"); } } //override turnLeft() to decrease power protected void turnLeft(){ super.turnLeft(); if (powerLevel > 0){ robotEx.turnLeft(); powerLevel--; }else{ breakRobot("Out of Power"); } } //override pickThing()to decrease power protected void pickThing(){ super.pickThing(); if (RobotEx.canPickThing()){ RobotEx.pickThing(); powerLevel--; } } //override putThing() to decrease power protected void putThing(){ super.putThing(); RobotEx.putThing(); powerLevel--; } } //override toString() to return clicks and powerLevel information //Override the toString method to return a String (reference) //that combines the clicks and powerLevel information protected String toString(){ return String.format("Clicks: %.2f Powerlevel: %.2f", clicks, powerLevel); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.