435,347 Members | 2,113 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,347 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 