karel j robot an introduction to bluej and object- oriented programming
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 yourJavaPrograms 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 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
BlueJ: Robot1 Object
• Right-click on theRobot1 codesquare and choose“void main(String[]args)”
• When this windowappears, choose“OK”
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)
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 seeour robot “ren” move forwardone space in thedirection that itwas 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 9beepers in a line.
Please show me when you haveit 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:
InitializationInitialization
TestingTesting
ChangeChange
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
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 9beepers in a line using a for loop
Please show me when you haveit working
for (int i = 0; i < n ; i++) { ... }
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?
BlueJ: Robot1 Object
• Please let me know when yousucceed
BlueJ: Robot1 Object
• Our next programmingtask is to have ourrobot make a square
• We will need to repeatour commands fourtimes. How can wedo 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
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
•
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:
• Somecode toget youstarted:
BlueJ: Robot1 Object
• Here is the complete code:
• Note: you can usethe loop variable imore than once ina program
BlueJ: Robot1 Object
• Add a nestedloop to repeatyour code fivetimes so that your robot fillsthe screen asshown
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