Create a class and write a main method that does the following 1. create an inst
ID: 3566420 • Letter: C
Question
Create a class and write a main method that does the following
1. create an instance of Particle method.
2. The start values of the Particle object are xPosition =0.0, yPosition = 350, xVelocity = 0.0 and
yVelocity = 0.0Particle p = new Particle ( 0.0, 350.0, 0.0, 0.0);
3. Simulate the particle falling. When the y position of the particle <= 0 the particle has hit the ground. Repeat calling the passTime method with a time of 0.001 seconds until the yPosition of the
particle <= 0 ( i.e. the particle hits the ground).
int count = 0;
while (p.getYPosition() > 0) {
p.passTime(0.001)
count++;
}
4. Count the number of times you called the pastime method and multiply by 0.001 to get the second to hit the water. Print this value.
As you may know from physical science class, the time it takes for an object to fall and hit the
ground from a height in meters h (neglecting any air resistance) is
time = ?2 ? height/ 9.8
Explanation / Answer
public class MyClass
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Particle p = new Particle(0.0, 350.0, 0.0, 0.0);
int count = 0;
double height = p.getyPosition();
while (p.getyPosition() > 0)
{
p.passTime(0.001);
count++;
}
double hitWaterTime = 0.001 * count;
System.out.println("The number of seconds to hit water is "+ hitWaterTime);
double timeToFall = Math.sqrt(2 * height / 9.8);
System.out.println("The total falling time of the particle is "+ timeToFall);
}
}
class Particle
{
public void passTime(double sec)
{
long millisec = (long)sec * 1000;
try
{
Thread.sleep(millisec);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public double getxPosition()
{
return xPosition;
}
public void setxPosition(double xPosition)
{
this.xPosition = xPosition;
}
public double getyPosition()
{
yPosition--;
return yPosition;
}
public void setyPosition(double yPosition)
{
this.yPosition = yPosition;
}
public double getxVelocity()
{
return xVelocity;
}
public void setxVelocity(double xVelocity)
{
this.xVelocity = xVelocity;
}
public double getyVelocity()
{
return yVelocity;
}
public void setyVelocity(double yVelocity)
{
this.yVelocity = yVelocity;
}
private double xPosition, yPosition, xVelocity, yVelocity;
public Particle(double xPosition, double yPosition, double xVelocity,
double yVelocity)
{
this.xPosition = xPosition;
this.yPosition = yPosition;
this.xVelocity = xVelocity;
this.yVelocity = yVelocity;
}
}
----------------------------------------------------------------------------------------------------------------------
Sample Output:
The number of seconds to hit water is 0.34800000000000003
The total falling time of the particle is 8.439460278709673
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.