Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Imagine that we have a box to store particles. Initially, we place randomly 3 particles in that box. After each step, these particles will move freely inside the box. If two particles collide, a new particle will be
placed randomly in the box.
We want to simulate the movement particles for n steps and count the number of particles in the box.
Implement a box of particles (write a class of box, a class for the particle) in such a way that
1. A box has a fixed size: fixed width and height
2. Each particle has a position (x, y) where 0 ≤ x ≤ width of the box, and 0 ≤ y ≤ height of the box

3. A particle can move in one of the directions below but cannot move out of the box (20pts).
+ North (decreasing its y by 1),
+ North East (decreasing its y by 1 and increasing its x by 1),
+ East (increasing its x by 1),
+ South East (increasing its y by 1 and increasing its x by 1)
+ South (increasing its y by 1),
+ South West (increasing its y by 1 and decreasing its x by 1),
+ West (decreasing its x by 1),
+ North West (decreasing its y by 1 and decreasing its x by 1)
Hint: declare an enum type for Direction
4. If two particles collide, a new particle will be placed randomly in the box
and a class for the simulation where for each step,
5. It makes all particles in the box move
6. It shows the number of particles in the box
7. It visualizes the box with particles inside *
-------------------------------
| * * * |
| * * * |
| * |
-------------------------------
one particle = one star
8. Search about singleton pattern and make the box as a singleton

What I have tried:

I write a code in java but I just created the box and don't know how to do the rests. Help. Here is my code:
public class box
{
private int width;
private int height;

public box(int h, int w) {
height = h;
width = w;

if(height<0)
{
System.out.println("Invalid heigth. Setting to 1");
height=1;

}
if(width<0)
{
System.out.println("Invalid width. Setting to 1");
width=1;
}

}
public void drawline(int width, String s)
{
for (int i=0; i<width;i++)
{
System.out.print(s);
}
}
public void drawhollowline (int width)
{
System.out.print("|");
drawline(width - 2, " ");
System.out.print("|");
}
public void drawbox(int height,int width)
{
drawline(width,"-");
System.out.println();
for (int i=0;i<height;i++)
{
drawhollowline(width);
System.out.println();
}
drawline(width,"-");
}

}
Posted
Updated 19-Oct-18 1:53am
Comments
Member1x 19-Oct-18 6:13am    
That seems to be a kind of homework. So you should figure it out yourself to learn something. The task is already near to programming, but still some hints:
- add a property to your box class for particles inside the box
- create a particle class with position properties; in the constructor you initialize it so you can give it a position when being created
- then you will need your simulation class:
+ your box is initialized with three particles
+ in each simulation step you move each particle with a random direction move; the enum hint means you can access each entry with name or a number; you have a possible directions so you need a random number between 0 and 7
+ after you moved each particle you have to check for collision; if collision then create new particle
+ draw your box with particles
+ loop until end of simulation time
- and don't forget to make your box a singleton (use a static property)

We will not provide you with a full solution. But you are welcome to ask on specific problems.

Good luck!
Member 14025358 19-Oct-18 8:36am    
Ok, thank you

1 solution

Quote:
if(height<0)
{
System.out.println("Invalid heigth. Setting to 1");
height=1;

}
if(width<0)
{
System.out.println("Invalid width. Setting to 1");
width=1;
}

I guess you should use better constraints (e.g at least 20).
Your should design a Particle class with current position (x,y) and current direction (the suggested enum) as state variables.
Then you have to move particles, handle collisions between particles (simple, just make sure the particles have the same current position) and collisions between particles and the walls of the box (the requirements says nothing about, I think you may safely assuming usual elastic behaviour, e.g. reflection of x direction on particle hit against a vertical wall).
 
Share this answer
 
Comments
Member 14025358 19-Oct-18 9:33am    
But how can I do the random move and random spaw
CPallini 19-Oct-18 9:44am    
In order to assign random positions to particles (either initial ones and freshly create after collisions), generate two random numbers (X, Y) in the 0..(L-1) range (where L is the length of the Box side).
In order to assign a random direction to each particle, since the particles may have 8 directions, generate a number in the 0..7 range.
Member 14025358 19-Oct-18 9:51am    
Ok, thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900