Question: public class Cell { // fields int x; int y; static int size = 35; // constructors public Cell(int inX, int inY) { x =

public class Cell {

// fields

int x;

int y;

static int size = 35;

// constructors

public Cell(int inX, int inY) {

x = inX;

y = inY;

}

// methods

public void paint(Graphics g, Point mousePos) {

if (contains(mousePos)) {

g.setColor(Color.GRAY);

} else {

int val = 0 + (int) (Math.random() * ((4 - 0) + 1));

if (val > 2) {

//display grass

Color grass = Grass.paintShape(g, mousePos);

g.setColor(grass);

} else if (val > 1) {

//water and sand

Color water = Water.paintShape(g, mousePos);

Color sand = Sand.paintShape(g, mousePos);

Color[] colors = new Color[]{water, sand};

int i = 0 + (int) (Math.random() * ((2 - 1) + 1));

g.setColor(colors[i]);

} else {

//stone, wall and fence

Color stone = Stone.paintShape(g, mousePos);

Color wall = Wall.paintShape(g, mousePos);

Color fence = Fence.paintShape(g, mousePos);

Color[] colors = new Color[]{stone, wall, fence};

int i = 0 + (int) (Math.random() * ((3 - 1) + 1));

g.setColor(colors[i]);

}

Its a java code.

can u plz explain how this code working? i want to know how the int val = 0 + (int) (Math.random() * ((4 - 0) + 1)); and similar codes is working as a percentage out of 100 in this code. Plz explain in detain the math behind it

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!