Page 1 of 1

(Java) Step 3 Overview in more detail In Part 1, we had only a Vehicle class but the new data file vehicle_data_2.txt no

Posted: Tue Jul 12, 2022 8:04 am
by answerhappygod
(Java)
Step 3 Overview in more detail
In Part 1, we had only a Vehicle class but thenew data file vehicle_data_2.txt now contains lotsof specific data for cars, vans and trucks. Comments in the fileexplain what each item of data means.
This file also contains lines such as
[Car data]
that will not be relevant until later. All we do is to note thisfor the moment.
Going forward, our aim is to write code thatwill read data for different types of vehicles. Some of the datafor each type of vehicle is the same kind of data though someadditional data makes each type of vehicle distinct. And so itmakes sense to introduce subclasses of the Vehicleclass.
However, before starting to code, read carefully thisintroduction.
The vehicle hire company offers cars, vans and trucks to itscustomers and the vehicles are organised as shown in the diagrambelow:
Java Step 3 Overview In More Detail In Part 1 We Had Only A Vehicle Class But The New Data File Vehicle Data 2 Txt No 1
Java Step 3 Overview In More Detail In Part 1 We Had Only A Vehicle Class But The New Data File Vehicle Data 2 Txt No 1 (12.6 KiB) Viewed 71 times
Car and Commercial are directsubclasses of Vehicle, whilst Vanand Truck are subclasses of theCommercial class.
As well as the fields inherited from theVehicle class:
As well as the fields inherited from theCommercial class:
You should carefully examine the filevehicle_data_2.txt (both comments and actual data)to better understand these new fields and to help decide what typethey should be.
What we will next do is to write code that
Once you understand the above notes … carry onreading below.
As detailed above, let’s consider firstly reading the data forcars. It is obvious that we could write a methodreadCarData() (that would be similar to a methodreadVanData()). But, as our model grows, thisapproach means that we will end up with many "read" methods in theclass ReservationSystem e.g.readCarData(), readVanData(),readTruckData(),readAnyOtherSimilarData() etc. If we alsodecide to separate our data into separate classes for each typethen we will also end up with many different data files. A moresensible approach is to have one "read" method, as we have now, inthe ReservationSystem class and all the data inone file. This is the approach that we will adopt here.
Remember that in the last step of Part 1, we let aVehicle object read its owndata. Now, as a Car object holds thedata specific to cars and a Truck object holds thedata specific to trucks and a Van object holds thedata specific to vans, it makes sense for the same idea to be usedwith these classes i.e. the readVehicleData()method lets the data for a Car object be read bythe Car class and similarly for the data forTruck and Van objects to be readby their own classes.
If you understood the notes above, then you will realise that –because you are going to use read methods in each class to populateyour fields – then your constructor with parameters inVehicle is no longer needed and should be deleted.Also, any default constructor that you have inVehicle is no longer needed for similar reasonsand can be removed.
For each of the steps below, do not forget to update yourTest class.
Car Vehicle Van Commercial Truck