Announcements Announcements Reading for next Wednesday (Sep 7) - - PowerPoint PPT Presentation

announcements announcements
SMART_READER_LITE
LIVE PREVIEW

Announcements Announcements Reading for next Wednesday (Sep 7) - - PowerPoint PPT Presentation

Announcements Announcements Reading for next Wednesday (Sep 7) Reading for next Wednesday (Sep 7) Chapter 3 Check Check http://www.itk.ilstu.edu/faculty/kwsuh/courses/ ITK168/Fall2011/index.htm ITK168/Fall2011/index.htm


slide-1
SLIDE 1

Announcements Announcements

  • Reading for next Wednesday (Sep 7)

Reading for next Wednesday (Sep 7)

– Chapter 3 Check – Check http://www.itk.ilstu.edu/faculty/kwsuh/courses/ ITK168/Fall2011/index.htm ITK168/Fall2011/index.htm – http://www.itk.ilstu.edu/itk168

  • Quiz#1
  • Quiz#1

– Clean up your desk and get read for quiz#1

slide-2
SLIDE 2

Recap: Class Details Recap: Class Details

  • Attributes

Al i t ( ) – Always private (-) – Name – Type

  • Constructors

Constructors

– Always public (+) – Used to build objects – No return type S th l – Same name as the class – May or may not have parameter list

  • Default constructor
  • Special constructor
  • Services

– Public (+), private (-), or protected (#) – Name Parameter list – Parameter list – Return type

slide-3
SLIDE 3

Recap: Messages Recap: Messages

Argument list “dot” Argument list aka: parameter list

karel.move();

semicolon Reference to the

  • bject

The service to execute semicolon j aka: variable aka: method

slide-4
SLIDE 4

Recap: Reading Documentation Recap: Reading Documentation

  • www.learningwithrobots.com

– www.learningwithrobots.com/doc/index.html

  • http://download.oracle.com/javase/6/docs/api/ or
  • http://download.oracle.com/javase/7/docs/api/

p j p

slide-5
SLIDE 5

Recap: Basic Java Patterns Recap: Basic Java Patterns

  • Java Program Pattern

Java Program Pattern

  • Object Instantiation Pattern

C d I ti P tt

  • Command Invocation Pattern
  • Sequential Execution Pattern
slide-6
SLIDE 6

Planting Flowers

  • You have a square enclosure. You want to

have a robot plant flowers (Things) around the enclosure See below the enclosure. See below. Questions: Ho man alls are there? Ho are the positioned? How many walls are there? How are they positioned? Where do the flowers come from?

slide-7
SLIDE 7

Extending Classes with Services Extending Classes with Services IT 168 Fall 2011

Robots Learning to Program with Java Learning to Program with Java Byron Weber Becker

h chapter 2

slide-8
SLIDE 8

Chapter 2 objectives Chapter 2 objectives

  • Extend an existing class with new

Extend an existing class with new commands

  • Explain how a message sent to an object
  • Explain how a message sent to an object

is resolved to a particular method U i h it d i

  • Use inherited services
  • Override services in the superclass
  • Java programming convention
slide-9
SLIDE 9

Recap: Sketch Before and After Recap: Sketch Before and After

import becker.robots.*; public class Longer p g { public static void main(String[] args) { City austin = new City(); Robot lisa = new Robot(austin, 3, 2, Direction.EAST); li () lisa.move(); lisa.move(); lisa.move(); lisa.turnLeft(); lisa.turnLeft(); lisa turnLeft(); lisa.turnLeft(); lisa.move(); lisa.move(); lisa.move(); lisa.turnLeft(); lisa.turnLeft(); lisa.move(); lisa.move(); lisa.move(); lisa.turnLeft(); lisa.move(); li () lisa.move(); lisa.move(); lisa.turnLeft(); lisa.turnLeft(); } }

slide-10
SLIDE 10

Robot vs ExperimentalRobot Robot vs ExperimentalRobot

Robot

Experimental Robot

  • street:int
  • avenue:int
  • direction:Direction
  • packpack:ThingBag

Robot

  • street:int
  • avenue:int
  • direction:Direction
  • packpack:ThingBag

+ Robot(City aCity, int aStreet, int anAvenue, Direction aDirection) + mo e()

  • id

+ ExperimentalRobot(City aCity, int aStreet, int anAvenue, Direction aDirection) + mo e()

  • id

+ move():void + turnLeft():void + pickThing():void + putThing():void + move():void + move3():void + turnLeft():void + turnRight():void + putThing():void + turnRight():void + turnAround():void + pickThing():void + putThing():void + putThing():void

slide-11
SLIDE 11

Recap: Sketch Before and After Recap: Sketch Before and After

import becker.robots.*; public class Shorter { public static void main(String[] args) p ( g[] g ) { City austin = new City(); ExperimentalRobot lisa = new ExperimentalRobot(austin, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); lisa.move3(); lisa.turnLeft(); lisa.move3(); (); lisa.turnAround(); } }

slide-12
SLIDE 12

Abstraction Abstraction

  • Raising the level of abstraction creates a

Raising the level of abstraction creates a more natural programming language

Makes programs easier to write – Makes programs easier to write – Allows us to understand programs better Makes programs easier to debug – Makes programs easier to debug – Makes programs easier to modify C t bl d – Creates reusable code

slide-13
SLIDE 13

Vocabulary Vocabulary

  • Extends or inherits

Extends or inherits

  • Superclass or parent class

S b l hild l

  • Subclass or child class
  • Superclasses (plural) Grandparent or

great-grandparent

  • In a class diagram the Superclass is

g p shown above the subclass with arrow pointing from subclass to superclass p g p

  • This represents an “IS-A” relationship
slide-14
SLIDE 14

Extending Robot Extending Robot

Robot

  • street:int
  • avenue:int
  • avenue:int
  • direction:Direction
  • packpack:ThingBag

+ Robot(City aCity, int aStreet, int anAvenue, Direction aDirection) + move():void + turnLeft():void + pickThing():void + putThing():void

E i t lR b t ExperimentalRobot

+ ExperimentalRobot(City aCity, int aStreet, int anAvenue, Direction aDirection) + turnAround():void + turnRigth():void + move3():void

slide-15
SLIDE 15

Form of an Extended Class Form of an Extended Class

import <<importedPackage>>; public class <<ClassName>> extends <<SuperClass>> { <<list of attributes used by this class>> <<list of attributes used by this class>> <<list of constructors for this class>> <<list of services provided by this class>> }

slide-16
SLIDE 16

Constructor of Subclass Constructor of Subclass

public ExperimentRobot(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); }

  • super refers to the superclass

– In this case Robot

slide-17
SLIDE 17

this this

  • Every method has at least one automatic

Every method has at least one automatic parameter

The calling object – The calling object – Not passed in the parameter list

Inside a method thi represents the

  • Inside a method, this represents the

calling object // move the calling object this.move();

slide-18
SLIDE 18

Subclass Methods Subclass Methods

  • turnAround()

turnAround()

  • move3()

t Ri ht()

  • turnRight()
slide-19
SLIDE 19

Flow of a Java Program Flow of a Java Program

  • Write a main method to declare an

Write a main method to declare an ExperimentalRobot and do the following:

Move one block – Move one block – Turn around Move 3 blocks – Move 3 blocks – Turn left M t bl k – Move two blocks – Turn right

slide-20
SLIDE 20

Extension vs Modification Extension vs Modification

  • The Robot class was created by the

The Robot class was created by the author and only the compiled class (byte code) is shared code) is shared

– The Robot class is closed for modification

The Robot class is programmed in a way

  • The Robot class is programmed in a way

to allow users to extend the class

Th R b t l i f t i – The Robot class is open for extension

slide-21
SLIDE 21

The Thing Class The Thing Class

  • Explore the documentation

– http://www.learningwithrobots.com/doc/index.html

  • Change the appearance of a Thing

Thing deceptiveThing = new Thing(aCity, 3, 4); WallIcon anIcon = new WallIcon(); WallIcon anIcon new WallIcon(); deceptiveThing.setIcon(anIcon);

slide-22
SLIDE 22

In Sec2.3 you are asked to Complete this Cl Class

import becker.robots.*; public class Lamp extends Thing{ public Lamp(City myCity int street int ave) { public Lamp(City myCity, int street, int ave) { … } public void turnOn() { … } public void turnOff() { … } }

slide-23
SLIDE 23

The Constructor The Constructor

  • Call to super

Call to super

  • Might add additional information
  • A street light is off during the day and on

at night – you need to decide which should be default

  • Code it

Code it

slide-24
SLIDE 24

The Lamp Class The Lamp Class

  • What does a Thing look like?

What does a Thing look like?

  • What should a Lamp look like?

Wh t h ld b th diff b t

  • What should be the difference between a

Lamp that is on and one that is off?

  • How do you change the appearance of a

Thing?

slide-25
SLIDE 25

Creating a Color Object Creating a Color Object

  • public Color(int r int g int b int a)

public Color(int r, int g, int b, int a)

– Creates an sRGB color with the specified red, green blue and alpha values in the range (0 - green, blue, and alpha values in the range (0 255). – Parameters: Parameters:

  • r - the red component
  • g - the green component
  • b - the blue component
  • a - the alpha component

Color onColor = new Color(255, 255, 200, 150);

slide-26
SLIDE 26

Is the Lamp On or Off? Is the Lamp On or Off?

public void turnOn() { Color onColor = new Color(255, 255, 200, 150); CircleIcon onIcon = new CircleIcon(onColor); this.setIcon(onIcon); }

  • How would the turnOff method differ?
  • How would the turnOff method differ?
slide-27
SLIDE 27

The Lamp Class The Lamp Class

import java.awt.Color; import becker robots *; import becker.robots. ; import becker.robots.icons.CircleIcon; public class Lamp extends Thing{ public Lamp(City myCity, int ave, int street) { super(myCity, ave, street); this.turnOff(); } public void turnOn() { Color onColor = new Color(255, 255, 200, 150); CircleIcon onIcon = new CircleIcon(onColor); this.setIcon(onIcon); } public void turnOff() { Color offColor = new Color(0, 0, 0); CircleIcon offIcon = new CircleIcon(offColor, .25); this.setIcon(offIcon); } }

slide-28
SLIDE 28

Testing the Lamp Class Testing the Lamp Class

import becker.robots.*; public class LampTest { p p { public static void main(String[] args) { // set up city City myCity = new City(); Lamp lamp1 = new Lamp(myCity, 1, 0); Lamp lamp2 = new Lamp(myCity, 1, 1); hi d hi ( i 1 2) Thing dot = new Thing(myCity, 1, 2); SERobot karl = new SERobot(myCity, 0, 0, Direction.EAST); // turn one lamp on the other off lamp1.turnOn(); lamp2.turnOff(); // pick up one lamp // pick up one lamp karl.turnRight(); karl.move(); karl.pickThing(); // place on next lamp karl.turnLeft(); karl.move(); karl.putThing(); // pick up thing karl.move(); karl.pickThing(); // t thi l // put thing on lamp karl.turnAround(); karl.move(); karl.putThing(); } }

slide-29
SLIDE 29

Programming Style Programming Style

  • Whitespace

p

  • Indentation
  • Naming conventions

P k – Packages – Classes – Methods – Variables – Constants

  • Comments
  • Comments

– Single line – Multi-line – Javadoc

slide-30
SLIDE 30

Closed for Modification Closed for Modification

  • How can I change the Robot methods

How can I change the Robot methods

– move, turnLeft, etc

Suppose I want to create a Flying Robot

  • Suppose I want to create a Flying Robot

– One that moves very quickly – What changes might I make

slide-31
SLIDE 31

Overriding the move Method Overriding the move Method

  • The speed of a Robot is the number of moves it

The speed of a Robot is the number of moves it makes in one second

  • The default value of a Robot’s speed is 2

p

  • This can be changed by using this Robot

method

public void setSpeed(double movesPerSecond)

  • Override the move method for our FlyingBot so it

tra els 20 blocks per second travels 20 blocks per second

  • We want the robot to turn at a normal speed so

you must reset the speed to default at the end of you must reset the speed to default at the end of the move method

slide-32
SLIDE 32

FlyingBot that Walks FlyingBot that Walks

  • My FlyingBot typically flies but

My FlyingBot typically flies, but

  • ccasionally it might want to walk a few

blocks (just for exercise) blocks (just for exercise)

  • Write a walk method that moves at the

default speed default speed

  • What happens if we do not reset the

speed in the overridden move method

slide-33
SLIDE 33

Redesigning the FlyingBot Redesigning the FlyingBot

  • How quickly will our FlyingBot turn

How quickly will our FlyingBot turn

  • I’ve changed my mind. I want the

FlyingBot to do everything fast (as a FlyingBot to do everything fast (as a default setting) and only slow down if I choose to call a slower method choose to call a slower method

– walk t Sl – turnSlow

  • How would my design change
slide-34
SLIDE 34

Redesigning PlantingFlowers Redesigning PlantingFlowers

  • Change the PlantingFlowers practice

Change the PlantingFlowers practice exercise from the chapter 1 slides to use a

Garden class that extends City and starts out – Garden class that extends City and starts out with the walls already created – GardenerBot class that extends RobotSE GardenerBot class that extends RobotSE

  • Automatically start the Robot at (0,1) facing south
  • Automatically start the Robot with enough flowers

y g to complete the garden

slide-35
SLIDE 35

ColorBot ColorBot

  • Create a RobotSE that draws attention to

Create a RobotSE that draws attention to itself by changing color when it moves

  • Typically the ColorBot will be red (like all
  • Typically the ColorBot will be red (like all

Robots), but when the ColorBot moves it should change color (you choose a color) should change color (you choose a color)

  • Write a main method to test the ColorBot

l class