Karel J Robot An introduction to BlueJ and Object- Oriented Programming.

Slides:



Advertisements
Similar presentations
Introduction to Programming Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
Advertisements

Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Karel – Making More Complex Decisions IF / THEN / ELSE IF THEN BEGIN Instructions END ELSE BEGIN Instructions END Do these when test = False Do these when.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Copyright, Joseph Bergin
Nested If Statements While Loops
1 karel_part4_functions.ppt Functions Functions return values or Objects. –Using a function allows the programmer to focus on other task. –Using a function.
Program: Little Crab Mr Gano.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Computer Science 1620 Loops.
Karel The Robot In the beginning… software. Karel the Robot  All robots are controlled by software  Artificially intelligent robots that can “think”
Robot? What’s a Robot? Introducing Karel-the-Robot.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
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.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
1 karel_part5_loops Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit (known)
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)
1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming.
Karel the Robot A Gentle Introduction to the Art of Programming.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Copyright © Curt Hill Turtles The beginning of media computation.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
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 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
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.
15-100: Introduction to Programming w/ Java * Ananda Gunawardena -- Lecture – School of Computer Science – Phone : (x81559) – Office: Wean Hall.
Iterative Statements: while, for, do-while Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
Karel J. Robot Chapter 6 Instructions That Repeat.
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.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Compiler Errors Syntax error Lexical Can not resolve/find symbol Can not be applied Execution error Oh wait, a run time error Intent error It ran, but.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Alice and Java Unit 7 1. Day 1  Objective: Gain an introduction to Java and Eclipse  Essential skill: DM-1: Use technology to advance critical thinking.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Karel J. Robot Chapter 6 Instructions That Repeat.
Boğaziçi Ünv Koç Ünv Darüşşafaka Lisesi
Karel – Primitive Instructions
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.
In the beginning… software
“Form Ever Follows Function” Louis Henri Sullivan
Eric Roberts and Jerry Cain
Copyright © 2008 by Helene G. Kershner
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Copyright © 2008 by Helene G. Kershner
karel_part4_functions_2
Karel – Primitive Instructions
Karel J Robot.
CS 106A, Lecture 2 Programming with Karel
CS 106A, Lecture 6 Control Flow and Parameters
LRobot Game.
slides courtesy of Eric Roberts
A Gentle Introduction to the Art of Object Oriented Programming
Unit 1 Lab16.
Nested If Statements While Loops
Drawing complex figures
Karel – Primitive Instructions
The beginning of media computation Followed by a demo
Presentation transcript:

Karel J Robot An introduction to BlueJ and Object- Oriented Programming

Objects Modern programs are based on reusable pieces of code known as objects (or class in Java) Each object (class) has certain actions (methods) that it can do In our introduction today we will use the Karel J Robot object (class) which has certain actions (methods)

BlueJ: New Project Please chose “Project” and then “New Project” Name the new project “Karel 1”, place it in your JavaPrograms directory, and choose “Create”

BlueJ: New Class Click on the “New Class” button Name your first object “Robot1” and make it a Karel Object Click “OK”

BlueJ: Robot1 Object Double click on the Robot1 code. It will open the program editor Add the following line to the main method:

BlueJ: Robot1 Object Click on the “Compile” button near the top-left corner of the window If the code’s syntax correct, you should see the following message at the bottom of your window: If you see the message, close the editor window and return to the main BlueJ window

BlueJ: Robot1 Object Right-click on the Robot1 code square and choose “void main(String[] args)” When this window appears, choose “OK”

BlueJ: Robot1 Object The program will open two windows. A command terminal and the Karel J Robot display: You will see our new robot “ren” sitting at (10,1)

BlueJ: Robot1 Object Let’s make our robot move. Please add the following line to your main method: Please compile your program and run it again

BlueJ: Robot1 Object You should see our robot “ren” move forward one space in the direction that it was facing

BlueJ: Robot1 Object Near the top of your program you will see the following line: Note that our Robot1 object extends the UrRobot object When one class extends another, it gains all of the actions (methods) present in the original (super) class

BlueJ: Robot1 Object The UrRobot class has the following five methods: move() turnLeft() putBeeper() pickBeeper() turnOff()

move() forward only, one block “Error Shutoff” if trying to move into a wall (Look first!) – execution error

turnLeft() stay at same corner turn 90 degrees to the left cannot cause an error shutoff

pickBeeper() picks up beeper at current location and places in beeper bag attempted on beeper-less locations causes an error shutoff

putBeeper() take beeper from beeper bag and put down on current location attempted with an empty beeper bag causes an error shutoff

BlueJ: Robot1 Object Please add the following lines to your main method: Run your new program, what does it do?

BlueJ: Robot1 Object Programming task #1 Write a program that places 9 beepers in a line. Please show me when you have it working.

The for loop You probably noticed that you had to use many move() and putBeeper() statements for the last program. Is there a better way?

8-20 The for Loop The for loop is a shorthand that combines in one statement initialization, condition, and change: for ( initialization; condition; change ) { statement1; statement2;... statementN; }

8-21 // Returns the smallest n // such that 2^n >= x public static int intLog2 (int x) { int n = 0, p; for (p = 1; p < x; p * = 2) { n++; } return n; } The for Loop (cont’d) Example: Initialization Testing Change

8-22 The for Loop (cont’d) Java allows you to declare the loop control variable in the for statement itself. For example: for (int i = 0; i < n ; i++) {... } The scope of i is the body of the loop, and i is undefined outside the loop

8-23 The for Loop (cont’d) “Repeat n times” model: or for (int i = 0; i < n ; i++) {... } for (int count = 1; count <= n ; count++) {... }

BlueJ: Robot1 Object Programming task #2 Re-write the program that places 9 beepers in a line using a for loop Please show me when you have it working for (int i = 0; i < n ; i++) {... }

BlueJ: Robot1 Object I would like your robot to turn left at the end of its journey. The turnLeft() method Where should you place the turnLeft() method in your code? What would happen if you did this?

BlueJ: Robot1 Object Please let me know when you succeed

BlueJ: Robot1 Object Our next programming task is to have our robot make a square We will need to repeat our commands four times. How can we do this?

8-28 Nested Loops A loop within a loop is called nested. // Draw a 5 by 3 grid: for (int x = 0; x < 50; x += 10) { for (int y = 0; y < 30; y += 10) { g.fillRect(x, y, 8, 8); }

8-29 Nested Loops (cont’d) Braces are optional when the body of the loop(s) is one statement: for (int x = 0; x < 100; x += 10) for (int y = 0; y < 200; y += 10) g.fillRect(x, y, 8, 8); Inner for is the only statement in the outer for’s body Many programmers prefer to always use braces in loops, especially in nested loops.

BlueJ: Robot1 Object What if we wanted to allow the user to choose the size of our square? Please add the following to the top of your program:

BlueJ: Robot1 Object We are going to use one of the built in GUI classes inside of Java to ask the user for input. Please add the following two lines to your program:

BlueJ: Robot1 Object

Adding new methods to our robot Our UrRobot object has only five basic commands: move(), turnLeft(), putBeeper(), pickBeeper(), and turnOff() It would be nice to create some new commands (methods) for our robot A turnRight() command would be great. How can we create one from the commands that we have?

BlueJ: Robot1 Object You can add new methods easily:

BlueJ: Robot1 Object Please modify your program to create a “clock- wise” square using right turns Please start your robot at (1,1): Please show me your program when you are complete

BlueJ: Robot1 Object We are going to modify our robot’s world for the next few programs to make or robot run faster Please find this code near the bottom of your program and modify it:

BlueJ: Robot1 Object Programming challenge. Modify your program so that the robot fills the screen with beepers as shown: Some code to get you started:

BlueJ: Robot1 Object Here is the complete code: Note: you can use the loop variable i more than once in a program

BlueJ: Robot1 Object Add a nested loop to repeat your code five times so that your robot fills the screen as shown

BlueJ: Robot1 Object Some sample code

Method Parameters It would be nice to make a general method to place beepers

Threading Please add the following import statements: And this line to the top of your program:

Threading Each object must implement Runnable Each object must have a run method f