Announcements Announcements Reading for Wednesday Reading for - - PowerPoint PPT Presentation

announcements announcements
SMART_READER_LITE
LIVE PREVIEW

Announcements Announcements Reading for Wednesday Reading for - - PowerPoint PPT Presentation

Announcements Announcements Reading for Wednesday Reading for Wednesday the rest of Chapter 2 (with a few exceptions) Check Check http://www.itk.ilstu.edu/faculty/kwsuh/courses/ ITK168/Fall2011/index.htm


slide-1
SLIDE 1

Announcements Announcements

  • Reading for Wednesday

Reading for Wednesday

– the rest of Chapter 2 (with a few exceptions) 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
  • Quiz
slide-2
SLIDE 2

Object-oriented vs. non-OO programming

  • Demo

Demo

slide-3
SLIDE 3

Software Objects Software Objects

  • Maintain information called attributes

Maintain information called attributes

  • Answer questions based on that

information using queries information using queries

  • Change the information using commands
  • Queries and commands are collectively

called services

  • Services are provided to clients
slide-4
SLIDE 4

Objects Objects

  • Based on a “template” called a class

Based on a template called a class

  • Have the same attributes and services

Att ib t h diff t l

  • Attributes have different values
  • Services are the same for all objects of the

same class

slide-5
SLIDE 5

Class Definitions Class Definitions

  • This is what we code in a Java program

This is what we code in a Java program

  • Used to create multiple objects

C ll d i t f th l – Called instances of the class

  • Specifies attributes and services
  • Class – vs – Object

– Blueprint – house p – Pattern – dress – Cookie cutter - cookie

slide-6
SLIDE 6

Queries and Attributes Queries and Attributes

  • A query is always answered by the object

A query is always answered by the object

– True or false A number – A number – A string of characters

A b d th bj t’

  • Answers are based on the object’s

attributes (therefore possible questions li it d) are limited)

slide-7
SLIDE 7

Commands Commands

  • Change the value of an attribute (or

Change the value of an attribute (or attributes) to reflect a new reality

  • Can be visualized using a state of
  • Can be visualized using a state of

change diagram

St t f th bj t b f – State of the object before – State of the object after

slide-8
SLIDE 8

Class Diagram Class Diagram

  • Rectangle with 3 sections

Rectangle with 3 sections

– Name of class List of attributes – List of attributes – List of services

  • Sometimes class diagrams do not list the

attributes

– Black-box programming

slide-9
SLIDE 9

Modeling Robots Modeling Robots

  • Book author provides many classes in a

Book author provides many classes in a package called – becker.jar

  • Basic enough to grasp easily
  • Basic enough to grasp easily
  • Complex enough to be interesting
  • Simple enough to be easy to program
  • Rich enough to demonstrate OOP

g concepts

slide-10
SLIDE 10

Incomplete Class Diagram Incomplete Class Diagram

Robot

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

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

slide-11
SLIDE 11

Class Details 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-12
SLIDE 12

Services Services

  • A robot performs a service only when it is

p y invoked by a corresponding message

– turnLeft – turns 90 degrees to the left remaining on the same intersection the same intersection

  • Turning is a safe activity

– move – attempts to move forward one intersection still facing in the same direction – pickThing – attempts to pick up a thing from the current intersection – putThing – attempts to put a thing down in the current intersection

  • Moving picking up and putting down objects are not always
  • Moving, picking up, and putting down objects are not always

safe activities

slide-13
SLIDE 13

Messages 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-14
SLIDE 14

Example Program Example Program

  • Situation

Situation

– A delivery robot is to pick up a parcel, represented by a Thing at intersection (1 2) represented by a Thing, at intersection (1,2) and deliver it to (2,3) Initial state Final state

slide-15
SLIDE 15

The Java Program The Java Program

import becker.robots.*; public class DeliverParcel { pub c c ass e e a ce { public static void main(String[] args) { //Set up the initial situation City prague = new City(); Thing parcel = new Thing(prague, 1, 2); g p g(p g , , ); Robot karel = new Robot(prague, 1, 0, Direction.EAST); //Direct the robot to the final situation karel.move(); (); karel.move(); karel.pickThing(); karel.move(); karel.turnLeft(); //start turning right as 3 lefts (); // g g karel.turnLeft(); karel.turnLeft(); //finished turning right karel.move(); karel.putThing(); p g(); karel.move(); } }

slide-16
SLIDE 16

Sketch the initial state and final state of this program

import becker.robots.*; public class GoAroundRoadBlock p { public static void main(String[] args) { //set up initial situation City ny = new City(); Wall blockAve0 = new Wall(ny, 0, 2, Direction.WEST); ll bl k 1 ll( 1 2 i i ) Wall blockAve1 = new Wall(ny, 1, 2, Direction.WEST); Robot mark = new Robot(ny, 0, 2, Direction.WEST); Robot ann = new Robot(ny, 0, 1, Direction.EAST); //what does mark do here? mark turnLeft(); mark.turnLeft(); mark.move(); mark.move(); mark.turnLeft(); mark.turnLeft(); mark.turnLeft(); mark.move(); //what does ann do here? ann.turnLeft(); ann.turnLeft(); t L ft() ann.turnLeft(); ann.move(); ann.move(); ann.turnLeft(); } }

slide-17
SLIDE 17

Reading Documentation 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-18
SLIDE 18

Basic Java Patterns 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-19
SLIDE 19

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-20
SLIDE 20

Beyond Robots Beyond Robots

  • Objects are used in all contexts in Java

Objects are used in all contexts in Java programs.

  • The same patterns are used to develop
  • The same patterns are used to develop

GUI (Graphical user interface) programs as to develop Robot programs as to develop Robot programs.

  • However, the classes we use are different.
slide-21
SLIDE 21

Sample Simple GUI

  • Use the JFrame, JLabel, JTextField, and

JTextArea classes to produce a program that g creates a window that looks like a browser window (sort of).

slide-22
SLIDE 22

End of Chapter End of Chapter

  • Be sure to study the patterns listed at the

Be sure to study the patterns listed at the end of each chapter

  • You will be expected to understand them
  • You will be expected to understand them
  • Likewise, study the concept map at the

d f h h t end of each chapter

  • Download and complete the exercise set

posted on the course web site

slide-23
SLIDE 23

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-24
SLIDE 24

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-25
SLIDE 25

Sketch Before and After 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-26
SLIDE 26

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-27
SLIDE 27

Sketch Before and After 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-28
SLIDE 28

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-29
SLIDE 29

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-30
SLIDE 30

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-31
SLIDE 31

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-32
SLIDE 32

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-33
SLIDE 33

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-34
SLIDE 34

Subclass Methods Subclass Methods

  • turnAround()

turnAround()

  • move3()

t Ri ht()

  • turnRight()
slide-35
SLIDE 35

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-36
SLIDE 36

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-37
SLIDE 37

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(); deceptiveThing.setIcon(anIcon);

slide-38
SLIDE 38

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-39
SLIDE 39

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-40
SLIDE 40

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-41
SLIDE 41

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-42
SLIDE 42

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-43
SLIDE 43

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-44
SLIDE 44

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-45
SLIDE 45

Programming Style Programming Style

  • Whitespace

p

  • Indentation
  • Naming conventions

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

  • Comments
  • Comments

– Single line – Multi-line – Javadoc

slide-46
SLIDE 46

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-47
SLIDE 47

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-48
SLIDE 48

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-49
SLIDE 49

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-50
SLIDE 50

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-51
SLIDE 51

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