# Using methods to abstract out patterns

In the Light-bot problems at the beginning of the semester, we saw how the use of Light-bot functions could help us solve complex levels that required more command “tiles” than would fit in the main scripting area. We also saw how functions could allow us to capture patterns of movement in order to gain a higher-level view on what the bot was doing.

Java has a feature similar to a Light-bot function called a method. Java’s methods are described in detail in the Horstmann Java for Everyone textbook in Chapter 5: you should read that material before attempting this programming problem.

The goal of this assignment is to use Java methods to program a solution to a Light-bot level, by printing out codes representing the movements that the bot should make. We’ll use methods in order to capture the same kind of patterns we saw in the original Light-bot functions.

Let’s use the following codes for basic Light-bot moves:

• F for Forward motion;
• R for a Right turn;
• L for a Left turn;
• J for a Jump;
• and S for the Light-bot “Squat” (i.e., the move that turns on or off the light on a square).

Given a Light-bot level, your Java program should print out a sequence of these basic moves that will solve the level (i.e., walk around the board and turn on all the lights). In order to make the output readable, put spaces in between the letter codes. You may also want to use newlines to show the structure of your method calls (see the example in the next section below).

## Using methods to solve Light-bot Level 7

Here's a picture of the level 7 game board for Light-bot:

We can solve this level using the two Light-bot functions to capture a pattern of alternating forward movement and “squatting”, along with a little bit of turning and other set-up steps:

We could write this up as a corresponding group of Java methods as follows: (but also see another approach below):

```/* A Java-based solution to Light-bot: level 7  */
/* Fritz Ruehr * Willamette University * CS 141 */

class Level7a {

public static void main(String[] argh) {

sout("F ");

f2();

sout("R F R ");

f2();

sout("\n");

}

public static void f1() {

sout("S F S F S F ");

}

public static void f2() {

f1();

f1();

sout("S ");

}

public static void sout(String s) {

System.out.print(s);

}

}
```

When run, the Java solution prints as follows:

F S F S F S F S F S F S F S R F R S F S F S F S F S F S F S

Here the main Java method corresponds to the main scripting area in the Light-bot solution and the methods called f1 and f2 correspond to the two original Light-bot functions.

Of course, Java methods do not have the same artificial space restrictions that the Light-bot functions do: we could take advantage of this to make an even more succinct and readable solution to the level 7 problem, using just a single method to capture the “long run” down each side like this:

```/* A Java-based solution to Light-bot: level 7  */
/* Fritz Ruehr * Willamette University * CS 141 */

class Level7b {

public static void main(String[] argh) {

sout("F ");

f1();

sout("R F R ");

f1();

sout("\n");

}

public static void f1() {

sout("S F S F S F S F S F S F S ");

}

public static void sout(String s) {

System.out.print(s);

}

}
```

When run, this Java solution prints as follows (note this is the same as the output above):

F S F S F S F S F S F S F S R F R S F S F S F S F S F S F S

Of course, we could just write a main method that just prints out the whole string!

The point is to use methods to try and capture the important patterns in your solution: not just to use methods for their own sake, or even to make the solution more concise, but to reflect something important about the structure of your solution.

These issues will show up more clearly when you write your own Java methods to solve ...

## Your assignment: Light-bot levels 8 and 9

You should write and turn in solutions to the 8th and 9th levels of Light-Bot, written in Java, to generate string codes, like the ones shown above. Try to find common patterns in the codes you are trying to generate, and use as many methods as you like. You should also pick better names than f1 and f2; good choices would correspond to parts of the board (e.g., “side”, “corner”, etc.).

## NEW! Check your Light-bot codes here!

Try this little Java applet to check your Light-bot level solution codes (hit “reload” if it doesn’t show up the first time):

Note! Due to recent changes in Oracle's security arrangements, it is not possible on most newer Java systems to paste text into a Java applet running on the web, such as the Handy-Dandy CS 141 Light-bot Checker above. You can change this policy by adding the following lines to the bottom of your Java security policy file (only the second one really matters):

```// allow cut and paste

permission java.awt.AWTPermission "accessClipboard";
```

You may find the appropriate security file as follows:

• on recent model Macs:
the file /Library/Java/Home/lib/security/java.policy
• on Unix machines:
the file java.home/lib/security/java.policy
• on Windows machines:
the file java.home\lib\security\java.policy

Remember! Oracle (the new owners of Java) think this is dangerous, so do so only at your own risk! (Ironically, if you have a somewhat older version of Java, you won’t even see the problem at all!)