473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
2 2875
pbrockway2
151 Expert 100+
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
pbrockway2
151 Expert 100+
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

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

Similar topics

6
by: Ravi | last post by:
Hi All, I am trying to execute a select statement using the DBI module of perl in a for loop. I am getting a strange behaviour, the select statement is excuting correctly only for the last element...
6
by: JP SIngh | last post by:
Hi All I run a query which gets data from two tables. I store in MainContracts Table ContractId InitiatedBy
32
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
8
by: Microsoft | last post by:
I have the following loop the length contains somewhere around 1000+ items: For elemIndex = 0 To len - 1 elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement) If elem.tagName = "A" Then If...
4
by: Byte | last post by:
The following code will not work for me: x = 1 while x == 1: print 'hello' x = input('What is x now?: ') while x == 2: print 'hello again'
7
by: Kim | last post by:
Hi, why doesn't this work inside a loop, gives me the wrong "kontrollnummer" (increases it as the loop goes on) <% Dim kontrollnummer Dim raknare Dim Summa Dim referens referens =...
4
by: chellemybelle | last post by:
Hello, I basically have made a little cheezy slideshow and would like to add captions to each pic as it loops through. I've tried everything I can think of. I'm assuming I might need to use an...
17
by: jeddiki | last post by:
I am wanting to create a unique user id for my clients, so I am taking the first 3 letters of there name and adding the 4 digits from the rand() function If the clients namd is david, this...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.