Exercise: Physics Projectile Simulation

Construct a physics simulation that uses loops and arithmetic expressions to calculate and display the real-time height of a falling object.

Problem statement

A physics student needs to simulate throwing a ball straight up into the air. Given an initial velocity (v0v_0) and the constant of gravity (g9.8m/s2g \approx 9.8m/s^2), the height (hh) at any given second (tt) can be calculated using the formula:

The simulation should start at t=0t = 0 and continue calculating the height every second until the ball hits the ground (whenever the height becomes 0 or less).

Task requirements

  • Prompt the user to enter the initial upward velocity in meters per second.

  • Safely parse the input into a numeric format to prevent application crashes.

  • Create a loop that calculates the height for the current second at t=0t=0.

  • Inside the loop, print a formatted table with “Time” and “Height” columns using the tab escape sequence.

  • The loop must continue past the initial launch (t=0,h=0t=0, h=0) and terminate only when the ball returns to the ground (where t>0t > 0 and the calculated height is 00 or less).

  • Display a final message indicating the ball has hit the ground.

Constraints

  • Use a while (true) loop combined with a break statement to implement the exit strategy.

  • Use double.TryParse for numeric safety.

  • Use the tab escape sequence (\t) to align the columns in your output table.

  • Use string concatenation (the + operator) to display the output, as string interpolation has not been introduced yet.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • In a while (true) loop, the code runs indefinitely until it hits a break statement.

  • To implement the exit strategy, use an if condition to check if the ball has moved past the start (t>0t > 0) AND has touched or passed the ground (h0h \le 0).

  • Use + to join strings and variables together, such as Console.WriteLine("Time: " + time);.

  • Inside the loop, remember to increment the time variable by 1 after each calculation.

  • Use \t between “Time” and “Height” in your Console.WriteLine to keep the columns straight.

Exercise: Physics Projectile Simulation

Construct a physics simulation that uses loops and arithmetic expressions to calculate and display the real-time height of a falling object.

Problem statement

A physics student needs to simulate throwing a ball straight up into the air. Given an initial velocity (v0v_0) and the constant of gravity (g9.8m/s2g \approx 9.8m/s^2), the height (hh) at any given second (tt) can be calculated using the formula:

The simulation should start at t=0t = 0 and continue calculating the height every second until the ball hits the ground (whenever the height becomes 0 or less).

Task requirements

  • Prompt the user to enter the initial upward velocity in meters per second.

  • Safely parse the input into a numeric format to prevent application crashes.

  • Create a loop that calculates the height for the current second at t=0t=0.

  • Inside the loop, print a formatted table with “Time” and “Height” columns using the tab escape sequence.

  • The loop must continue past the initial launch (t=0,h=0t=0, h=0) and terminate only when the ball returns to the ground (where t>0t > 0 and the calculated height is 00 or less).

  • Display a final message indicating the ball has hit the ground.

Constraints

  • Use a while (true) loop combined with a break statement to implement the exit strategy.

  • Use double.TryParse for numeric safety.

  • Use the tab escape sequence (\t) to align the columns in your output table.

  • Use string concatenation (the + operator) to display the output, as string interpolation has not been introduced yet.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • In a while (true) loop, the code runs indefinitely until it hits a break statement.

  • To implement the exit strategy, use an if condition to check if the ball has moved past the start (t>0t > 0) AND has touched or passed the ground (h0h \le 0).

  • Use + to join strings and variables together, such as Console.WriteLine("Time: " + time);.

  • Inside the loop, remember to increment the time variable by 1 after each calculation.

  • Use \t between “Time” and “Height” in your Console.WriteLine to keep the columns straight.

// Physics Projectile Simulator
const double gravity = 9.8;
double time = 0;

Console.WriteLine("=== Projectile Simulation ===");
Console.Write("Enter initial velocity (m/s): ");
string input = Console.ReadLine();

// 1. TODO: Parse velocity safely using double.TryParse
// 2. TODO: Print table headers (Time and Height)
// 3. TODO: Implement a while(true) loop with a break for the exit strategy
// 4. TODO: Use string concatenation (+) for output
Starter code for the projectile simulation exercise