Page 434 - Introduction to Programming with Java: A Problem Solving Approach
P. 434
400 Chapter 10 Arrays and ArrayLists
Figures 10.19a and 10.19b contain the heart of the program—the FlightTimes class. In Figure 10.19a, the constructor initializes the flightTimes and cities instance variable arrays with the data
/*********************************************************************
*
FlightTimes.java
Dean & Dean
This manages a table of intercity flight times.
**********************************************************************/
*
*
*
import java.util.Scanner;
public class FlightTimes
{
private int[][] flightTimes; // table of flight times
private String[] cities;
// cities in flightTimes table
//*******************************************************************
public FlightTimes(int[][] ft, String[] c)
{
}
flightTimes = ft;
cities = c;
Apago PDF Enhancer
//*******************************************************************
// Prompt user for cities and print associated flight time.
public void promptForFlightTime()
{
Scanner stdIn = new Scanner(System.in);
int departure;
// index for departure city
Print the number-city legend.
}
int destination; // index for destination city
for (int i=0; i<cities.length; i++) ⎫ {⎪
⎬ System.out.println(i+1 + " = " + cities[i]);⎪
}⎭ System.out.print("Enter departure city's number: "); departure = stdIn.nextInt() - 1; System.out.print("Enter destination city's number: "); destination = stdIn.nextInt() - 1; System.out.println("Flight time = " +
flightTimes[departure][destination] + " minutes.");
// end promptForFlightTime
Figure 10.19a
FlightTimes class that displays intercity flight times—part A