Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CISC124 Assignment 2: Writing methods and introduction to using objects

Instructions

Download and save this eclipse project archive file but do not extract it. Copy or move the file into the assignment directory on your computer.

In eclipse, import the project archive using the following steps:

  1. Under the File menu choose Import…
  2. Under General choose Existing Projects into Workspace
  3. Click the radio button beside Select archive file and browse for the zip file A2_project.zip
  4. Make sure that the project A1 appears in the list of projects and has a check mark beside it.
  5. Click Finish to import the project.

Next, copy your Assignment 1 solution for ForestFire.java into the A2 project soc package. You will be adding several methods to your ForestFire class in this assignment to produce a runnable Forest-fire simulation.

Submitting your work

Submit your work on onq under Assignment 2. You should upload one file:

  • ForestFire.java

DO NOT upload a zip file containing your work. Doing so greatly slows down the processing of assignments, and you will receive a grade of zero on this assignment if you do so.

Your class should both be in the package soc (the same package that they appear in in the project file). DO NOT change the package name or remove the package name from your files.

When marking your work the teaching assistants will be looking for the following:

  • are your methods named correctly?
  • is your code neatly and consistently formatted?
  • are your methods correct?

Review of the Forest-fire model

Students should review the information in Assignment 1 for the details of the Forest-fire model. In particular, students should review the rules that govern the model and the definition of the Moore neighborhood.

https://research.cs.queensu.ca/home/burtonma/2023F/CISC124/A1/A1.html

Step 1: Generating random numbers

Rules 3 and 4 of the Forest-fire model introduce randomness to the model. Rule 3 says that a tree can randomly be struck by lightning and set on fire. Rule 4 says that a tree can randomly grow in an empty cell. To implement a computer program of the Forest-fire model we require a way to generate random numbers.

The class java.util.Random allows the programmer to create an object of type Random that can generate random boolean, integer, and floating-point values. To use the class, we must first import the class because it is defined in the package java.util.

Open ForestFire.java and add the following import statement after the package declaration:

package soc;  import java.util.Random; // ADD THIS LINE

Next, create a Random object by adding the following lines of code somewhere inside the class body of ForestFire:

public class ForestFire {   // SOMEWHERE INSIDE THE CLASS BODY  // THE MOST LOGICAL LOCATION IS AFTER THE DECLARACTION OF PROB_GROWTH   /**  * A random number generator.  */  private static Random rng = new Random(1);  }

The added code declares a static variable named rng that stores a reference to a Random object.

The class Random has two methods that are useful in this assignment. The method:

public double nextDouble()

returns a random double value between 0.0 and 1.0. For example

double value = ForestFire.rng.nextDouble();

sets value to a random value between 0.0 and 1.0.

The method:

public int nextInt(int bound)

returns a random int value between 0 and bound - 1. For example

int value = ForestFire.rng.nextInt(100);

sets value to a random value between 0 and 99.

Add and complete the following two methods to the ForestFire class:

 /**  * Tests if a tree spontaneously grows during one time step in a Forest-fire  * simulation.  *   * @return true if a tree spontaneously grows, false otherwise  */  public static boolean randomlyGrows() {    }   /**  * Tests if a tree burns after being struck by lightning during one time step in  * a Forest-fire simulation.  *   * @return true if a tree burns after being struck by lightning, false otherwise  */  public static boolean randomlyIgnites() {    }

For the method randomlyGrows(), you should generate a random double value between 0.0 and 1.0. If the generated value is less than ForestFire.PROB_GROWTH then the method should return true, otherwise it should return false.

For the method randomlyIgnites(), you should generate a random double value between 0.0 and 1.0. If the generated value is less than ForestFire.PROB_IGNITION then the method should return true, otherwise it should return false.

Step 2: Initializing the cells of a Forest-fire

To run a Forest-fire simulation, we must initialize the cells of a Forest-fire model. There are many ways that we can initialize the cells; for example, we can set the cells so that they:

  • are all equal to the same value (e.g., all equal to State.EMPTY)
  • are arranged in some deterministic pattern such as a checkerboard of State.EMPTY and State.OCCUPIED cells
  • are randomly set to some combination of State.EMPTYState.OCCUPIED, and and State.BURNING cells

Add the following method to the ForestFire class:

 /**  * Sets all of the cells of a Forest-fire model to a specified state.  *   * 
	

* NOTE: THIS METHOD IS ALREADY IMPLEMENTED. * * @param cells the cells of a Forest-fire model */ public static void fill(State[][] cells, State s) { int rows = ForestFire.rowsIn(cells); int cols = ForestFire.colsIn(cells); for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { cells[r][c] = s; } } }

The fill method sets all of the cells of a Forest-fire model to some specified state s.

Add and complete the following method to the ForestFire class:

 /**  * Sets the cells of a Forest-fire model to a checkerboard pattern of  * {@code State.EMPTY} and {@code State.OCCUPIED} cells. The state of the  * upperleft-most cell {@code State.EMPTY}.  *   * @param cells the cells of a Forest-fire model  */  public static void checkerBoard(State[][] cells) {    }

After completing the checkerBoard method, you can run the Viewer class to visualize the cells of a Forest-fire model. You should see the following if your method is correct:

Step 3: Updating the forest

The Forest-fire model simulates the evolution of a forest over time. The initial configuration of cells corresponds to the forest at time =0. The model then updates the state of the forest to produce the forest at time =1. It repeats the update process producing the state of the forest at times =2,3,4,....

When the array of cells updates from time = to time