1/28/2008ITK 1681 An enhanced robot Robot int street int avenue Direction direction ThingBag backback Robot(City aCity, int aStreet, int anAvenue, Direction.

Slides:



Advertisements
Similar presentations
2/18/2008ITK 1681 Feb. 20, Wed. 8:00 – 9:30 pm STV Closed book, notes, and no computer is allowed. 2.Everyone is allowed to bring a self- prepared.
Advertisements

Chapter 3 Extending the Robot Programming Language.
1 Karel J Robot-Lesson 3 NTSD-Mr. Moon Karel J Robot Lesson 3 A Gentle Introduction to the Art of Object-Oriented Programming in Java.
Polymorphism Are there different ways to solve the Harvester problem? – Robot teams – instead of one robot to solve a problem, let’s get a team of robots.
Robot? What’s a Robot? Introducing Karel-the-Robot.
Lab 1 City, Robot, Thing, Wall. Documentation of Classes and Methods.
Extending the Robot Programming Language In the Robot world 1 mile = 8 blocks Suppose we want a robot to run a marathon (26+ miles)? Does our program have.
Lecture 6a Instructor: Craig Duckett. Upcoming Assignments & Mid-Term Assignment 1 Revision Assignment 1 Revision is due NEXT Wednesday, July 29 th, by.
9/12/2015IT 2751 IDE ( Integrated Development Environment ) 1.A customized plain text editor 2.Compiler 3.Loader 4.Debugging tool JDK (Java Development.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you.
1 Note: Original slides provided by and modified for Mr. Heath’s AP Computer Science A classwww.apComputerScience.com.
1 Karel J Robot OOP approach to learning computer science “Its study involves development of the ability to abstract the essential features of a problem.
1/16/2008ITK 1681 HardwareSoftware Theory 1800 AD  Architecture 1945 AD  What is Computer Science? Languages 1960 AD 
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 4.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Wednesday, January 20 th one week from today I will.
For Friday Finish reading chapter 2 Complete WebCT quiz.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
THINKING PROCEDURALLY OR CONCURRENT (SIMULTANEOUSLY OR SIDE BY SIDE OR IN LOGICAL ORDER A- IDENTIFY THE COMPONENTS OF A PROBLEM B-IDENTIFY THE COMPONENTS.
Unified Modeling Language (UML)
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Lecture 11 Instructor: Craig Duckett Instance Variables.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
For Monday Read Becker, chapter 4, sections 1 and 2.
BIT 115: Introduction To Programming L ECTURE 3 Instructor: Craig Duckett
BIT115: Introduction to Programming
Chapter 3 Extending the Robot Programming Language.
Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
Lecture 13 Instructor: Craig Duckett. Assignment 2 Revision DUE TONIGHT! November 9 th DUE TONIGHT! November 9 th Uploaded to StudentTracker by midnight.
GC211 Data structure Lecture 3 Sara Alhajjam.
Algorithms and Problem Solving
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Robot Class name Attributes Constructor Services, methods
BIT115: Introduction to Programming
Copyright © 2008 by Helene G. Kershner
BIT115: Introduction to Programming
Copyright © 2008 by Helene G. Kershner
Karel J Robot.
Polymorphism and Observers
CS 106A, Lecture 2 Programming with Karel
Karel J Robot OOP approach to learning computer science
BIT115: Introduction to Programming
Unit# 9: Computer Program Development
Stepwise Refinement Eric Roberts CS 106A January 8, 2010.
Lecture 8-3: Encapsulation, this
ITK 168 Lecture 5 - Inheritance
Object initialization: constructors
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
import becker.robots.City;
Karel J Robot OOP approach to learning computer science
Ch.3 Classes & Stepwise Refinement
Unit 1 Lab16.
ITK 168 Section 13 Dr. Doug Twitchell.
Unit 1 Lab14 & Lab15.
slides courtesy of Eric Roberts
Computational Thinking
Representation and Management of Data on the Internet
Branching Statement Condition Statement Condition list 1 list
IDE (Integrated Development Environment)
Quiz: Computational Thinking
CSC 205 – Java Programming II
Presentation transcript:

1/28/2008ITK 1681 An enhanced robot Robot int street int avenue Direction direction ThingBag backback Robot(City aCity, int aStreet, int anAvenue, Direction aDir) void move() void turnLeft() void pickThing() void putThing() ExperimentRobot ExperimentRobot(City aCity, int aStreet, int anAvenue, Direction aDir) void turnAround(); void turnRight(); void move3(); = ExperimentRobot int street int avenue Direction direction ThingBag backback ExperimentRobot(City aCity, int aStreet, int anAvenue, Direction aDir) void move() void turnLeft() void pickThing() void putThing() void turnAround(); void turnRight(); void move3();

1/28/2008ITK 1682 import becker.robots.*; public class ExperimentRobot extends Robot { // Construct a new ExperimentRobot. public ExperimentRobot(City theCity, int aStreet, int anAvenue, Direction aDirection) { super(theCity, aStreet, anAvenue, aDirection); } public void turnAround() { this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); }

1/28/2008ITK 1683 import becker.robots.*; public class DeliverParcel { public static void main(String[] args) { // Set up the initial situation City prague = new City(); Thing parcel = new Thing(prague, 1, 2); Robot karel = new Robot(prague, 1, 0, Direction.EAST); ExperimentRobot mark = new ExperimentRobot(prague, 1,4, Direction.EAST);.... k.turnAround(); k.move3();.... } Note: the new robot class must be defined in the same project We can extend City, Thing, and any Classes...

1/28/2008ITK 1684 Robot ExperimentRobot void turnAround(); void turnRight(); void move3(); A class diagram can be very simple, flexible, and abstract Object Shape CircleRectangle RectanglePrismCylinder {abstract}

1/28/2008ITK 1685 Thing Lamb void: turnOn() void: turnOff A class diagram can be very simple and abstract implementation details may not necessary

1/28/2008ITK 1686 import becker.robots.*; import becker.robots.icons.*; import java.awt.Color; public class Lamp extends Thing { public Lamp(City aCity, int aStreet, int anAvenue) { super(aCity, aStreet, anAvenue); this.turnOff(); } public void turnOn(){ Color onColor = new Color(255, 255, 200); CircleIcon onIcon = new CircleIcon(onColor); onIcon.setSize(0.75); onIcon.setTransparency(0.5); this.setIcon(onIcon); } public void turnOff() { Color offColor = new Color(0, 0, 0); CircleIcon offIcon = new CircleIcon(offColor); offIcon.setSize(0.25); this.setIcon(offIcon); } You need to study Documents

1/28/2008ITK 1687 import becker.robots.*; public class Main { public static void main(String[] args) { // Construct the initial situation. City paris = new City(); Lamp lamp1 = new Lamp(paris, 1, 1); Lamp lamp2 = new Lamp(paris, 2, 1); Robot lampMover = new Robot(paris, 1, 0, Direction.EAST); // Turn one lamp on and the other off. lamp1.turnOn(); lamp2.turnOff(); // Use the robot to move one of the lamps. lampMover.move(); lampMover.pickThing(); lampMover.move(); lampMover.putThing(); lampMover.move(); }

1/28/2008ITK 1688 If a problem is computable, then we can solve it with an algorithm. When we have an algorithm, then we implement with a program.

1/28/2008ITK 1689 An algorithm: A finite sequence of operations that will lead to a solution A step by step operations

1/28/2008ITK A big problem can be decomposed into a sequence of small problems. We shall refine our problem into smaller problems step by step until we reach the elementary problem. Stepwise refinement: that makes our solution 1.more logical, easier to understand 2.easier to debug 3.easier to modify 4.easier to analyze 5.more portable

1/28/2008ITK Message to the space

1/28/2008ITK WriteBot: a robot that can light Thing We need a robot that can write an Hwrite an Ewrite an Lwrite an O

1/28/2008ITK enhanced robots Robot int street int avenue Direction direction ThingBag backpack void move() void turnLeft() void pickThing() void putThing() RobotSE void turnAround(); void turnRight(); WriteBot void putBonfire() void writeH() void writeE(); void writeL(); void writeO(); void nextPosition();

1/28/2008ITK Specifying an operation (service) void putBonfire() void writeH() void writeE(); void writeL(); void writeO(); void nextPosition(); For each operation: 1.operation description 2.pre-conditions 3.post-conditions 4.returns 5.exceptions void writeH() void nextPosition()

1/28/2008ITK import becker.robots.*; public class WriteBot extends RobotSE { // Constructor..... public void putBonfire(){ this.putThing(); }..... } How do I know the backpack has Things (Bonfire) to put down? Make it enough in the constructor...

1/28/2008ITK import becker.robots.*; public class WriteBot extends RobotSE { // Construct a new WriteBot with 50 Things in // it backpackRobot. public WriteBot(City theCity, int avenue, int street, Direction aDirection) { super(theCity, avenue, street, aDirection, 50); }..... } Constructor Summary RobotRobot(City aCity, int aStreet, int anAvenue, Direction aDirection) Construct a new Robot at the given location in the given city with noThing in its backpack.CityDirection RobotRobot(City aCity, int aStreet, int anAvenue, Direction aDirection, int numThings) Construct a new Robot at the given location in the given city with the given number of Things in its backpack.CityDirection Again, we learn from Documents

1/28/2008ITK import becker.robots.*; public class WriteBot extends RobotSE {..... public void nextPosition() { this.move();this.move();this.move(); this.move(); }.... public void writeH(){ turnRight(); putBonfire(); move(); putBonfire(); move(); putBonfire(); move(); putBonfire(); turnLeft(); move(); move(); turnLeft(); putBonfire(); move(); putBonfire(); move(); putBonfire(); move(); putBonfire(); turnAround(); move(); move(); turnRight(); move(); putBonfire(); move(); turnRight(); move(); move(); turnRight(); }.... }

1/28/2008ITK import becker.robots.*; public class Message { public static void main(String[] args) { // Set up the initial situation City houston = new City(); WriteBot mark = new WriteBot(houston, 1, 1, Direction.EAST); mark.writeH(); mark.nextPosition(); mark.writeE(); mark.nextPosition(); mark.writeL(); mark.nextPosition(); mark.writeL(); mark.nextPosition(); mark.writeO(); }

1/28/2008ITK import becker.robots.*; public class Message { public static void main(String[] args) { // Set up the initial situation City houston = new City(); WriteBot markH = new WriteBot(houston, 1, 1,Direction.EAST); WriteBot markE = new WriteBot(houston, 5, 5,Direction.EAST); WriteBot markL = new WriteBot(houston, 9, 9,Direction.EAST); WriteBot markL2= new WriteBot(houston, 1,13,Direction.EAST); WriteBot markO = new WriteBot(houston, 1,17,Direction.EAST); markH.writeH(); markE.writeE(); markL.writeL(); markL2.writeL(); markO.writeO(); }

1/28/2008ITK Example of refinement Harvesting task  This is the field to harvest

1/28/2008ITK Pseudo code for the harvesting task 1.create a harvester (a kind of robot) 2.step in the field 3.harvest the field 4.step out the field This way, we have refined the harvesting task into four smaller tasks, and now we are able to translate the pseudo code into Java.

1/28/2008ITK import becker.robots.*; /*****************************************************/ /* A program to harvest a 6 by 5 field of things */ /* wide and 6 rows high. */ /*****************************************************/ public class HarvestTask { public static void main (String[] args) { City stLouis = new City("Field.txt"); Harvester mark = new Harvester (stLouis, 1, 0, Direction.EAST); mark.move (); mark.harvestField(); mark.move (); }

1/28/2008ITK Inheritance again! Robot int street int avenue Direction direction ThingBag backpack void move() void turnLeft() void pickThing() void putThing() RobotSE void turnAround(); void turnRight(); Harvester void harvestField(); This harvester is a bit silly, since it can only work on a 6 by 5 field.

1/28/2008ITK Harvesting a 6 by 5 field: Plan A harvest a row from west to east 2.move south 3.harvest a row from east to west 4.move south 5.harvest a row from west to east 6.move south 7.harvest a row from east to west 8.move south 9.harvest a row from west to east 10.move south 11.harvest a row from east to west Is this a good plan?

1/28/2008ITK Harvesting a 6 by 5 field: Plan B harvest two rows 2.position next row 3.harvest two rows 4.position next row 5.harvest two rows This is a better plan since we do generate fewer subtasks. (there are only two involved) This way, we have refined the task of harvest field into smaller tasks, and now we are able to translate the pseudo code into Java.

1/28/2008ITK import becker.robots.*; public class Harvester extends RobotSE { public Harvester(City aCity, int str, int ave, Direction dir) { super(aCity, str, ave, dir); } public void harvestField() { this.harvestTwoRows(); this.positionForNextHarvest(); this.harvestTwoRows(); this.positionForNextHarvest(); this.harvestTwoRows(); }..... } We now turn to refine the subtasks; there are two.

1/28/2008ITK import becker.robots.*; public class Harvester extends RobotSE {.... private void positionForNextHarvest() { this.turnLeft(); this.move(); this.turnLeft(); }.... }

1/28/2008ITK import becker.robots.*; public class Harvester extends RobotSE {.... protected void harvestTwoRows() { this.harvestOneRow(); this.goToNextRow(); this.harvestOneRow(); } private void goToNextRow() { this.turnRight(); this.move(); this.turnRight(); }..... } Refine harvest two rows.

1/28/2008ITK import becker.robots.*; public class Harvester extends RobotSE {.... protected void harvestOneRow() { this.harvestIntersection(); this.move(); this.harvestIntersection(); this.move(); this.harvestIntersection(); this.move(); this.harvestIntersection(); this.move(); this.harvestIntersection(); } protected void harvestIntersection() { this.pickThing(); } } Refine harvest one row.

1/28/2008ITK Harvesting with three harvesters public static void main(String[] args) { City stLouis = new City("Field.txt"); Harvester mark = new Harvester(stLouis,1,0,Direction.EAST); Harvester lucy = new Harvester(stLouis,3,0,Direction.EAST); Harvester greg = new Harvester(stLouis,5,0,Direction.EAST); mark.move(); mark.harvestTwoRows(); mark.move(); lucy.move(); lucy.harvestTwoRows(); lucy.move(); greg.move(); greg.harvestTwoRows(); greg.move(); }

1/28/2008ITK MultiTasking in Java public void run() { this.move(); this.harvestTwoRows(); this.move(); } public class SimHarvester extends RobotSE implements Runnable {..... }

1/28/2008ITK MultiTasking in Java import becker.robots.*; public class SimHarvestTask extends Object { public static void main (String[] args) { City stLouis = new City("Field.txt"); SimHarvester mark = new SimHarvester(stLouis,1,0,Direction.EAST); SimHarvester tom = new SimHarvester(stLouis,3,0,Direction.EAST); SimHarvester john = new SimHarvester(stLouis,5,0,Direction.EAST); Thread t1 = new Thread(mark); t1.start(); Thread t2 = new Thread(tom); t2.start(); Thread t3 = new Thread(john); t3.start(); }