karel j robot an introduction to bluej and object- oriented programming

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

Upload: joleen-robbins

Post on 28-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

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

Karel J Robot

An introduction to BlueJ and Object-Oriented Programming

Page 2: 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)

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

BlueJ: New Project

• Please chose “Project” and then “New Project”

• Name the new project “Karel 1”, place it in yourJavaPrograms directory,and choose “Create”

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

BlueJ: New Class

• Click on the “New Class” button

• Name your first object “Robot1” and make it a Karel Object

• Click “OK”

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

BlueJ: Robot1 Object

• Double click on the Robot1 code.It will open the program editor

• Add the following line to the main method:

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

BlueJ: Robot1 Object

• Click on the “Compile” button nearthe 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

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

BlueJ: Robot1 Object

• Right-click on theRobot1 codesquare and choose“void main(String[]args)”

• When this windowappears, choose“OK”

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

BlueJ: Robot1 Object

• The program will open two windows. A command terminal and the Karel J Robot display:

• You will seeour new robot“ren” sitting at(10,1)

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

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

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

BlueJ: Robot1 Object

• You should seeour robot “ren” move forwardone space in thedirection that itwas facing

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

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

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

BlueJ: Robot1 Object

• The UrRobot class has the following five methods:

move()turnLeft()putBeeper()pickBeeper()turnOff()

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

move()

• forward only, one block• “Error Shutoff” if trying to move into a wall

(Look first!) – execution error

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

turnLeft()

• stay at same corner• turn 90 degrees to the left• cannot cause an error shutoff

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

pickBeeper()

• picks up beeper at current location and places in beeper bag

• attempted on beeper-less locations causes an error shutoff

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

putBeeper()

• take beeper from beeper bag and put down on current location

• attempted with an empty beeper bag causes an error shutoff

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

BlueJ: Robot1 Object

• Please add the following lines to your main method:

• Run your new program, what does it do?

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

BlueJ: Robot1 Object

• Programming task #1

Write a program that places 9beepers in a line.

Please show me when you haveit working.

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

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?

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

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; }

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

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:

InitializationInitialization

TestingTesting

ChangeChange

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

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

The scope of i is the body of the loop, and i is undefined outside the loop

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

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++) { ... }

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

BlueJ: Robot1 Object

• Programming task #2

Re-write the program that places 9beepers in a line using a for loop

Please show me when you haveit working

for (int i = 0; i < n ; i++) { ... }

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

BlueJ: Robot1 Object

• I would like your robot to turn leftat the end of its journey. TheturnLeft() method

• Where should you place theturnLeft() method in your code?

• What would happen if you didthis?

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

BlueJ: Robot1 Object

• Please let me know when yousucceed

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

BlueJ: Robot1 Object

• Our next programmingtask is to have ourrobot make a square

• We will need to repeatour commands fourtimes. How can wedo this?

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

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); } }

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

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

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.

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

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:

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

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:

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

BlueJ: Robot1 Object

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

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?

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

BlueJ: Robot1 Object

• You can add new methods easily:

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

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

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

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:

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

BlueJ: Robot1 Object

• Programming challenge. Modify your program so that the robot fills the screen with beepers as shown:

• Somecode toget youstarted:

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

BlueJ: Robot1 Object

• Here is the complete code:

• Note: you can usethe loop variable imore than once ina program

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

BlueJ: Robot1 Object

• Add a nestedloop to repeatyour code fivetimes so that your robot fillsthe screen asshown

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

BlueJ: Robot1 Object

• Some sample code

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

Method Parameters

• It would be nice to make a general method to place beepers

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

Threading

• Please add the following import statements:

• And this line to the top of your program:

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

Threading

• Each object must implement Runnable• Each object must have a run method

• f