7 Replies - 1254 Views - Last Post: 28 September 2009 - 05:18 AM Rate Topic: -----

#1 randomfun   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-September 09

Need Help with program stuck

Post icon  Posted 25 September 2009 - 09:11 AM

Consider a baseball player throwing a ball from the outfield to home plate. He throws the ball at an angle q to the ground and with an initial speed v, such that the time taken for the ball to go up and come down is the same as the time taken for the ball to travel the horizontal distance from his position to home plate. The player of course can calculate the required speed and angle in his head with no training in calculus or physics. In this assignment you set a random distance and speed (roughly appropriate for a baseball game) and ask the user to guess the proper angle at which to throw the ball so that it arrives close to home plate. Your program then simulates the flight of the ball and shows how close it lands to home plate. The idea of simulation is that the position of the ball is calculated and updated every DELTA seconds during the course of its flight. The formula for updating the y-position is

y = y + vy * DELTA ;

where vy is the vertical velocity, and we assume that at time zero y is 0. The vertical velocity is updated by

vy = vy - g * DELTA ;

where g = 9.81 m/sec2 is the acceleration due to gravity near the earth. There is no acceleration in the horizontal direction. Therefore, the velocity in the x-direction remains constant, and the horizontal x-position is updated by

x = x + vx * DELTA.

Given an initial speed v and angle , the horizontal and vertical speeds are

vx = v * cos(q) ; vy = v * sin(q) ;

1. Write a class Baseball with private instance variables x, y, vx, and vy. Make appropriate constructors so that these variables can be initialized when constructing a Baseball object. Give the class a static variable DELTA which you should set to 0.01, so that the position can be updated each 0.01 seconds. Also make a static variable for gravitational acceleration G. Give an instance method update() which updates the instance variables, as described above. Provide instance methods for accessing each of the four instance variables (e.g., public double getX()).

2. Write a driver class BaseballTest.java. In the main method create a random number generator, and generate a random distance d in the range 60 to 120 meters. Generate a random speed in the range sqrt(dG) and sqrt(dG) + 20. Tell the user the speed and the distance, and ask the user to guess the proper angle of the throw to make the ball arrive at home plate.

3. Start a do-while loop which will continue as long as the user wants to try guessing the proper angle. Within this loop have another do-while loop where you get the guess of angle from the user, accepting only guesses in the range 0 to 90 degrees. After getting a valid guess, calculate vx and vy from the formulas above. Create a baseball with these speeds and an initial x-value which is the negative of the distance to the plate and an initial y-value of 0. Call a method simulate (described in the next step) to run a simulation of the flight of the baseball. The method returns the distance from the plate when the ball lands. If the ball lands within ten meters of the plate, congratulate the user. If the ball lands farther than 10 meters away, ask the user if she would like to guess again. If not, tell the user the correct answer, as calculated in step 5.

4. Write a method public static double simulate(Baseball ball) which runs the simulation, updating the ball's position each DELTA seconds. Print out the position, however, only each second. Also print out the last position of the ball before the next iteration would make the y-position be negative.

5. The correct answer can be calculated from the formula y(t) = -0.5 gt2 + vyt = 0 and d = vxt. The answer, as you can check, is: q = 0.5 * arcsin(dg/v2), as well as 90 - q.

Here is a sample run:

The fielder is 104.0 meters from home plate.
He throws the ball at a speed of 45.94 m/sec
i.e,, 165.39 km/hr
Choose the angle (in degrees) at which he should
throw the ball so that it lands at home plate>
Enter an angle between 0 and 90
100
Enter an angle between 0 and 90
30
t = 00.00 x = -104.00 y = 00.00
t = 01.00 x = -64.21 y = 18.11
t = 02.00 x = -24.43 y = 26.42
t = 03.00 x = 15.36 y = 24.91
t = 04.00 x = 55.14 y = 13.60
t = 04.69 x = 82.60 y = 00.07
The ball flew long by 82 meters.
Would you like to guess again? (y = yes)
y
Enter an angle between 0 and 90
20
t = 00.00 x = -104.00 y = 00.00
t = 01.00 x = -60.83 y = 10.86
t = 02.00 x = -17.66 y = 11.90
t = 03.00 x = 25.51 y = 03.14
t = 03.21 x = 34.58 y = 00.05
The ball flew long by 34 meters.
Would you like to guess again? (y = yes)
y
Enter an angle between 0 and 90
15
t = 00.00 x = -104.00 y = 00.00
t = 01.00 x = -59.62 y = 07.03
t = 02.00 x = -15.25 y = 04.26
t = 02.43 x = 03.83 y = 00.05
Congratulations: good angle!
The calculated angles are 14.45 and 75.55


That was the question here is what i have done so far and not sure how to use the specific formulas indicated.


public class baseball
{
public static void main(String[] args)
{
  Scanner scanner = new Scanner(System.in);  
Random generator = new Random();
int distance,angle_entered;
double gravity = 9.8;
//generate a random number
distance = generator.nextInt(60) + 60;
System.out.println("The fielder is " + distance + " meters from home plate.");
System.out.println("He throws the ball at a speed of ");
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.");
// ask user to enter a interger between 0 and 90
System.out.println("Enter an angle between 0 and 90");
angle_entered = scanner.nextInt();

if (angle_entered is < 0) || (angle_entered > 90)
{
  System.out.println("Not a valid angle, Try again");
}
}
}




For some reason its not reading the if staement at the bottom should i be doing something different?

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help with program stuck

#2 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Need Help with program stuck

Posted 25 September 2009 - 09:16 AM

You have a parentheses problem. Yours don't match.

// yours

if (angle_entered < 0) // STOPS HERE (you have more code, but that ")"  after the zero closed the entire if.)

// correct...(you don't need parentheses)

if (angle_entered < 0 || angle_entered > 90)


You don't have to use parentheses, but when you do, make sure you match them up correctly, or you'll get some very unexpected results.

Hope this helps! :)

This post has been edited by Locke: 25 September 2009 - 09:17 AM

Was This Post Helpful? 1
  • +
  • -

#3 randomfun   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-September 09

Re: Need Help with program stuck

Posted 25 September 2009 - 06:42 PM

does anyone know to do this part? i know how to generate a random number but im not sure how to make a random number between 2 numbers. Please help if you can.


Generate a random speed in the range sqrt(dG) and sqrt(dG) + 20. Tell the user the speed and the distance, and ask the user to guess the proper angle of the throw to make the ball arrive at home plate.
Was This Post Helpful? 0
  • +
  • -

#4 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Need Help with program stuck

Posted 25 September 2009 - 07:06 PM

Have a look at this.

You can use that and add a certain value that you get back to get a random number between 2 numbers.
Was This Post Helpful? 0
  • +
  • -

#5 randomfun   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-September 09

Re: Need Help with program stuck

Posted 27 September 2009 - 08:47 AM

im gonna have to use a while statement for this problem. So i already switched iot from the if statement to the while loop. The example that you linked me to i dont quite understand. I understand the sqrt(dG) but im not sure how to make it between sqrt(dG) + 20. Could you link me to another example perhaps? i dont just want you to give me the answer.

This post has been edited by randomfun: 27 September 2009 - 09:06 AM

Was This Post Helpful? 0
  • +
  • -

#6 randomfun   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-September 09

Re: Need Help with program stuck

Posted 27 September 2009 - 09:11 AM

also is there a way to block this topic to only restricted plp or at least delete the question / my code above so that some people dont come and copy it??
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Need Help with program stuck

Posted 27 September 2009 - 12:00 PM

View Postrandomfun, on 27 Sep, 2009 - 08:11 AM, said:

also is there a way to block this topic to only restricted plp or at least delete the question / my code above so that some people dont come and copy it??

You can always edit your previous topic and remove the code
Would be polite to let a note explaining why you did so thow
Was This Post Helpful? 0
  • +
  • -

#8 randomfun   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-September 09

Re: Need Help with program stuck

Posted 28 September 2009 - 05:18 AM

Can someone explain to me how to make the t,z,y values increase and stop at a certain point? i have the code to increase the t values like t = 1.00 but it doesnt stop at a certain value. for example above it stopped at 3.21 but in my program it keeps going. How would i make it stop to make the correct calculation?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1