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?

New Topic/Question
Reply



MultiQuote





|