By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,491 Members | 1,168 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,491 IT Pros & Developers. It's quick & easy.

Do While loop...Please take a look at my code...

P: 19
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.DecimalFormat;
import java.util.Scanner;
import java.util.Random;


public class BaseballTest
{
//Static method simulate
public static double simulate(Baseball Ball)
{
double t=0;
double i=t+0.01;
t=i;

Ball.update();
while(Ball.getY()>=0)
{
System.out.println("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(distance);//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.println("The fielder is"+distance+" meters from home plate.");
System.out.println("He throws the ball at a speed of "+speed+" m/sec");
System.out.println("i.e,, "+165.39 +"km/hr");//? to km /h
System.out.println("Choose the angle (in degrees) at which he should ");
System.out.println("throw the ball so that it lands at home plate>");

Baseball B= new Baseball(distance,speed);//instantiating a baseball object
String ans;
do
{
//if angle entered is out of range do a while loop
do{
System.out.println("Enter an angle between 0 and 90");
angle=kb.nextDouble();
}
while(angle >90);


B.Calculate(angle);//Calculating Vx and Vy
simulate(B);
System.out.println("Vx is"+B.getVx());
System.out.println("Vy is"+ B.getVy());


if(B.getX()<10)
{
double q = 0.5*(Math.asin((distance*9.81)/Math.pow(

System.out.println("Congratulations: good angle!");
System.out.println("The calculated angles are "+q +" and"+ (90-q));
break;
}
else
System.out.println("The Ball flew long by"+ (-distance));
double q = 0.5*(Math.asin((distance*9.81)/Math.pow(
System.out.println("The calculated angles are "+q +" and"+ (90-q));
System.out.println("Would you like to guess again? (y = yes)");
ans=kb.next();
}
while (ans.equalsIgnoreCase("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(double angle)
{
vx= v*Math.cos(angle);
vy= v* Math.sin(angle);
}
}
Oct 22 '09 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 151
Could you please highlight the code and use the code button (#) because it's a bit hard to read.

You say "the probelm is:The value of vertical position is always negative.", but that sounds more like a claim about the cause of the problem than a description of the problem itself. What input do you give? What output do you get? What output did you expect?

edit: I've just realised BaseballTest is the class being run...
Oct 22 '09 #2

Expert 100+
P: 151
From the simulate method:

Expand|Select|Wrap|Line Numbers
  1. Ball.update();
  2. while(Ball.getY()>=0)
  3. {
  4.     System.out.println("t = "+ t +" x = "+ Ball.getX()+" y = "+Ball.getY());
  5. }
  6.  
Notice that there's no update going on within the the while loop. So once you enter this loop you never leave it.

Secondly if the loop does end at some point Ball.getY() will have returned a negative number.
Oct 22 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.