Getting Started With Java and Karel The Robot using jGRASP

 

Note:  Please keep the programs that you create today, in case you have a question about your grades for the ICEs at the end of the quarter. When you're working with a partner, each person should save their own, individual copy.

 

ICE #1:  Errors In Programs

 

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

Code Snippet  1

Jo will break if it runs into a wall (see  Figure 1.)

Initial State

Final State – OOPS!

Figure  1

You should try adding the above code, and make sure that when you run your program, the robot crashes into the wall.  Notice how the program will blindly crash the robot, rather than fixing this mistake.  Since the Java language will happily, blindly crash your program for you, you'll have to fix this error yourself, which is exactly what you'll do in the next section.

 

When you get done with this exercise, go on to the next section.


ICE # 2: Get Jo around the wall

 

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

        ...

   }

}

Code Snippet  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

 

If you finish this ICE, show the instructor, and then proceed on to the next ICE.


 

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)

 

ICE #4: Yet More Complexity

 

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