Question
The program should be developed step by step and In the last part you should: The program should be developed step by step and In
The program should be developed step by step and In the last part you should:
The program should be developed step by step and In the last part you should:
- Vehicle:
- Implement Fileable interface.
- Implement the writeData (PrintWriter out) method, it should be used to write comma separated values in all class members to file.
- Implement the readData (Scanner in) method, it should be used to read comma separated values from file and assign them to class members.
Car and Bicycle:
- They will overwrite the two aforementioned methods and add functionality to write / read their own class members.
TestVehicles:
- When the program starts, all vehicles that were stored should be read from file and added to the ArrayList object, when the program ends, all vehicles must be sorted and written to file.
- When writing to file it must be taken care of whether it is a Car or Bicycle object being written, here you can use the object class method getClass (). GetName () - it will return the name of the class. When reading, this name must be read in and an object of this type must be created, here methods must be used in Class as shown below.
Scanner in = new Scanner (file) .Local (Locale.US); in.useDelimiter ( ","); String vehClass = in.next (); // reads the class name from the class veh1 = Class.forName (vehClass) file ; // creates Class object for specified class name (String) Vehicle veh = (Vehicle) veh1.newInstance (); // creates new instance of Vehicle
Example of the application's print is:
Vehicle read from file: Name: Monark 1 Color: yellow Price: 4000 Model: 1993 Serial #: BC100 Direction: 0 Speed: 0.00 Buying date: 1993-01-20 Gears: 10 Production date: 2017-12-29 Vehicle read from file: Name: DBS 2 Color: pink Price: 5000 Model: 1994 Serial #: 42 Direction: 0 Speed: 0.00 Buying date: 1995-07-24 Gears: 10 Production date: 2017-11-21 Vehicle read from file: Name : Volvo 740 Color: blue Price: 85000 Model: 1985 Serial #: 1010-11 Direction: 0 Speed: 0.00 Buying date: 1988-09-04 Power: 120 Production date: 2017-03-05 Vehicle read from file: Name: TestCar Color: Indigo Price: 123456 Model: 2018 Serial #: 69-420 Direction: 0 Speed: 0.00 Buying date: 2018-01-29 Power: 240 Production date: 2018-01-29 Vehicle read from file: Name: Testarossa Color: red Price: 1200000 Model: 1996 Serial #: A112 Direction: 0 Speed: 0.00 Buying date: 1997-05-15 Power: 350 Production date: 2018-01-12 1 ... ................................ New car 2 ............... ................ New bicycle 3 ...................... Find vehicle by name 4 .... .......... Show data about all vehicles 5 ....... Change direction of a given vehicle 6 .................... ..... Test clone method 7 .................. Test driveable interface 8 ................... ........... Exit program ............................. Your choice? 2 Input bicycle data: Name: TestBicycle Color: Maroon Price: 12345 Model: 1234 Serial #: 69-42 Gears: 12 1 ................................... New car 2 .. ............................. New bicycle 3 .................. .... Find vehicle by name 4 .............. Show data about all vehicles 5 ....... Change direction of a given vehicle 6 ....... .................. Test clone method 7 .................. Test driveable interface 8 ...... ........................ Exit program ........................ ..... Your choice? 8 Vehicle written to file: Name: Monark 1 Color: yellow Price: 4000 Model: 1993 Serial #: BC100 Direction: 0 Speed: 0.00 Buying date: 1993-01-20 Gears: 10 Production date: 2017-12-29 Vehicle written to file: Name: DBS 2 Color: pink Price: 5000 Model: 1994 Serial #: 42 Direction: 0 Speed: 0.00 Buying date: 1995-07-24 Gears: 10 Production date: 2017-11-21 Vehicle written to file: Name: TestBicycle Color: Maroon Price: 12345 Model: 1234 Serial #: 69-42 Direction: 0 Speed: 0.00 Buying date: 2018-01-29 Gears: 12 Production date: 2018-01-29 Vehicle written to file: Name: Volvo 740 Color: blue Price: 85000 Model: 1985 Serial #: 1010-11 Direction: 0 Speed: 0.00 Buying date: 1988-09-04 Power: 120 Production date: 2017-03-05 Vehicle written to file: Name : TestCar Color: Indigo Price: 123456 Model: 2018 Serial #: 69-420 Direction: 0 Speed: 0.00 Buying date: 2018-01-29 Power: 240 Production date: 2018-01-29 Vehicle written to file: Name: Testarossa Color: red Price: 1200000 Model: 1996 Serial #: A112 Direction: 0 Speed: 0.00 Buying date: 1997-05-15 Power: 350 Production date: 2018-01-12
Fileable interface:
import java.io.IOException ; import java.io.PrintWriter ; import java.util.Scanner ; public interface Fileable { void writeData ( PrintWriter out ) throws IOException ; void readData ( Scanner in ) throws IOException ; } Tip: You can collaborate on the different parts of the task by writing one part of the assignment on one person's PC and the other part on the other PC, you can learn a lot from collaborating on the task and get different views on the task and how it should solved. When you save the date object to file, you can avail the designer of the GregorianCalendar class that receives year, month and day as whole numbers and creates a date object based on them. Then you can write year, month and day to file using the get method defined in the Calendar class, productionDate.get (Calendar.DAY_OF_MONTH).
G, Vehicle colour fname fserialNr O model String String String Driveable MAX_SPEED_CAR double MAX_SPEED_BIKE double Cloneable )accelerate(int) (breaks(int) void 1Comparable rice compareTo(T) int O&direction double speed O a buyingDate Calendar Scanner mVehicle) T&power Ogears Vehicle(String, String, int, int, String, int) & productionDate Calendar Calendar & productionDate msetAllFields(0) , o turnLeft(int) Ob turnRight(int) Car) 0b Car(string, string, int, int, string, int, int) msetAllFields() mturnRight(int) Bicycle() Bicycle(String, String, int, int, String, int, int) msetAllFields(0) mturnRight(int) void void mwriteData(PrintWriter) @ readDataScanner) mturnLeft(int) maccelerate(int) mturnLeft(int) maccelerate(int) String String getName() getColour() getPrice() getModel() getSerialNrO getDirection() breaks(int) breaks(int) mwriteData(PrintWriter) mwriteData(PrintWriter) mreadData(Scanner) readData(Scanner) getPower() getGears() String Calendar mgetProductionDate() msetPower(int) msetProductionDate(Calendar) Calendar mgetProductionDate() msetGears(int) msetProductionDate(Calendar) mclone mtoString() double etSpee getBuyingDate() setName(String) String Object String a clone mtoString() Object String 0 . SetColour(string) setPrice(int) msetModel(int) setSerialNr(String) @ setDirection(int) msetSpeed(double) Fileable writeData(PrintWriter) readData(Scanner) , void setBuyingDate(Calendar) compareTo(Vehicle mclone() mtoString() Object String Powered by yFiles
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started