Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Karel J Robot An introduction to BlueJ and Object- Oriented Programming."— Presentation transcript:

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

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

3 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”

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

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

6 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

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

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

9 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

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

11 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

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

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

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

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

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

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

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

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

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

21 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

22 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

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

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

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

26 BlueJ: Robot1 Object Please let me know when you succeed

27 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?

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

29 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.

30 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:

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

32 BlueJ: Robot1 Object

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

34 BlueJ: Robot1 Object You can add new methods easily:

35 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

36 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:

37 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:

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

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

40 BlueJ: Robot1 Object Some sample code

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

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

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


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

Similar presentations


Ads by Google