ITEC 120
L01c: Interactive program

Objectives

After successfully completing this lab you will be able to develop interactive programs.

Assignment

Change the Relay program you wrote for L01b to make it interactive. That means you will ask the user for input (prompt), read the input, and print results based on the user input.

To do this, we will use the Scanner class to get input from the keyboard. At the top of your program, you will need to import the Scanner class, as shown in green:

/**
  * header comments
  */
  import java.util.Scanner;
  public class Relay
  {

In in the main method, before you read the first input from the user, you will need to create a Scanner object which you will use to call methods of the Scanner class:

Scanner scan = new Scanner(System.in);

System.in means the input you are going to scan comes from the keyboard.

Change your program to ask the user to type the names of the people who ran in the 4x400m relay. After each prompt, you'll use the Scanner object to read in what the user types. For example:

runner1 = scan.nextLine();

The nextLine()method reads information as a String, so you'll need to store that info in a String variable. (Note: You'll need to change some constants to variables.)

Then you'll prompt the user for each person's time (in seconds). For example, if the user typed in "Sara" for the name of the first runner, your program will then ask for Sara's time, like so:

Please enter Sara's time:

If you want to read information as an integer, you'll use the nextInt() method.

Now your program reads in four names and four times, and reports each runner's time plus the total time. Test your program to be sure it is still working correctly.

This program does the same basic operation as the last lab, however, it will produce different output depending on the user's input. This program is more valuable because the output may change without making a change to the code. So you have made this program is more usable.

Submit Your Assignment

Submit your source code file, Relay.java, to the L01c dropbox on D2L.