Hi,
Could you please rewrite the program for me?I tried my best and the program still does not do what it has to do.
I have to write a code that generates random speed and distance .it ask the user for angle and start calculating the vertical and horizantal positions.
when the vertical position gets negative program should stop and check the horizantal position and print out different messeges based on the value of the horizantal speed.
I wrote the code,but for some reason the code is not working properly.
I belive that it 's a logical error and the probelm is:The value of vertical position is always negative.
Please take a look at my code and fix it if you can:
import java.text.Decim alFormat;
import java.util.Scann er;
import java.util.Rando m;
public class BaseballTest
{
//Static method simulate
public static double simulate(Baseba ll Ball)
{
double t=0;
double i=t+0.01;
t=i;
Ball.update();
while(Ball.getY ()>=0)
{
System.out.prin tln("t = "+ t +" x = "+ Ball.getX()+" y = "+Ball.getY ());
}
return Ball.getX();
}
public static void main(String[]args)
{
Scanner kb=new Scanner(System. in);
Double distance,speed, sqrDistance, angle;
distance = (double) (60 + Math.random() *60);// generate nb between 60 and 120
sqrDistance= Math.sqrt(dista nce);//square root of distance
speed = (double) (sqrDistance + Math.random() *20);//Generate a random speed in the range sqrt(dG) and sqrt(dG) + 20.
System.out.prin tln("The fielder is"+distance+ " meters from home plate.");
System.out.prin tln("He throws the ball at a speed of "+speed+" m/sec");
System.out.prin tln("i.e,, "+165.39 +"km/hr");//? to km /h
System.out.prin tln("Choose the angle (in degrees) at which he should ");
System.out.prin tln("throw the ball so that it lands at home plate>");
Baseball B= new Baseball(distan ce,speed);//instantiating a baseball object
String ans;
do
{
//if angle entered is out of range do a while loop
do{
System.out.prin tln("Enter an angle between 0 and 90");
angle=kb.nextDo uble();
}
while(angle >90);
B.Calculate(ang le);//Calculating Vx and Vy
simulate(B);
System.out.prin tln("Vx is"+B.getVx() );
System.out.prin tln("Vy is"+ B.getVy());
if(B.getX()<10)
{
double q = 0.5*(Math.asin( (distance*9.81)/Math.pow(…
System.out.prin tln("Congratula tions: good angle!");
System.out.prin tln("The calculated angles are "+q +" and"+ (90-q));
break;
}
else
System.out.prin tln("The Ball flew long by"+ (-distance));
double q = 0.5*(Math.asin( (distance*9.81)/Math.pow(…
System.out.prin tln("The calculated angles are "+q +" and"+ (90-q));
System.out.prin tln("Would you like to guess again? (y = yes)");
ans=kb.next();
}
while (ans.equalsIgno reCase("y"));
}
}
public class Baseball
{
private double x;
private double v;
private double y;
private double vx;
private double vy;
public static double DELTA=0.01;
private static double G=9.81;
public Baseball(double myx,double myv)
{
y=0;
x=myx;
v=myv;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getVx()
{
return vx;
}
public double getVy()
{
return vy;
}
public void update()
{
y = y + vy * DELTA ;
vy = vy - G * DELTA ;
x = x+vx * DELTA;
}
public void Calculate(doubl e angle)
{
vx= v*Math.cos(angl e);
vy= v* Math.sin(angle) ;
}
}