Filling in the Formula

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Did you get the condition exactly correct? Check below.

Filling in the Formula

The program so far:

import java.util.Scanner;

// User picks ending value for time, t.
// The program calculates and prints 
// the distance the brick has fallen for each t.
//
class FallingBrick
{
  public static void main (String[] args )  
  {
    final  double G = 9.80665;  // constant of gravitational acceleration
    int    t, limit;            // time in seconds; ending value of time
    double distance;            // the distance the brick has fallen
    Scanner scan = new Scanner( System.in );

    System.out.print( "Enter limit value: " );
    limit = scan.nextInt();

    // Print a table heading
    System.out.println( "seconds/tDistance"  );  // '/t' is tab
    System.out.println( "-------/t--------"  ); 

    t  =  0 ;
    while (  t <= limit )    
    {

        // calculate distance

        // output result

      t = t + 1 ;
    }

  }
}

The loop body will execute for t = 0, 1, 2, ..., limit. At the end of the last execution, t is changed to (limit+1). But the conditional expression will not allow execution back into the loop body when t is (limit+1).

Now let us calculated the distance for each value of t using the formula:

distance = (1/2)*G*t2

Translate the formula into a Java statement to fill the first blank.

QUESTION 16:

Fill in the two blanks. Use a tab character in the output statement. Watch out: there are two traps!