Have the
Vehicle class mutator for number of occupants throw such an exception if the numberOfOccupants
would exceed the maximum number of occupants for the vehicle. What will you need to change in
your ManyVehicles test class?
ANSWERS TO REVIEW QUESTIONS 197
public class TooManyOccupantsException extends Exception {
TooManyOccupantsException( int occupantCount ) {
super( "Vehicle cannot accommodate " + occupantCount );
}
}
In ManyVehicles test class:
try{
System.out.println( "adding people to Ford" );
v1.setOccupants( 6 );
}
catch( Exception e ) {
System.out.println( e.getMessage() );
}
5.11 Change your ManyVehicles class so that it reads from a text file called Vehicles.txt the
specifications for the Vehicles to create. Use a BufferedReader or a Scanner to read the file.
Using a Scanner is probably easier in this case. Here is a sample Vehicles.txt file. The first
word in a line is the class, the second word in a line is color, the third word in a line is the make, the
fourth word is the model, and the fifth word is the maximum number of occupants. In the case of a bus,
there is a sixth word in the line giving the driver??™s name:
Vehicle red Ford F-150 3
Vehicle silver BMW 328i 4
Bus blue GM bus 32 Jones
Vehicle gold Chrysler PTCruiser 4
Skateboard orange WorldIndustries ProBoard 1
If there is a file name on the command line, read from the file; otherwise simply create some hard-coded
examples in the ManyVehicles main method.
Pages:
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519