Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Introduction To Data Structures And Algorithm Analysis Java Edition

Authors: Clifford A. Shaffer

1st Edition

0136609112, 978-0136609117

More Books

Students also viewed these Programming questions