Exercise: Physics Projectile Simulation
Problem statement
A physics student needs to simulate throwing a ball straight up into the air. Given an initial velocity (
The simulation should start at
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
. 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 (
) and terminate only when the ball returns to the ground (where and the calculated height is or less). Display a final message indicating the ball has hit the ground.
Constraints
Use a
while (true)loop combined with abreakstatement to implement the exit strategy.Use
double.TryParsefor 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 abreakstatement.To implement the exit strategy, use an
ifcondition to check if the ball has moved past the start () AND has touched or passed the ground ( ). Use
+to join strings and variables together, such asConsole.WriteLine("Time: " + time);.Inside the loop, remember to increment the
timevariable by 1 after each calculation.Use
\tbetween “Time” and “Height” in yourConsole.WriteLineto keep the columns straight.
Exercise: Physics Projectile Simulation
Problem statement
A physics student needs to simulate throwing a ball straight up into the air. Given an initial velocity (
The simulation should start at
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
. 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 (
) and terminate only when the ball returns to the ground (where and the calculated height is or less). Display a final message indicating the ball has hit the ground.
Constraints
Use a
while (true)loop combined with abreakstatement to implement the exit strategy.Use
double.TryParsefor 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 abreakstatement.To implement the exit strategy, use an
ifcondition to check if the ball has moved past the start () AND has touched or passed the ground ( ). Use
+to join strings and variables together, such asConsole.WriteLine("Time: " + time);.Inside the loop, remember to increment the
timevariable by 1 after each calculation.Use
\tbetween “Time” and “Height” in yourConsole.WriteLineto 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