Presentation is loading. Please wait.

Presentation is loading. Please wait.

Administrative Matters

Similar presentations


Presentation on theme: "Administrative Matters"— Presentation transcript:

1 Administrative Matters
No more tirgulim in the computer labs. Use lab support & reception hours to get help. Follow course website, forums, and s carefully for notices. IntroCSP is meant for students with no previous experience. Only students who got permission may be registered.

2 Administrative Matters
You can find these slides on the website We try to post them early so you can print them (careful not to run out of printing quota). Ex1 is now released. Due next Wednesday. Solve exercises on your own! No copying, no looking at other’s code, no sharing written material. If you consult with someone, write it in your README file.

3 Hello World Program

4 Elements of the Program
/* * Written by the IntroCSP staff. * This is a short program that prints Hello World. */ public class Hello { public static void main(String[] args){ //Will print out Hello World: System.out.println("Hello World"); } comment Class declaration Main method For now we code here. comment Statement

5 Compilation and Execution
Notice: The file name must be the same as the class name

6 What if we write it wrong? Compilation Errors
A common mistake: writing system instead of System.

7 What if we write it wrong? Compilation Errors
Another common mistake: Forgetting the ;

8 Variables (Quick Reminder)
Different types of variables int Integers: …,-2,1,0,1,2,… boolean true/false: double Real Numbers: 0.1, -0.7,100.0,… char A single character: ‘a’, ‘%’, ‘A’, ’b’ String A sequence of characters: “hello”,”this is a string\n”,”a”

9 Variables (Reminder) Declaration: Assignment: Both together:
int numberOfPeople; Assignment: numberOfPeople=5; Both together: int numberOfPeople=5; Now we can use the variable within expressions: System.out.println(numberOfPeople*2+3); numberOfPeople = numberOfPeople+5;

10 Naming Conventions The language gives a lot of freedom in choosing variable names. However, for better readability: Use informative names Loop variables can have short names i,j etc. Start variables with a lowercase letter, every added word starts capitalized. Eg: numberOfElements, height, tableWidth

11 Naming Conventions Start class names with a capital letter.
Eg: HelloWorld, LinkedList, Component Stick to this style when you write code! Diverging will cost points on exercises!

12 Karel’s (Robot) World A simple environment that we built for you.
Karel will accompany us in the following couple of weeks Ex1 uses Karel’s world. Karel lives in a 2 dimensional grid world. I.e., each place is identified by a pair (i,j), i,j are integer numbers.

13 Karel’s (Robot) World N Robot (facing East) wall beeper W E 8 S t r e
7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10 11 12 13 Origin Avenues

14

15 Karel’s Capabilities Can move forward Can turn in place
Can pick up and put down beepers into his bag Can look at the world and see nearby walls, beepers, etc.

16 Karel’s Tasks We will try to accomplish different tasks with Karel. For example: Pick up all the beepers in the world. The world we start in may be different every time. We’ll need to give Karel a “flexible” program. Handle different cases that might arise.

17 A Simple Program with Karel
import intro.csp.ex1.BeepersAndWalls; import intro.proceduralKarel.*; public class KarelDemo { public static void main(String[] args){ ProceduralKarel.chooseWorld(BeepersAndWalls.ID, -1); ProceduralKarel.setMoveDelayMilli(1000); //... Here we should tell Karel to do more stuff... ProceduralKarel.turnOff(); } Allows us to use Karel and the BeepersAndWalls World Choose the World to use Animation delay Shuts down Karel and closes the animation window

18 Karel’s Commands All of Karel’s commands can be found in the documentation of the course library. On the course website go to “useful links” and there see a link to the documentation. All commands start with the prefix ProceduralKarel. They also end with Brackets: (); For example: ProceduralKarel.move();

19

20

21 Karel’s Commands - Statements
chooseWorld(World.ID, seed); setMoveDelayMilli(delay); turnOff(); move(); turnLeft(); turnRight(); pickBeeper(); putBeeper(); Selects and loads the world. Sets the animation delay Turns off the robot. You must remember to do this! Move one step forward Turn 90 degrees Pick up one beeper Put down one beeper

22 Example of a Simple Program
import intro.csp.ex1.BeepersAndWalls; import intro.proceduralKarel.*; public class KarelProgram { public static void main(String[] args){ ProceduralKarel.chooseWorld(BeepersAndWalls.ID, -1); ProceduralKarel.setMoveDelayMilli(1000); //here we do some actions with Karel: ProceduralKarel.move(); ProceduralKarel.pickBeeper(); ProceduralKarel.turnLeft(); ProceduralKarel.putBeeper(); ProceduralKarel.turnOff(); } programs need to be more documented than this. Especially in exercises! (We have no room on slides)

23 Error Shutdown Some of the statements may cause errors if you use them in the wrong situation: putBeeper() when Karel has no beepers pickBeeper() if no beepers at current location move() when facing a wall This causes the program to stop – an error shutdown. Must make sure it never ever happens!

24 Unsuccessful move and Error Shutoff
1 2 3 4 5 Before move S t r e s Avenues 1 2 3 4 5 After move: Error Shutoff S t r e s

25 More of Karel’s Commands
We can avoid errors by having Karel look at the world around him. The following commands tell us about the world. They have a boolean type (true/false). frontIsClear() leftIsClear() rightIsClear() anyBeepersInBag() anyBeepersInCorner() Checks if there is a wall next to Karel. Check if Karel has beepers Check if there are beepers at the current location.

26 Example of Safer Use We want to walk only if the way is clear.
if(ProceduralKarel.frontIsClear()){ ProceduralKarel.move(); } else{ //we may want to do something here Notice how we use one of Karel’s commands for our condition. Do we always have to check before acting? Not if we know we are safe.

27 The Rest of the Commands
facingEast() facingNorth() facingSouth() facingWest() getBagBeeperNumber() getCornerBeeperNumber() getX() getY() getMoveDelayMilli(); Checks if Karel is facing in a given direction. (boolean type) Tells the number of beepers in the bag and in the corner. (int type) Gets the coordinates Karel is in. (int type) Gets the currently set animation delay. (int)

28 The Complete Karel Cheat-Sheet
chooseWorld(World.ID, seed) setMoveDelayMilli(delay) getMoveDelayMilli() turnOff() move() turnLeft() turnRight() pickBeeper() putBeeper() facingEast() facingNorth() facingSouth() facingWest() frontIsClear() leftIsClear() rightIsClear() anyBeepersInBag() anyBeepersInCorner() getBagBeeperNumber() getCornerBeeperNumber() getX() getY() Don’t forget the prefix: ProceduralKarel.

29 A Simple Task – Walking Until Karel Faces a Wall
A really bad way to do this: if(ProceduralKarel.frontIsClear()){ ProceduralKarel.move(); }

30 While loops (Reminder)
while( condition ){ Statements } more of the program The condition is checked If it is true: the statements are executed. Return to step 1. If it is false, proceed with the program

31 More on while Loops The command break; exists the current loop.
The command continue; skips the rest of the current iteration and goes back to the beginning. An infinite loop: while(true){…} If your program is in an infinite loop, you can kill it by pressing Ctrl-c.

32 Walk Till Facing a Wall TwoPacksOfBeepers is another world used in Ex1
import intro.csp.ex1.TwoPacksOfBeepers; import intro.proceduralKarel.*; public class KarelDemo { public static void main(String[] args){ ProceduralKarel.chooseWorld(TwoPacksOfBeepers.ID,-1); ProceduralKarel.setMoveDelayMilli(1000); //walk to the wall ahead: while(ProceduralKarel.frontIsClear()){ ProceduralKarel.move(); } ProceduralKarel.turnOff(); Notice The indentation of the while command

33 Let’s Pick up Beepers Along the Way
Again, notice The indentation. //walk to the wall ahead: while(ProceduralKarel.frontIsClear()){ ProceduralKarel.move(); //if there are beepers, pick one up. if(ProceduralKarel.anyBeepersInCorner()){ ProceduralKarel.pickBeeper(); } What happens to beepers that are in the origin? What if we want to pick up all the beepers in each corner?

34 Walk to the Wall and Pick Up All Beepers
Is there another way to get this task done? //walk to the next wall: while(ProceduralKarel.frontIsClear()){ ProceduralKarel.move(); //pick up and count beepers in this square: int beeperCount = 0; //this will count the beepers while(ProceduralKarel.anyBeepersInCorner()){ ProceduralKarel.pickBeeper(); beeperCount++; } System.out.println("Found " + beeperCount + “ beepers in this corner"); Use informative variable names This println() was too long so we broke it to several lines. Do the same with long lines: break and indent.

35 For loops (Reminder) for( initialization ; condition ; update){
Statements } Do the initialization Check the condition. If it is true: Execute the statements Execute the update Repeat step 2 Otherwise go on with the program

36 A Program With a Bug while(ProceduralKarel.frontIsClear()){
ProceduralKarel.move(); //pick up and count all beepers in this square: int counter; //will count the beepers for(counter = 0; counter< ProceduralKarel.getCornerBeeperNumber(); counter++){ ProceduralKarel.pickBeeper(); } System.out.println("Found " + counter + "beepers in this corner"); The program compiles. Can you spot the bug?

37 A Program With a Bug while(ProceduralKarel.frontIsClear()){
ProceduralKarel.move(); //pick up and count beepers in this square: int counter; for(counter = 0; counter< ProceduralKarel.getCornerBeeperNumber(); counter++){ ProceduralKarel.pickBeeper(); } System.out.println("Found " + counter + "beepers in this corner"); The code would have worked if getCornerBeeperNumber() was computed only at the begining, but it changes when we pick up beepers within the for loop!

38 Tip: Need to understand a complex loop? Simulate it manually!
Another version while(ProceduralKarel.frontIsClear()){ ProceduralKarel.move(); //pick up all beepers in this square: int numBeepers = ProceduralKarel.getCornerBeeperNumber(); for(int i=0; i<numBeepers ; i++){ ProceduralKarel.pickBeeper(); } Tip: Need to understand a complex loop? Simulate it manually!

39 Tips For Easy Coding and Debugging
Compile and Execute often. Don’t let errors accumulate. Write code the right way from the start Don’t go back and change variable names to meaningful names. Use them from the beginning. Don’t go back and indent – do it right the first time.

40 Tips For Easy Coding and Debugging
Open and Close brackets immediately. Use Brackets even for a single expression inside if,for,while Debug using println() inside the code Learn the value of hidden variables, How many times loops are executed, Etc. (Don’t forget to remove the prints later)

41 Tips For Easy Coding and Debugging
Write indented code. It helps! You see where blocks start and end more clearly. Tip: If the Automated indentation of emacs seems wrong, it probably indicates you did something wrong like forget some ‘;’ or ‘}’ Test, test, and test some more.

42 Allowing the User to Control Karel
Let’s allow the user to input orders for Karel. We will use the following commands: 1 to go forward to the nearest wall 2 to turn right 3 to turn left 4 to exit the program

43 import intro.csp.ex1.TwoPacksOfBeepers;
import intro.proceduralKarel.*; import intro.utils.SimpleInput; public class KarelControl{ public static void main(String[] args){ ProceduralKarel.chooseWorld(TwoPacksOfBeepers.ID, -1); ProceduralKarel.setMoveDelayMilli(1000); System.out.println("To control Karel:\n" + "1: Walk to wall\n" + "2: Turn right\n" + "3: Turn left\n" + "4: Exit the program"); boolean notDone = true; //a variable while(notDone){ ... } ProceduralKarel.turnOff(); This import allows us to use SimpleInput Really bad documentation Do the work in here Again, this should be better documented.

44 while(notDone){ System.out.println("Enter your command:"); int action = SimpleInput.in.readlnInt(); switch(action){ case 1: while(ProceduralKarel.frontIsClear()) ProceduralKarel.move(); break; case 2: ProceduralKarel.turnRight(); case 3: ProceduralKarel.turnLeft(); case 4: notDone = false; default: System.out.println("Huh?"); } Can this program crash? What happens if we forget one of the break; commands?

45 Walk to the Wall, dance the “Holla” every corner coming back, minimizing number of front is clear() operations int numOfSteps=0; // walk to the wall ahead while(ProceduralKarel.frontIsClear()){ ProceduralKarel.move(); numOfSteps++; } // Turn Back ProceduralKarel.turnRight(); do{ for(int j=1; j<=4; j++){ // turn in a circle numOfSteps--; }while(numOfSteps>0); ProceduralKarel.turnOff(); import and class declaration missing… What if numOfSteps = 0 when we enter the do while statement? When does it happen?

46 Now dance the Holla all around the world…
do{ //dance in a straight line while(ProceduralKarel.frontIsClear()){ ProceduralKarel.move(); //twirl around for(int i=0; i<4; i++){ ProceduralKarel.turnRight(); } //turn and be ready for the next straight line }while(!ProceduralKarel.facingNorth()); Can also write (int i=1; i<=4; i++) and so on… When does the do-while loop stop?


Download ppt "Administrative Matters"

Similar presentations


Ads by Google