THINK FIRST·CODE LATER

← All labs

Free fall and the 1 / 2 trap

Problem

An object dropped from rest falls a distance d = ½ g t² and reaches the speed v = g t (g = 9.8 m/s²). FreeFall reads the time in seconds and prints:

  • the value of the tempting but wrong expression 1 / 2 * G * seconds * seconds;
  • the correct distance, the final speed in m/s and in km/h, and the average speed (distance / time).

For the input

3

the program prints

With 1 / 2 first: 0.0
Distance fallen: 44.10 m
Speed at the end: 29.40 m/s
Speed at the end: 105.8 km/h
Average speed: 14.70 m/s

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l4-9-data-1.txt

3
What your program must print for l4-9-data-1.txt
With 1 / 2 first: 0.0
Distance fallen: 44.10 m
Speed at the end: 29.40 m/s
Speed at the end: 105.8 km/h
Average speed: 14.70 m/s

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l4-9-data-1.txt

Windows PowerShell does not understand <:

Get-Content l4-9-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.