9
1 • Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up statements Brackets {} should be on a line by themselves Use comments // to identify what the code is supposed to do (very useful)

1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up

Embed Size (px)

Citation preview

Page 1: 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

• Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase)

• Use indentation to line up statements

• Brackets {} should be on a line by themselves

• Use comments // to identify what the code is supposed to do (very useful)

Page 2: 1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up

2

• A Definition of Symbol: A symbol is a written word or sequence of characters used to represent something else.

• Special symbols: ; {} . (they mean something special in Java)

• Names are symbols: karel, UrRobot, putBeeper. UrRobot is not a robot itself, but represents a type of robot.

• Reserved words are symbols: class, void, new (they have a built-in purpose)

Note: Symbols must match in case when used in Java programs (i.e. karel is not the same as Karel and putBeeper is not the same as PutBeeper)

Page 3: 1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up

3

(Errors are referred to as bugs)

• lexical error (compiler catches)– word not in its dictionary (i.e. you type MOVE()

instead of move() )

• syntax error (compiler catches)– incorrect grammar, punctuation, incorrect location of

a statement (i.e. leaving out a parenthesis or using a colon instead of semi-colon)

• execution error (run-time environment catches)– can’t perform what you ask (i.e. error shutoff if

performing move() when a wall is in the way)

• intent error (logic - guess who catches this one!)– program terminates successfully – junk output, however

(i.e. moving robot to the wrong corner)Which is the hardest type of error to correct? Why?

Page 4: 1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up

4

public Class SampleTest implements Directions{

public static void main(String args[] {

UrRobot karel = New UrRobot(2, 1, East, 0). karel.move(); karel.turnLeft() getBeeper(); karel.Move(); karel.turnOff; karel.move(); }

}

IDENTIFY THE BUGS(Clue: There are at least 9 errors)

Page 5: 1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up

5

public class SampleTest implements Directions 1) should be class instead of Class{

public static void main(String args[]) 2) needs parentheses {

UrRobot karel = new UrRobot(2, 1, East, 0);. 3) should be new instead of New

4) use semi-colon instead of period karel.move(); karel.turnLeft(); 5) needs a semi-colon on end karel.pickBeeper(); 6) needs an object and is named pickBeeper

karel.move(); 7) Move is not a method karel.turnOff(); 8) needs () (i.e. turnOff() ); karel.move(); 9) can’t perform move after turnOff}

}

IDENTIFY THE BUGSSOLUTION

public Class SampleTest implements Directions {

public static void main(String args[] {

UrRobot karel = New UrRobot(2, 1, East, 0).

karel.move(); karel.turnLeft() getBeeper();

karel.Move(); karel.turnOff; karel.move(); }

}

Page 6: 1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up

6

Now You Try Writing a Short Program to Draw the

Letter H• Robot starts at origin (1,1), facing north• Robot draws the letter H• The bottom of the left bar of the H starts at (2,2)

• The H is 5 beepers high• The middle bar should be 2 beepers between the bars

• Robot should return to origin, face north, and turn itself off when finished (leaving the world in its original state of being

Take a little time to think about the best approach before jumping into the code. It may help to sketch the

robot’s path on a sheet of paper.

Page 7: 1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up

7

import kareltherobot.*;public class DrawH_YOURNAME implements Directions{

public static void main(String args[]) {

World.setVisible(true);

UrRobot karel = new UrRobot(1, 1, North, infinity);

// …….insert your code here to draw the letter H

} }

DrawH programStarting Point

Name your class like this: (i.e. DrawH_JohnDoe)

Page 8: 1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up

8

Beginning Situation Ending Situation

Page 9: 1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up

9

Copying Classes between School and Home

• Since there are times when you will want to continue your work at home, you will need to be able to transfer your code back and forth. This is where your flash drive becomes handy.

• To reduce confusion, keep the folder structure on your flash drive the same as the folder structure on your school computer

• Use file manager to locate the Java source file (that has extension .java) within the package folder.

• Copy only the .java files (not the .class files) to your flash drive folder.

• You can then copy these files to your package folder on your destination computer.

• Note: After you launch jGRASP, it is important to refresh the project.