Understanding variables and assignment is critical to being able to
program but is an easy thing to be confused about. Get over this hurdle and
programming will be far easier.

In this exercise we complete a series of dry run exercises where we step through short fragments of code. This is an important activity to reinforce your understanding of variables. The instructor will step through the first exercise with you.

Specifications

Take out a piece of paper to record your answers, making sure to put the exercise name and your name on the paper.

We will be adding more code to these files in subsequent exercises, so it is not time to submit them to Canvas yet. However, it is a good idea to have a backup copy in case of problems later in development.

Specifications

Open the editor for the Bug class and add the following code to the act() method:

if (isTouching(Flower.class)) {
removeTouching(Flower.class);
}

Press the Compile button and verify there are no errors.

Resolve any errors you find, getting help from a guild member or the instructor as needed.

Open the source code editor of the Lizard class and copy the following code into the act() method:

if (isTouching(Flower.class)) {
removeTouching(Flower.class);
}

Modify the code added to the Lizard class to check for and remove a Bug instead of a Flower.

Compile all the classes and verify there are no errors. Resolve any errors you find, getting help from a guild member or the instructor as needed.

Set up several flowers and one or more bugs and a Lizard to see how the scenario works. Verify that the bug eats flowers and the Lizard eats bugs.

Save a copy of your final scenario with all the changes made to upload to Canvas as part of the next lab.

We will be adding more code to these files in subsequent exercises, so it is not time to submit them to Canvas yet. However, it is a good idea to have a backup copy in case of problems later in development.

Methods have public access if we want to make them accessible to other classes

There are other access modifiers but we should use public for now

Can you identify each of these syntax items in the act() method we have always used?

public void act()
{
// add commands here
}

Example of Writing a New Method

As an example of writing a new method, let us make a method out of our code to look for a flower

For example:

/**
* Checks whether we have stumbled upon a flower.
* If we have, then eat it. Otherwise, do nothing.
*/
public void lookForFlower() {
if (isTouching(Flower.class)) {
removeTouching(Flower.class);
}
}

The overall code is known as a method definition

The above code defines a new method

Let us look at each part of the new method

Comments

The first four lines are a comment

Comments are ... comments -- notes to people reading the code

The computer ignores comments entirely

We write comments to explain our code to human readers

Every method should have a comment block like the example

Method signature

The next line after the comment is the method signature:

public void lookForFlower()

We technically can use words other than public, but for now always use public

Specifications

If you are missing the scenario then download, save and unzip the file: bugs2.zip.

Open the source code editor of the Bug class and add the following method:

/**
* Checks whether we have stumbled upon a flower.
* If we have, then eat it. Otherwise, do nothing.
*/
public void lookForFlower()
{
if (isTouching(Flower.class))
{
removeTouching(Flower.class);
}
}

Add two new methods to the Bug class, copying the appropriate code from the act() method:

public void turnAtEdge()

public void randomMove(int speed)

Change the act() method of Bug as follows:

public void act()
{
turnAtEdge();
randomMove(5);
lookForFlower();
}

When completed, compile the class and verify there are no errors. Resolve any errors you find, getting help from a guild member or the instructor as needed.

Organize the code in the Lizard class in a similar way.

However, instead of looking for and eating flowers, look for and eat bugs.

Compile and run your scenario to verify all the changes work well.

If you have problems, ask a guild member or the instructor for help as needed.

Save a copy of your final scenario with all the changes made to upload to Canvas as part of the next lab.

We will be adding more code to these files in subsequent exercises, so it is not time to submit them to Canvas yet. However, it is a good idea to have a backup copy in case of problems later in development.

As time permits, read the following sections and be prepared to answer the Check Yourself questions in the section: 3.4.4: Summary.