Announcements Announcements For Monday read Becker sections 1 4-1 - - PowerPoint PPT Presentation

announcements announcements
SMART_READER_LITE
LIVE PREVIEW

Announcements Announcements For Monday read Becker sections 1 4-1 - - PowerPoint PPT Presentation

Announcements Announcements For Monday read Becker sections 1 4-1 8 For Monday, read Becker, sections 1.4-1.8 Fill out the Questionnaire in MS Word format and then email it to me (kwsuh@ilstu edu) by and then email it to me


slide-1
SLIDE 1

Announcements Announcements

  • For Monday read Becker sections 1 4-1 8

For Monday, read Becker, sections 1.4-1.8

  • Fill out the Questionnaire in MS Word format

and then email it to me (kwsuh@ilstu edu) by and then email it to me (kwsuh@ilstu.edu) by Friday h //bl kb d il d

  • http://blackboard.ilstu.edu
  • Lab 2

– Course Web Site: http://www.itk.ilstu.edu/itk168 – Section Web Site: http://www.itk.ilstu.edu/faculty/kwsuh/courses/ITK168/Fall2011/index.ht m

slide-2
SLIDE 2

Chapter 1 Objectives Chapter 1 Objectives

  • Describe what models are

Describe what models are

  • Describe the relationship between objects and

classes

  • Understand the syntax and semantics of a

simple Java program p p g

  • Write object-oriented programs that simulate

robots

  • Understand and fix errors in programs
  • Read/use documentation for classes (API)

( )

slide-3
SLIDE 3

Modeling with Objects

Model Information Operations

Concert Who’s performing Performance date Sell a ticket Count tickets sold Which seats are sold Schedule List of task, each with est. Insert or delete a task time Calc est. finish time Restaurant Occupied tables Mark a table Restaurant seating Occupied tables Unoccupied tables # of seats at each table Mark a table

  • ccupied

Mark a table # o seats at eac tab e unoccupied

slide-4
SLIDE 4

Using Models Using Models

  • A model is a simplified description of

A model is a simplified description of something that is used to

– Make decisions – Predict future events – Maintain up-to-date information p – Simulate a process – Answer questions

  • Change over time
  • Form an abstraction
  • Provide operations to be performed
slide-5
SLIDE 5

Abstraction Abstraction

  • The key to dealing with complexity

The key to dealing with complexity

  • Focus only on relevant information

O i d t il i t f l hi h l l

  • Organize details into useful higher-level

chunks

  • Eliminate and hide some details
  • Group similar details together in chunks

p g

slide-6
SLIDE 6

Using Software Objects to Create Models Create Models

  • A program is a detailed set of instructions

A program is a detailed set of instructions

  • Java is an object-oriented language

U ft bj t th t d t – Uses software objects that correspond to an identifiable entity in the problem

slide-7
SLIDE 7

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

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

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

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

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

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

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

Karel’s World Karel s World

  • City – rectangular grid

City rectangular grid

  • Streets – run east and west
  • Avenues – run north and south

Avenues run north and south

  • Streets and avenues start numbering at zero

(typical in programming) (typical in programming)

  • Robots and other objects are placed at the

intersections – listed as ordered pairs te sect o s sted as o de ed pa s

– (1, 2) = street 1, avenue 2 – Multiple robots and things can occupy the same intersection

slide-15
SLIDE 15

Walls and Things Walls and Things

  • Intersections may be surrounded by walls

Intersections may be surrounded by walls

  • Walls block robots

I t ti h “thi ” (l ft

  • Intersections may have “things” (left non-

descript to be identified as needed – b l t t ) paper, box, plant, etc)

  • Typically, things do not block robots
slide-16
SLIDE 16

Robots Robots

  • Exist to serve clients

Exist to serve clients

  • Perform 4 services

– Move Move – Turn – Pick things up Pick things up – Put things down

  • Answer queries about their location and

Answer queries about their location and direction

  • Respond to commands – such as

Respond to commands such as controlling speed

slide-17
SLIDE 17

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

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

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

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

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

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

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

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

Errors Errors

  • Compile-time errors

Compile time errors

– Also known as syntax errors

  • Run-time errors

Run time errors

– Results of instructions executing in an illegal context (e.g., crashing a robot into a wall)

  • Intent errors

– Also known as logic error – occurs when program fails to carry out its intended purpose – no compilation or run-time errors but fail to – no compilation or run-time errors, but fail to accomplish the job

slide-26
SLIDE 26

Compile-time Errors Compile time Errors

  • Program breaks some rules of Java

Program breaks some rules of Java

  • Examples

L i t th i l b – Leaving out parentheses, semi-colons, braces – Misspelling words C ( ) – Capitalization wrong (Java is case-sensitive)

  • Discovered because the compiler points

them out

slide-27
SLIDE 27

Run-time Errors Run time Errors

  • Program compiles correctly and starts to

Program compiles correctly and starts to run but reaches an error state and stops

  • Examples:
  • Examples:

– Robot runs into a wall R b t t i t t thi d h it – Robot tries to put a thing down when it doesn’t have one R b t t i t i k thi b t th ’ – Robot tries to pick something up, but there’s nothing there

slide-28
SLIDE 28

Intent Errors Intent Errors

  • Programs run fine but doesn’t actually

Programs run fine, but doesn t actually work

  • What if we forgot to make a robot turn left
  • What if we forgot to make a robot turn left

in the relay race program? Th t t h d t i d

  • These are not too hard to recognize and

fix in the robot world, but can be quite h ll i i th ki d f challenging in other kinds of programs.

slide-29
SLIDE 29

Software Patterns Software Patterns

  • Intended to help you learn and generalize

Intended to help you learn and generalize.

  • Not to be skipped over and ignored.
slide-30
SLIDE 30

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

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

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

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

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