Java Programming: Creating and Managing a SimpleBot Object, Study notes of Computer Science

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

Pre 2010

Uploaded on 09/02/2009

koofers-user-cxk
koofers-user-cxk 🇺🇸

10 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java:
Learning to Program with Robots
Chapter 06: Using Variables
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download Java Programming: Creating and Managing a SimpleBot Object and more Study notes Computer Science in PDF only on Docsity!

Java:Learning to Program with RobotsChapter 06: Using Variables

Chapter Objectives

After studying this chapter, you should be able to:

Add new instance variables to a simple version of the

Robot

class.

Store the results of calculations in temporary variables and usethose results later in the method.

Write methods using parameter variables.

Use constants to write more understandable code.

Explain the differences between instance variables, temporaryvariables, parameter variables, and constants.

Extend an existing class with new instance variables.

6.1.1: Implementing Attributes with Inst. Vars.

A simplified version of

Robot

, called

SimpleBot

Paintable

+void paint(Graphics2D g)

SimpleBot

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.

6.1.2: Declaring 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!

} Four key parts to an instance variable declaration:

An access modifier; use

private

except in

rare

circumstances.

A type such as

int

to store integers or

String

to store a string of

characters.

A descriptive name for the variable.

An initial value, placed after an equal sign.

6.1.3: Accessing Instance Variables

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)

6.1.4: Modifying Instance Variables

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(){ } }

6.1.4: Modifying Instance Variables

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(){ } }

6.1.5: Testing the SimpleBot Class

/** 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(); } }

6.1.6: Adding Direction

…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;} } }

6.1.6: Adding Direction

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;} } }

6.1.7: Providing Accessor Methods

An

accessor method

answers the question “What value does attribute

X

currently hold?”In general:

public

«typeReturned»

get

«Name»

{ return this.

«instanceVariable»

For example: public class SimpleBot extends Paintable{

private int avenue = 2;… public int getAvenue(){ return this.avenue;} … }

6.1.8: Instance Variables vs. Other Variables

Instance variables, temporary variables, and parameter (variables) allstore information. Instance variables are different in the followingways.

Instance variables are declared inside a class but outside of allmethods. Parameter and temporary variables are declared inside amethod.

Instance variables have a larger scope – the entire class. Parameterand temporary variables have a scope no larger than a method.

Instance variables have a longer lifetime – the same as the objectthat contains them. Parameter and temporary variables disappearwhen their method finishes executing.

Case Study 1: Calculating Values

(bodyX-15, bodyY-15)^ (bodyX, bodyY)

(sensorX-6, sensorY-6)(sensorX, sensorY)

(this.avenue * 50, this.street * 50)

Case Study 1: Calculating bodyX, bodyY

int bodyX = this.avenue * 50 + 50 / 2;

Size of each intersection^ Robot's current position

Half an intersection

Distance from origin to robot's centerReplace

with a more meaningful (but short) name:

int iSize = Constants.INTERSECTION_SIZE;int bodyX = this.avenue * iSize + iSize / 2;

Assuming the robot is on Avenue 2, we have:

this.avenue

*****^

iSize

+^

iSize

/^

2 int^2

int^50

int 100

int^50

int^2

int^25

int 125