Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 23 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
23
Dung lượng
190,83 KB
Nội dung
ANSWERS TO REVIEW QUESTIONS 197 v1 = new Vehicle( "Ford", "Mustang", 2, "red" ); v2 = new Vehicle( "BMW", "328i", 4, "silver" ); v3 = new Vehicle( "Chrysler", "PT Cruiser", 4, "gold" ); System.out.println( "There are " + Vehicle.vehicleCount + " vehicles." ); System.out.println( "Make of v1 (Ford): " + v1.getMake() ); System.out.println( "Model of v2 (328i):" + v2.getModel()); System.out.println( "Color of v3 (gold: " + v3.getColor()); System.out.println( "Max occupants of v1 (2): " + v1.getMaxOccupants() ); double accel = v1.changeSpeed( 70 ); System.out.println( v1.getModel() + " accelerated by " + accel + "mph to " + v1.getSpeed() + "mph." ); v1.setMake( "Chevrolet" ); v1.setModel( "Malibu" ); v1.setColor( "white" ); v1.setSpeed( 60 ); System.out.println( "v1 is now a " + v1.getColor() + " " + v1.getMake() + " " + v1.getModel() + " going " + v1.getSpeed() + "mph." ); v4 = new Skateboard( "Mako", "Shark", "red" ); accel = v4.changeSpeed( ); System.out.println( "v4 is a " + v4.getMake() + " " + v4.getModel() + " " + v4.getColor() + " Skateboard going " + v4.getSpeed() + "mph."); accel = v4.changeSpeed( 22 ); System.out.println( "The Skateboard is now going " + v4.getSpeed() + "mph." ); v2.reFuel( 11.3, 295.); //should be 26.1 mpg System.out.println( "The " + v2.getMake() + " mileage: " + v2.fuelEconomy() ); try{ System.out.println( "Refueling skateboard" ); v4.reFuel( 0., 5.); } catch( Exception e ) { System.out.println( e.getMessage() ); } } } 5.10 Write a class that extends Exception and is called TooManyOccupantsException 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? 198 ANSWERS TO REVIEW QUESTIONS 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( ); } 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 Vehicle silver BMW Bus blue Vehicle gold Skateboard orange Ford F-150 328i GM bus Chrysler PTCruiser WorldIndustries ProBoard 32 Jones 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 This is the code snippet that decides how to create the Vehicles and then creates them: if( args.length == ) { //There is no file name on the command line, so // make up some vehicles with hard coding vehicles[0] = new Vehicle( "Ford", "Mustang", 2, "red" ); vehicles[1] = new Vehicle( "BMW", "328i", 4, "silver" ); vehicles[2] = new Vehicle( "Chrysler", "PT Cruiser", 4, "gold" ); System.out.println( "There are " + Vehicle.vehicleCount + " vehicles." ); System.out.println( "Make of vehicles[0]: " + vehicles[0].getMake() ); System.out.println( "Model of vehicles[1] (328i): " + vehicles[1].getModel() ); System.out.println( "Color of vehicles[2] (gold): " + vehicles[2].getColor() ); System.out.println( "Max occupants of vehicles[0]: " + vehicles[0].getMaxOccupants() ); } ANSWERS TO REVIEW QUESTIONS 199 else {//There is a file name on the command line, so // read from a file using Scanner Scanner sc = null; try { sc = new Scanner( new File(args[0]) ); } catch(Exception e) { System.out.println( e.getMessage() ); } int index = 0; while( sc.hasNext() ) { //read line in file vehicle = sc.next(); color = sc.next(); make = sc.next(); model = sc.next(); maxOccupants = sc.nextInt(); if( vehicle.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 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 columns with a line of column headings at the top You will find this easy to using the println() method of the PrintWriter 200 ANSWERS TO REVIEW QUESTIONS import java.io.*; public class Squares { public static void main( String[] args ){ PrintWriter output = null; try { output = new PrintWriter( new File( "Squares.txt" ) ); } catch( Exception e ) { System.out.println( e.getMessage() ); System.exit(1); } output.println( "number\tsquare\n \t " ); for( int i = 1; i