equals( "Vehicle" ) ){
vehicles[index] = new Vehicle(
make, model, maxOccupants, color );
}
else if(vehicle.equals( "Bus" ) ) {
vehicles[index] = new Bus( make, model,
maxOccupants, color, sc.next() );
}
else if(vehicle.equals("Skateboard") ) {
vehicles[index] = new Vehicle(
make, model, maxOccupants, color );
}
else {
System.out.println(
"Unrecognized vehicle type: "
+ vehicle );
System.exit(1);
}
System.out.println( "Created "
+ vehicles[index].getModel() );
index++;
}//while
}//else
Note that you must also add these import statements at the very beginning of your source code file, above
even the declaration of public class ManyVehicles {...
import java.util.Scanner; //for access to the Scanner class
import java.io.*; //for access to file I/O
5.12 Write a Java program that iterates through the integers from 1 to 20, computing the square of each
number and writing the information to a file called squares.txt. Use a PrintWriter to write the
file of the first 20 integers and their squares. Arrange for 2 columns with a line of column headings at
the top. You will find this easy to do using the println() method of the PrintWriter.
ANSWERS TO REVIEW QUESTIONS 199
import java.io.*;
public class Squares {
public static void main( String[] args ){
PrintWriter output = null;
try {
output = new PrintWriter( new File(
"Squares.
Pages:
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521