A-A+

更改ManyVehicles类 使之从名为Vehicles.txt的文本文件中读取要创建的V

2022-08-12 10:14:39 问答库 阅读 195 次

问题详情

更改ManyVehicles类,使之从名为Vehicles.txt的文本文件中读取要创建的Vehicle的规范。使用BufferedReader或Scanner读取文件。在这里,使用Scanner可能更容易。下面给出了一个示例Vehicles.txt文件。一行中的第一个单词是颜色,第二个单词是制造厂,第三个单词是型号,第四个单词是最大乘客数: red Ford F-150 3 siiver BMW 328i 4 blue GM bus 32 gold Chrys ler PTCruiser 4 orange WorldIndustries ProBoard 1


请帮忙给出正确答案和分析,谢谢!

参考答案

正确答案:如果命令行上有一个文件名就读取该文件;否则只需在ManyVehicles类的main方法中创建一些手工编码的示例。下面给出了决定如何创建Vehicles以及创建这些示例的代码段:if(args.1ength==0){//There is no file name on the command lineso//make up some vehicles with hard codingvehicles[0]=new Vehicle("Ford""Mustang"2"red");vehicles[1]=new Vehicle("BMW""328i"4"silver");vehicles[2]=new Vehicle("Chrysler""PT Cruiser"4"goid");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());}else{ //There is a file name on the command lineso//read from a file using Scanner.Scanner sc=null;try{SC=new Scanner(new Fi]e(args[0]));}catch(Exception e){System.out.println(e.getMessage());}int index=0;while(sc.hasNext()){ //read 1ine in filevehicle =sc.next();c010r =sc.next();mske =sc.next();model =sc.next();maxOccupants=sc.nextInt();if(vehicle.equals("Vehicle")){vehicles[index]=new Vehicle(makemodelmaxOccupantscolor);}else if(vehicle.equals("Bus")){vehicles[index]:new Bus(makemodelmaxOccupantscolorsc.next());}else if(vehicle.equals("Skateboard")){vehicles[index]=new Vehicle(makemodelmaxOccupantscolor);}else{System.out.println("Unrecognized vehicle type:"+vehiCle);System.exit(1);}System.out.printin("Created"+vehicles[index].getModel());index++;} //while} //else注意:还必须在源代码文件的开头添加下面这些import语句甚至要使它们位于public class ManyVehicles{…的声明上面:import java.util.Scanner; //for access to the Scanner classimport java.io.*; //for access to file I/O
如果命令行上有一个文件名,就读取该文件;否则,只需在ManyVehicles类的main方法中创建一些手工编码的示例。下面给出了决定如何创建Vehicles以及创建这些示例的代码段:if(args.1ength==0){//Thereisnofilenameonthecommandline,so//makeupsomevehicleswithhardcodingvehicles[0]=newVehicle("Ford","Mustang",2,"red");vehicles[1]=newVehicle("BMW","328i",4,"silver");vehicles[2]=newVehicle("Chrysler","PTCruiser",4,"goid");System.out.println("Thereare"+Vehicle.vehicleCount+"vehicles.");System.out.println("Makeofvehicles[0]:"+vehicles[0].getMake());System.out.println("Modelofvehicles[1](328i):"+vehicles[1].getModel());System?out.println("Colorofvehicles[2](gold):"+vehicles[2].getColor());System.out.println("Maxoccupantsofvehicles[0]:"+vehicles[0].getMaxOccupants());}else{//Thereisafilenameonthecommandline,so//readfromafileusingScanner.Scannersc=null;try{SC=newScanner(newFi]e(args[0]));}catch(Exceptione){System.out.println(e.getMessage());}intindex=0;while(sc.hasNext()){//read1ineinfilevehicle=sc.next();c010r=sc.next();mske=sc.next();model=sc.next();maxOccupants=sc.nextInt();if(vehicle.equals("Vehicle")){vehicles[index]=newVehicle(make,model,maxOccupants,color);}elseif(vehicle.equals("Bus")){vehicles[index]:newBus(make,model,maxOccupants,color,sc.next());}elseif(vehicle.equals("Skateboard")){vehicles[index]=newVehicle(make,model,maxOccupants,color);}else{System.out.println("Unrecognizedvehicletype:"+vehiCle);System.exit(1);}System.out.printin("Created"+vehicles[index].getModel());index++;}//while}//else注意:还必须在源代码文件的开头添加下面这些import语句,甚至要使它们位于publicclassManyVehicles{…的声明上面:importjava.util.Scanner;//foraccesstotheScannerclassimportjava.io.*;//foraccesstofileI/O

考点: