


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A java programming assignment where students are required to create two classes, taxi and fleet. The taxi class should have methods for a tostring(), getservicename, and costoftrip. The fleet class should have an array of taxi services, a constructor that initializes the fleet's array of taxis from a text file, and methods for bestfareamount, bestfaretaxi, howbig, and getservicenames. Students must handle errors and save the fleet data to a text file.
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Description: Design and implement two Java classes one called “Taxi”, one called Fleet (one called Program9 will be supplied to you). Put these into 3 separate files. The Taxi class should support: a toString() method to display the service name and rate data [just as you did in Program8]. It should also supply an instance method that returns a double that is the computed cost of a ride for a given distance based on the particular Taxi [just as you did in Program 8]. Taxi should obviously have a constructor [just like in Program 8]… Taxi should also support a getServiceName instance method that returns the name of the given service since that instance variable will be private. hmmm it looks like most of your code from Program8 for Taxi should work here (except of course with the driver portion of it removed). The Fleet class will :”have a” array of Taxi services where that Fleet constructor takes a single String as its argument (namely the name of the text file that holds the data need to initialize the Fleet’s array of Taxis). See a sample of what that file might look like below. The first line of the text-file will hold an integer that tells you how many services (potentially) will be contained in the rest of the text file [you may assume that the number stated on line 1 of the file is accurate with respect to the number of lines to follow]. Each of the other lines in the file will contain data about a single Taxi service in a format called comma-delimited (I,e, a comma separates the first data field from the second and the second from the third). Note that the Service names might have un-needed leading or trailing spaces that you should remove and they may need a fix to their capitalization. The first letter of each word should be capitalize [all others should be left as is]. Thus in example below “mellow cab” should become “Mellow Cab”, “speedy” should become “Speedy”, “allStar” should become “AllStar”, and “Step-on-it” should be left exactly as is. Hint words start in col. 1 after trimming or they have a space in front of them. If a given line does not have exactly 2 commas in it your constructor for Fleet should produce an error message identifying the line number and text of the bad line and should then continue onward ignoring that line. Note: you will need to track “lastInUse” for your array since the first line tells you how big the array MIGHT be (if no lines are rejected). The Fleet class should also have its own toString method that will display (one per line) the data from each individual Taxis in the Fleet. [Hint: it should use the toString from Taxi!] The Fleet should have an instance method [much like some code from Program8] that finds the best fare for a given distance (ie it returns a double) and takes a double as an argument [call this one bestFareAmount]. Fleet will have another instance method that returns a Taxi (actually a Taxi object reference) called bestFareTaxi( that works just like bestFareAmount except that it returns a reference to the Taxi service that offers that best fare for the given int number of miles. Fleet should also offer an instance method called howBig that returns the number of Taxis in the Fleet (5 in the case of the code shown below). The Fleet should also support an instance method called getServiceNames that returns an array of Strings, namely the names of each of the services in the array of Taxis. And finally, Fleet should offer a method called saveFleetData that will take a String as an argument (namely the pathname of the file to which the Fleet data is to be saved). The fleet data should be saved as a text-file with the following format: line 1 will be an int (the number of Taxis to follow – 5 for the sample input below) followed by the numbered toString output produced by each of the Taxis in the Fleet. An example of part of that output is shown below. Sample of what input file to be used might look like: 6 mello cab ,0.45,2. speedy ,0.50,1. , // bogus line here should lead to error message. a+, 0.55,1. allStar,0.85,0. Step-on-it, 0.52,1. Sample first three lines from text-output file produced: Taxi Services in the Fleet: 5
Error Checking: You must check for a bad line (not exactly two commas) in each of the lines beyond the first in the specified text file and generate an error message in that case and ignore the line. You may assume (after you put a test text file in correct location) that the file will be there at runtime. You may also assume that the output file name that our test program will use will put the file into the c:\java subdirectory (so make sure you have one). Coding Requirements The Taxi and Fleet classes will be in their own files (as will our Program9). The data members of both Taxi and Fleet will be private (thus limiting access to use of methods). Your name must appear in the title bar of all output. Hints: (1) The String class will provide numerous useful methods here! (2) You will have to add “throws IOException” to the END of the header line for the Fleet constructor. (3) You will need to add “throws IOException” to the end of the method header for saveFleetData. (4) The expression (char)(someChar – 32) will return the upper case version of someChar if it was a lowercase letter! That is that ‘a’ <= someChar && someChar <= ‘z’. (5) This expression might be useful to you somewhere: bestFareTaxi(miles).costOfTrip(miles) Example: String input = “Hello”; input.equalsIgnoreCase(“hello”); // This would return true input.equalsIgnoreCase(“heilo”); // This would return false You must submit a printed and electronic (CD or Floppy) copy of your commented program along with screen captures of the input and output for all test runs AND a UML diagram for the Taxi and Fleet classes in a manila envelope. Problem Analysis - all inputs, processes, and outputs described. You must also include any formulas or constants in the problem analysis. Pseudocode – These steps should be detailed enough for you to transform each English-like statement into Java statements.