473,597 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

19 New Member
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) ;
}
}
Oct 22 '09 #1
2 2886
pbrockway2
151 Recognized Expert New Member
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 Recognized Expert New Member
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
4787
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 in the for loop. I am including the portion of the code : #Get the connection to the database my $dbh = &getConnection(); my @acodes;
6
1386
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
3794
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
3153
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
8
1682
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 elem.innerText = "1." Then elem.click() End If End If Next elemIndex
4
1606
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
1439
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 = (rs_faktura.Fields.Item("customer_ID").Value) & (rs_faktura.Fields.Item("bill_ID").Value)
4
1622
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 array for the string but not sure. Here is my code so far. I've included the strings I'm trying to add as variables for right now. Please have look, and I would greatly appreciate any suggestions you may have on including a string loop synced with my...
17
2629
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 shoud give me something like: $Db_user = davi7024 Now I want to check that this IS a unique user id so
0
7886
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8035
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
3886
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.