Getting Started With Java and Karel The Robot using jGRASP |
In addition to robots and things, Jo’s world also contains walls. To add a wall to the world, use the command
new Wall(city_name, street#, avenue#, direction);
For example, to add one wall you would modify the Starting_Template with Code Snippet 2. Remember that avenues are those roads that go north-south, and streets are those roads that go east-west.
City toronto = new City();
Robot Jo = new Robot(toronto, 3, 0, Direction.EAST, 0);
new Thing(toronto, 3, 2);
Wall oops = new Wall(toronto, 3, 1, Direction.EAST);
Jo will break if it runs into a wall (see Figure 1.)
Initial State |
Final State – OOPS! |
|
Figure 1
For this in-class exercise, write the code to pick up the thing and put it down again as in the first example, but this time moving Jo around the wall. First save the Starting_Template file with a new name: ICE_1.java. When you change the file name of a Java program, you must also change the public class name (see Code Snippet 2) otherwise the code will not compile. The source code inside
public class ICE_1 extends Object
{
public static void main(String[] args)
{
City toronto = new City();
Robot Jo = new Robot(toronto, 3, 0, Direction.EAST, 0);
new Thing(toronto, 3, 2);
...
}
}
Work with a partner to write code that moves Jo from the initial state to the final state as shown in Figure 2.
Initial State |
Final State |
Figure 2
ICE #3: A More Complicated Example
First, download the file ICE_01_03.java from the course website. Open this in JGrasp, compile it, and run it. You'll notice that the program doesn't actually do anything, but that you do see the initial state, as pictured in Figure 3. You should then add the Java source code needed to move Jo from that state, to the Final state (also pictured in Figure 3).
Figure 3 (Initial State) (Final State)
First, download the file ICE_01_04.java from the course website. Open this in JGrasp, compile it, and run it. You'll notice that the program doesn't actually do anything, but that you do see the initial state, as pictured in Figure 4. You should then add the Java source code needed to move Jo from that state, to the Final state (also pictured in Figure 4).
Figure 4