Java Programming For Beginners
Java Programming For Beginners
Session Goals
(group discussion)
What’s an Object?
(group discussion; role play with a robot object)
Programming a Robot
import becker.robots.*;
Hands On:
• Download the workshop materials:
• Start the Safari web browser
• Go to www.cs.uwaterloo.ca/~bwbecker/BeginJava.zip It should download to your
desktop and unzip itself.
• Open the file PickFour.java in an editor. It should look like the above.
• Compile it:
• Stuff to do each time you log in:
• open an X terminal to use for compiling and running the programs:
• type cd Desktop/BeginJava to change directories into the directory we’ll
use for this workshop
• type setenv CLASSPATH becker.java:. (include the trailing colon and period).
This tells the compiler to use the becker.jar library.
• Stuff to do every time you want to compile a program:
• type javac «ClassName».java (for example, javac PickFour.java). This runs
the compiler to convert the source code into byte code
• Modify the program so the robot will pick up all four things and stop on Avenue 5.
Compile and run the program to verify your solution.
• Create a new program, PickFour2.java. You will need to change PickFour in line 3
to PickFour2. Modify this program to use two robots to pick up the things, one starting
on Avenue 0 and another starting on Avenue 5. Each robot should pick up two of the
things.
Discussion
Commands: Queries:
• void move() • boolean canPickThing()
• void pickThing() • int countThingsInBackpack()
• void putThing() • boolean frontIsClear()
• void setSpeed(double movesPerSecond) • int getAvenue()
• void turnLeft() • Direction getDirection()
• double getSpeed()
• int getStreet()
if («boolean test»)
{ «statement list»
} else if («boolean test»)
{ «statement list»
} ...
WHILE Statement
while («boolean test») while (karel.canPickThing())
{ «statement list» { karel.pickThing();
} }
FOR Statement
for («init»; «test»; «update») for (int i=0; i<4; i++)
{ «statement list» { karel.pickThing();
} }
Boolean Expressions
• not: !«boolExpr»
• and: «boolExpr1» && «boolExpr2» (boolExpr2 is evaluated only if necessary)
• or: «boolExpr1» || «boolExpr2» (boolExpr2 is evaluated only if necessary)
Inheritance
Robots are pretty limited in what they can do. For example, it would be nice to have a kind of
robot that could turn right with a single command instead of using turnLeft three times. A
class can be extended to do new things as shown:
import becker.robots.*;
import becker.robots.*;
import becker.robots.*;
karel.setSpeed(0.5);
while (karel.frontIsClear())
{ karel.move();
karel.pickThing();
}
System.out.println("\t\tKarel\tSue");
System.out.println("Avenue:\t\t" + karel.getAvenue() + "\t" + sue.getAvenue());
System.out.println("Street:\t\t" + karel.getStreet() + "\t" + sue.getStreet());
System.out.println("Direction:\t" + karel.getDirection()
+ "\t" + sue.getDirection());
System.out.println("Num Things:\t" + karel.countThingsInBackpack()
+ "\t" + sue.countThingsInBackpack());
System.out.println("Speed:\t\t" + karel.getSpeed() + "\t" + sue.getSpeed());
}
}
You can think of the Robot class as being similar to the following (this is much simplified, of
course):
Hands On
• Open Account.java and Bank.java. Leave Bank.java alone; modify Account.java to
model a bank account for use by the bank.
• Test your solution with the following commands:
javac Bank.java Account.java
java –ea Bank
The –ea turns on assertions. They can be useful for checking (testing!) that things you know
ought to be true really are true.
• Suggestion: work incrementally. Do what you need to do to get the uncommented code working.
Then work on getting the next assertion to pass, etc.