Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Model We begin by defining the model. The model consists of a Model object that

ID: 3772626 • Letter: M

Question

Model
We begin by defining the model. The model consists of a Model object that contains a larger amount of Particle object.
The particles are in a two dimensional coordinate system. In our simplified model is discretized time, and each update of the state entails that each particle moves distance L in a random direction, that we update the position of: x (t) = x (t-) + L * cos () y (t) = y (t-) + L * sin () where x (t) and y (t) are respectively the coordinates at time t, is the time step between two updates and is a randomly selected direction, which is uniformly distributed between 0 and 2.
1) Defining the Model. It should contain all the particle objects and constant L. It should have a constructor where you specify how many particles to be included, a method that updates the position of all particles, a method that returns the positions of the particles, and methods that sets respectively. reads the value of L.

2) Defining the Particle. It shall contain the particle position with good accuracy, and a method that continually updates the position of the above formula. It must be understood using the value of L from the model object. Particle may advantageously be a single class to the Model. Particle should have a constructor that takes two coordinates as arguments, and one without arguments that randomizes the position.

3) What is the advantage of allowing the arguments resolve the constructor call the other?

How to program 1,2 and 3 in Java?

Explanation / Answer

//Program:


package javaapplication108;

import java.util.Random;
import java.util.Scanner;
/*Defining the Particle. It shall contain the particle position with good accuracy,
and a method that continually updates the position of the above formula. It must be
understood using the value of L from the model object. Particle may advantageously be
a single class to the Model. Particle should have a constructor that takes two
coordinates as arguments, and one without arguments that randomizes the position.
*/
class Particle
{
//position
int x;
int y;
public Particle()
{
Random randomGenerator = new Random();
x=randomGenerator.nextInt(100);
y=randomGenerator.nextInt(100);
}
public Particle(int xx, int yy)
{
x=xx;
y=yy;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void updatePosition(int newX, int newY)
{
x=newX;
y=newY;
}
}
/* Defining the Model. It should contain all the particle objects and constant L.
It should have a constructor where you specify how many particles to be included,
a method that updates the position of all particles, a method that returns the positions
of the particles, and methods that sets respectively. reads the value of L.*/
class Model
{
Particle particles[];
double L; //distance
public Model(int n)
{
particles=new Particle[n];
L=10.5; // choose value of L
}
public void updateParticlesPosition()
{
Random generator=new Random();
double delta=0.5;
/* update of the state entails that each particle moves distance L in a random direction,
that we update the position of: x (t) = x (t-) + L * cos () y (t) = y (t-) + L * sin ()
where x (t) and y (t) are respectively the coordinates at time t,
is the time step between two updates and is a randomly selected direction,
which is uniformly distributed between 0 and 2.*/
for(int t=0;t<particles.length;t++)
{
int x=particles[t].getX();
int y=particles[t].getY();
double theta = generator.nextDouble() * 360.0;
int newX =(int) (x *(t-delta) + L * Math.cos(theta));
int newY =(int)( y *(t-delta) + L *Math.sin(theta));
particles[t].updatePosition(newX, newY);
}

}
public void getParticlesPosition()
{
for(int t=0;t<particles.length;t++)
{
System.out.println("Particle "+t+": "+ "("+particles[t].getX()+","+particles[t].getY()+")");
}

}
public double getL()
{
return L;
}
}

public class JavaApplication108 {

public static void main(String[] args) {
System.out.println("Enter the number of particles");
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
Model M=new Model(n);
for(int i=0;i<n;i++)
{
System.out.println("Enter particle position <x y>");
M.particles[i]=new Particle(scan.nextInt(),scan.nextInt());
}
M.updateParticlesPosition();
System.out.println("Particles available in the Model are");
M.getParticlesPosition();
}
  
}

//Output:

run:
Enter the number of particles
2
Enter particle position <x y>
12 45
Enter particle position <x y>
56
78
Particles available in the Model are
Particle 0: (-16,-22)
Particle 1: (24,48)
BUILD SUCCESSFUL (total time: 8 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote