Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using blue j Overview Your code from Part I should successfully read in data from the file tool_data_1. txt. In this part of the project,

using blue j image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Overview Your code from Part I should successfully read in data from the file tool_data_1. txt. In this part of the project, you will make it easier to deal with different types of vehicle data by introducing subelasses of the Teol class and using inheritance to enhance the usability of your code. In this project, you will be given - more than sufficient advice on how to code this project including advice on potential errors; - more detailed belp in parts of the project that we think you muy find more difficult than other parts of the project: - Bat you will not be given 100\% instructions at every step nor comtinually reminded of potential pitfalls. Learning to debug your own code is a skill honed by practice. The data file toola_data___ txt contains more tool data and, if you examine the data, you will see that the data has been split into two types, electric tool data and handtool data. Step 1 Examine the new data file and you will see that in addition to data for ficlds toolkame, toolCode. tinesporrowed, onLoan, cost and weight. all vehicles have data for the following additiveal fields the electric tool data now includes data for whether that tool is rechargeable and its power. The handtool data also has. some additional different data to the electric tool data. Note that the new data file contains lines such as [Electric tool data] that will not be relevant until later though it should be obvious that these are lubels or flags that signpost the type of data immediascly below this label. Now amend the Tool class so that it has these new fields shown above and modify the readData(0) method so that as well as ignoring comment lines and blank lines, it also (temporarily) ignores lines that seart with [. With these changes you should now be able to read in data from the file tool_data_2.txt. Modify the printbetails () method so that the details of each tool are displayed in the ternninal window in a format similar to that in the data file. Step 2 Reading Before starting to code, you will find it useful to have another look at the slides reliting to the Network project of Chapter 10 and note that: - The object post was declared to be of type Post but its type at runtime was cither Message?ost or PhotoPost. - The type of an object at runtime determined which display () method was executed. For example, if this type was MessagePost then the display () method of the MessagePont class would be executed. This is called method overriding. - Although the display () method of the MessagePost class would be executed in the above example, the method calls its superclass's display () method to output information about a post's fields before printing information about that message posi's fields. - The above notes (referring the Metwork class and the display (I) method) also apply to any similar method which is in both a subclass \& a superclass .. such as readData (). Hint, hint. Step 3 Overview in more detail The tool hire company offers a variety of tools to its customers and these tools are organised as shown in the diagram below : gleetrietool and Hand tool are direct subclasses of Tool and as well as the fields inherited from the Tool class: - the Eleetrietool class has two additional fields: rechargeable and power; - the HandTool class has one additional field; sharpenable i.e. whether that tool is able to be sharpened. You should carefully examine the file tool_data_2.txt (both comments and actual data) to better understand these new fields and to belp decide what type they should be. What we will next do is to write code that - firsly, creates the electrieTool class and links it with the Tool class as shown in the diagram above: - then fills the contents of the fields in both ElectrieTool and Tool with data from too1_data_2. txt: - then creates the Mandtool class, links it with the Fool class as shown in the diagram above and then fills the contents of its fields with data from tool_data_2. txt. Once you understand the above notes ... carry on reading below. As detailed above, let's consider firstly reading the data for electric tools. It is obvious that we could write a method readEleetrieteo1Data () (that would be similar to a method resdrandroo1Data ()) and have a data file eleetriesool_data. txt. Bat, as our model grows, this approach means that we will end up with many "read" methods in the class shop eg. readeleetrietooldata (), readHandroolData (). readAnyothersinilarData () etc. We will also end up with many difterent data files. A more sensible approach is to have one "read" method readroo1Data () in the shop class and all the data in one file. This is the approach that we will adopt here. Remember that in the last step of Part 1, we let a Tool object read its own data. Now, as an Electrietool. object holds the data specific to electric tools and a Handtool object holds the data specific to hand tools, if makes sense for the same idea to be used with these classes i.e. the readroolbata () method in shop lets the data for an ElectricTool object be read by the Electrietool class and similarly for the data for HandTool objects to be read by its own class. If you understood the notes above, then you will realise that - because you are going to use read methods in each class to populate your fields - then your constructor with parameters in Tool is no longer needed and should be deleted. Step 4 Starting coding We shall now add our first subclass: Electrierool. Stant coding by - adding the subclass gleetrierool which should have the fields meationed above; - then introduce a printDetails () method in the ElectrieTool class that overrides the Tool class's printDetails ( ) method, in a similar manner to that employed by the print () method of the Network projoct, - similarly, introduce a readbata () method in the Electrietool class. This should be the method that is used at run-time to read the data into this object. Java Programming Project Part 2 March 2023 At this point, your project should still compile but it will not read the data corresponding to the extra fields of the Eleetriefool class until you rename the resdroo1Data() method in the Shop as readDate (). The reason for this shoald be obvious from the lecture slides but if unsure ask a tutior For now, test your code by reading data from the new text file, tool_data_2. txt. Does it nun correctly ? Probably not (.) as you will probably get a nun-time erros... This is because the code has met something that it did not expect: a as in e.e. [ElectrieTool data] For the moment, add code here thar ignores any lines which start with a bracket as you did in Step I above. Run your code again. Does it run correctly this time? No, it will not, and it is expected that you will get an InputekiseatehExeeption. This means that the scanner has met a piece of data of a different type than it expected to meet eg. a Doolean instead of a string. Iry to discover the error by looking at this data file to see what is new that coeld cause this exeeption before reading the next paragraph. You should find that the exeeption is because you are trying to read the hand tool data (which is below the eloctric tool data in the data file) using an Eleetriefool object. It is possible that a different type of exception is caused. To debug this error, check the data file to see which datn caused the error by adding a printin () statement to your read method to print out which data was being read in at the timse of the error, and then debag accordingly. At this point, your project should still compile and read the data corresponding to the fields of the Fleetrietool class. both those stored in the class"' own fields and those stored in its superclass. Finally, for this step, a little bit of refactoring: because we are delegating the reading of data to the subclasses, we no longer need an inner while loop in readroolData () (if you did have one that is 9 ), and this can safely be removed now. However, you might like to think whether the check (for your remaining was le loop) that you have is the most appropriate. Step 3 Dealing with data specific to electric tools You should have code in readoeta () in Stop correspooding to the preudo-code below (or code that can be easily modified to this). Note that not all the details have been included in the pseudo-code. It is important to note that this basic loop structure will not change - in particular, yoe go round the loop once for each line of the data file. If you now look at the data file tool_data_2.txt, yoe will see that it is structured like this [8lectrictool data] data for an electrie tool data for another electrie tool [handtool data] data for a handtool data for anether handtool i.e. cach block of data for a particular type of tool is preceded by a flag (such as. [Electrie tool data]) so that we know what kind of data to expect. You can assume that any line that starts with a t. ignoting any leading spaces, is a flag denoting a type of data. So, each time you meet a flag, you need to record in your code what type of data comes next in the file. Why ? Ask a tutor if unsure. Use a local variable namsed typeofbata for this parpose. The lines following a flag are expected to be data which is to be read into your model. Qaber hnts: - When your code corresponding to the statement labelled ///A is cuccuted: the readData () method of the Electrictool class should make sure that all the data for an gleetrietool object will be read in: the ElectrieTool object assignod to the Tool variable (with its fields now pruperly initialised) will be stored in toolList: at this stage, the data for hand tool objects can be ignored. - The data file are essentially "comma separated" rather than "space sepurated" and enay also contain spaces before or after a comma. So, if you have not already done so, amend your call to usedeidmiter () so that it uses a regular expression that repsesents zero or more spaces, followed by a comma, followed by zero of more spaces. Your Linux module should be helpful here ! Step 4 Dealing with handtool data Next add the Band7ool class (with the relationships shown in the diagram on Page 2) to the project and amend the readbata () method so that it recognises this new data. If you have successfully completed the previous steps with suitably structured code then this should be straightforward althoogh, as wsual, you will need to pay athention to detail. Although, at this stage, you could simply deal with electric and hand tool data, beticr marks will be awarded for dealing with other possitilities i.e. for "unexpected flags". If such a flag is cacountered when reading from the file, print out a warning message though you will have to add a little more code than a single printlin(0. Step 5 In this step, you will start to extend the shop model to allow for more types of thop items. These additional items are accessories and are not available to be hired: these are items for sale only. Accessories can be items which are perishable e.g. liquids such as cleaning agents, or accessories may be workwear items e.g. gloves and other protective clothing. Anybow, what you need is a bierarcby of classes similar to this: Firstly, think carefully before you start coding. Star by deciding which of the existing fields should be in ShopItem and which should be in Tool. Once you have decided which fields go into these two classes, start coding by adding a new shopI tem class to the project. This will require simple yet careful refactoring! Test your code by reading data from the file tool_data_2, txt and you will get an InputisismatehException. As you know, this means that the scanner has met a pioce of data of a different type that it did not expect to meet. Try to work cut why you get this exception (bat do not try to rectify this problem) before readiag the indented text below: Remember that in the Setwork example, in order to priat the values of each field, the subclass's print method was called which in turn called the print method in its superelass. - Note that it could call the superclass metbod before or after printing its own fields or indeed, partway through printing its own fields. - But, once the superclass method is called, all of the fields in the superclass will be printed. The point is that there is no mechanism for printing some of the superclass fields, then printing some of the subclass fields then printing the rest of the superelass fields. - Make sure this is clear ask a tulor if unsure. Now the way that values are distributed to the fields of a subelass and to the fields of a superelass behaves in the same way as printing these values. In ofher words, a subclass method receives all of the data for all of its fields (including the ones inherited from its superelass) and then sends some of that data to the superelass (to be stored in its fields) and then storing the remaining data in the fields of the subclass. You can think of the subclass read method receiving the data in one chunk - effectively the line of data which is then split into separate pieces. Once - and cnce only - a "chunk" of that data is passed to the superelass. Because there is no to-ing and fro-ing between subclass and superelass in order to fill the superelass fields, this means that the "chunk" of data passed to the superelass must coatain all of the data for the superclass fields. Why could this be a problem? Well, the data that needs to be passed to shopr teen by Toel are values for itenkame, itemCode and cost. And these three values are not collected together in a sequence in our data file. That is why you get an InputMi smatehException. - Make sure this is clear: again ask a tutor if unsure. Essentially, we need to re-order our data so that i temktame, i temCode and cost are collected together on each line in the data file. This has been done for you in the file tool_data_3, txt which you should now use to test your latest code. The output of printA1IDe tails () should be the same as before you started this step, and, of course, formatted well. Remember to avoid any duplication of code in your clases. You should also note that, since we are new dealing with items other than tools, the data that was labelled toolkame should now be labelled itentiame. Next, you should introduce the classes Accessory, Perishable and workwear into the model. You will also need to extend the readDa ta () method in the shop class but if you have successfully completed earlier steps then this should be straightforward. You can test your code by using the file items_al1. txt which contains data for glectriefools and HandTools as well as for Perishables and for Workwear. Finally for this step, think (from Chapter 12 though mentioned in Chapter 10 ) about what type of classes some of your classes should be. You should aim to complete this part of the project within twe weeks when Part 3 of the preject will be made available

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions

Question

=+4. Comparing stocks and government bonds, which has more risk?

Answered: 1 week ago