





































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An example of creating and managing a simplebot object in java. The simplebot class extends the paintable class and includes instance variables for street, avenue, and direction. The class also includes methods for painting the bot, moving it, and turning it. An explanation of instance variables, accessing them, and testing the simplebot class.
Typology: Study notes
1 / 45
This page cannot be seen from the preview
Don't miss anything!






































+void paint(Graphics2D g)
int streetint avenueint direction+SimpleBot( )+void move( )+void turnLeft( )+void paint(Graphics2D g)
Override paint to determinethe robot's appearance. Everyobject displayed in
SimpleCity
must do this.Attributes (instancevariables) to remember thestreet, avenue, and direction.Methods that use and updatethe instance variables.
public class SimpleBot extends Paintable{
private int street = 4;
// Create space to store the robot’s current street
private int avenue = 2;
// Create space to store the robot’s current avenue
public SimpleBot(){ super();} // Incomplete class!
g.fillOval(
this.avenue
50
,
this.street
50
,
50
,
50
);
int^2
int^4
int^50
int^50
int^50
int^50
g.fillOval(
this.avenue
50
,
this.street
50
,
50
,
50
);
int^2
int^4
int^50
int^50
int^50
int^50
int
int 200
100
g.fillOval(
this.avenue
50
,
this.street
50
,
50
,
50
);
int^2
int^4
int^50
int^50
int^50
int^50
int
int 200
100
void
(the oval is drawn)
import java.awt.Graphics2D;import java.awt.Color;public class SimpleBot extends Paintable{
private int street = 4;
// Create space to store the robot’s current street
private int avenue = 2;
// Create space to store the robot’s current avenue
public SimpleBot()…public void paint(Graphics2D g){ g.setColor(Color.BLACK);
g.fillOval(this.avenue * 50, this.street * 50, 50, 50); } public void move(){ this.avenue = this.avenue + 1;
// Incomplete
} public void turnLeft(){ } }
import java.awt.Graphics2D;import java.awt.Color;import becker.util.Utilities;public class SimpleBot extends Paintable{
private int street = 4;
// Create space to store the robot’s current street
private int avenue = 2;
// Create space to store the robot’s current avenue
public SimpleBot()…public void paint(Graphics2D g){ g.setColor(Color.BLACK);
g.fillOval(this.avenue * 50, this.street * 50, 50, 50); } public void move(){ this.avenue = this.avenue + 1;
// Incomplete
Utilities.sleep(400);
// sleep for 400 milliseconds so user has// time to see the move
} public void turnLeft(){ } }
/** A main method to test the SimpleBot and related classes.* * @author Byron Weber Becker */ public class Main{
public static void main(String[ ] args){ SimpleCity newYork = new SimpleCity();
SimpleBot karel = new SimpleBot();SimpleBot sue = new SimpleBot();newYork.add(karel, 2);newYork.add(sue, 2);newYork.waitForStart();
// Wait for the user to press the start button.
for(int i=0; i<4; i = i+1){ karel.move();
karel.move();karel.turnLeft(); } sue.move(); } }
…public class SimpleBot extends Paintable{ …
private int east = 0;private int south = 1;private int west = 2;private int north = 3;private int direction = this.east;
// Begin facing east
… /** Turn the robot left 1/4 turn. */ public void turnLeft(){ if (this.direction == this.east)
// if facing east…
{ this.direction = this.north;
// face north
} else{ this.direction = this.direction – 1;} } }
public class Constants{
public static final int EAST = 0;public static final int SOUTH = 1;public static final int WEST = 2;public static final int NORTH = 3; } public class SimpleBot extends Paintable{ …
private int direction = Constants.EAST;
// Begin facing east
… /** Turn the robot left 1/4 turn. */ public void turnLeft(){ if (this.direction == Constants.EAST)
// if facing east…
{ this.direction = Constants.NORTH;
// face north
} else{ this.direction = this.direction – 1;} } }
public
«typeReturned»
get
«Name»
{ return this.
«instanceVariable»
private int avenue = 2;… public int getAvenue(){ return this.avenue;} … }
(bodyX-15, bodyY-15)^ (bodyX, bodyY)
(sensorX-6, sensorY-6)(sensorX, sensorY)
(this.avenue * 50, this.street * 50)
int iSize = Constants.INTERSECTION_SIZE;int bodyX = this.avenue * iSize + iSize / 2;
this.avenue
*****^
iSize
+^
iSize
/^
2 int^2
int^50
int 100
int^50
int^2
int^25
int 125