Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 7 - Water Particles Modify your program so that you can also paint with water particles, which move in one of three randomly chosen

Part 7- Water Particles
Modify your program so that you can also paint with water particles, which move in one of three randomly chosen directions: down, left, or right. In the step method, when the randomly chosen location contains a water particle, pick one of three random directions. If the location in that randomly chosen direction is empty, the water particle moves there. Test that the water behaves roughly like a liquid, taking the shape of a container.
Part 8- Dropping Sand into Water
What happens now when you drop sand particles into water? Right now, sand is only allowed to move into empty spaces. Modify your code so that a sand particle can also move into a space containing a water particle (by trading places with the water particle). Test that you can drop sand into water now (without destroying the water).
Part 9- Additional Particles
Congrats, you're doing well! To complete the assignment, add two additional particle types that behave differently than the existing ones. You can add whatever you want. Here are some ideas:
a gas that raises to the top of the grid
a particle that floats on water (maybe wood or plastic)
an acid that eats metal (or another type of material)package fallingsand;
import java.awt.*;
public class SandLab
{
//add constants for particle types here
public static final int EMPTY =0;
public static final int METAL =1;
//do not add any more fields
private int[][] grid;
private SandDisplay display;
public static final int ROWS =120;
public static final int COLUMNS =80;
public static void main(String[] args)
{
SandLab lab = new SandLab(ROWS, COLUMNS);
lab.run();
}
public SandLab(int numRows, int numCols)
{
String[] names;
names = new String[2];
names[EMPTY]= "Empty";
names[METAL]= "Metal";
display = new SandDisplay("Falling Sand", numRows, numCols, names);
}
//called when the user clicks on a location using the given tool
private void locationClicked(int row, int col, int tool)
{
}
//copies each element of grid into the display
public void updateDisplay()
{
Color empty = new Color(0,0,0);
for (int row =0; row < ROWS; row++)
{
for (int column =0; column < COLUMNS; column++)
{
if (grid[row][column]== EMPTY)
{
display.setColor(row, column, empty);
}
}
}
}
//called repeatedly.
//causes one random particle to maybe do something.
public void step()
{
}
//do not modify
public void run()
{
while (true)
{
for (int i =0; i < display.getSpeed(); i++)
{
step();
}
updateDisplay();
display.repaint();
display.pause(1); //wait for redrawing and for mouse
int[] mouseLoc = display.getMouseLocation();
if (mouseLoc != null)//test if mouse clicked
{
locationClicked(mouseLoc[0], mouseLoc[1], display.getTool());
}
}
}
}

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

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions

Question

=+What does the growth rate of GDP measure?

Answered: 1 week ago