University of Toronto Faculty of Arts and Science CSC324 -- Principles of Programming Languages Spring 1998 Department: Computer Science Instructor: John Mylopoulos Date: March 17, 1998 Assignment 3: Object-Oriented Programming with Java Due Date: Thursday, 10:00pm, April 9, 1998 This assignment counts for 15% of the final grade All programs should be written in Java and should be well documented and thoroughly tested. Make sure you use good object-oriented programming style; marks will be deducted for programs that look like translated Lisp or Prolog. Assignments should be handed in electronicaly, using the 'submit' command available on CDF machines. Here is how to use it for assignment 3. submit -N a3 csc324h a3.java where 'submit' is the command to submit your assignment electronically, '-N a3' indicates that you wish to create a subdirectory 'a3' in your submit directory containing that assignment, 'csc324h' is this course, and 'a3.java' is the file with your submitted assignment. If, for some reason, you decide later that you want to overwrite your earlier submission (you cannot "unsubmit"), you must explicitly tell submit to do so with the '-f' switch: submit -N a3 -f csc324h a3.java For more information on how to use submit, type 'man submit' to access manual documentation. -------------------------------------------- Problem Statement [150 marks] [An Animal Database] Write a program which manages animal descriptions for a zoology lab. The lab identifies animals in different African countries, and then tracks each animalUs movements (through means that need not concern us) until the animal dies. For each animal, the lab maintains the following information: 7 Type of animal: can take as values all the animal types mentioned below. 7 Name: this is a string which uniquely identifies each animal, e.g., RclydeS, RtweetyS,... 7 Country of residence, e.g., RkenyaS, RegyptS, TsouthAfricaS 7 Date of discovery, e.g. 21/05/1997 7 A list of its subsequent observation dates and locations, having the form ((date2,location2),(date3,location3)...) 7 Date of death (sometime after the last date of observation on the list ofobservations). In addition to these general attributes which apply to all animals, the database stores for each animal that is being tracked, values of attributes which are particular to individual classes of animals. Values for these attributes are given only for the first date of identification of an animal and are not updated throughout the observation period. For each attribute, we note the range of allowable values: 7 For birds, which are animals, wing span (length of open wings, from tip to tip): 2cm to 300cm size of their eggs: 1cm to 40cm range of flight in one day: 0m to 10,000m 7 For mammals, which are also animals, hair colour : {black, brown, white, yellow, grey, towny} milk production per day: 0 to 2liters 7 For carnivores, which are mammals, amount of meat eaten per day: 10gm to 10,000gm 7 For ungulates, which are mammals, hoof size: 5cm to 30cm 7 For cheetahs, which are carnivores, habitat: {trees, caves} hair colour: {tawny} 7 For tigers, which are carnivors hair colour: {yellow} 7 For giraffes, which are ungulates, neck length 100cm to 200cm 7 For zebras, which are ungulates, hair colour: {white} 7 For ostriches, which are birds, range of flight: 0m size of eggs: 20cm to 40cm 7 For penguins, which are birds. range of swim per day: 50m to 2,000m 7 For albatrosses, which are birds, amount of fish eaten per day: 50gms to 500gms ------------------------------------------- The Program Database operations Your program should offer four database operations: 7 newAnimal(Type [] x) -- which creates a new animal object in the database, based on the description provided in array x. The type of the array may be int or String, depending on how you choose to represent values. The format of the array has as follows: [animalType,animalName,attr1,val1,attr2,val2,...,attrn,valn] 7 newObservation(String animalName,Date date, Country country)) -- which adds another (date,location) pair to the observation list for the animal in question; Instead of the Country type, you may use int or String, depending on how you choose to represent country values. 7 deathReport(String animalName, Date date) -- adds the reported death date for a given animal. 7 report(String animalName) -- prints a report on an animal which includes its type and all its attribute-value pairs. Consistency Checking Your program should check all the values going into the database to make sure they are meaningful and consistent with the rules given above. This includes checking dates, checking the observation dates on an animalUs observation list to make sure they are temporally sequential, also checking all attribute values. Your program will need to define a class hierarchy for all the animal types, with a constructor which creates an instance for each animal class. You may want to check all the constraints for attribute values of a given class in its constructor methods. If any values are illegal, the program should reject them and ask for another value from the user. You may want to use the built-in Java class Hashtable (in java.util) to create a hash table for all animal names, thereby checking the uniqueness of animal names. here is how you can use HashTable: Hashtable animals = new Hashtable(); animals.put(RclydeS, new Elephant(description1)); animals.put(RtweetyS, new Albatross(description2)); ... Of course, before you create an entry in the animals hashtable for a new animal, you need to check if the name is already being used and what is the type of the animal. Non-numerical attribute values may be represented as either strings, or as constants. For example, for colour values, you may want to declare public interface Colours { public static final int BLACK = 0; public static final int BROWN = 1; public static final int WHITE = 2; public static final int YELLOW = 3; public static final int GREY = 4; public static final int TAWNY = 5; } If you use this encoding, the strings from the file input must be converted to their integer encoding. Program I/O The input to your program will come from an ascii file, to be provided by the instructor. You should not assume that the entries of the file are consistent with the problem description. There may be entries where the animal type is not in the list which appears above, or the attribute values are not consistent with the problem statement. In these cases, the program should be asking the user to provide a meaningful animal type or attribute value, or to instruct discarting of the animal description. You can assume, however, that each line of the file will have the format animalType animalName attr1 val1 attr2 val2 ... attrn valn The tokens of each line will be separated by one or more blanks. You will be provided with a class called FileTokenizer which reads all the contents of a file, given its file path, and returns an array of strings, each string corresponding to one line of the file. You can then use the built-in Java class StringTokenizer (in java.util) to read the tokens of each string and use them to build an animal description. Here is an example of use of FileTokenizer: FileTokenizer fl = new FileTokenizer(R~jm/animalsS); Array inputAnimals = fl.readAndGetTokens(); Here is an example of use for StringTokenizer: StringTokenizer st = new StringTokenizer(Rthis is a lionS); while (st.hasMoreTokens()) { println(st.nextToken()); } This will print this is a lion