COMP 1671 , Fall 2008

Assignment 4

Due November 15 at 6PM

Programming Assignment Four: People and Pets

Write a Java program to process two files: People.txt and Pets.txt. The test files will be made available to you next week tuesday. They will have no errors in them so you do not need to worry about error checking.

An example person file is:

Bob 1001 18 155
Sue 1002 25 120
Tom 1003 35 160
Mary 1004 23 135
John 1005 25 195

Where the first field is the name, second is an id, the third is an age, and the last is a weight.

And example pet file is:

cat Fluffy 1001 5
cat Stimpy 1005 15
dog Marmaduke 1002 9
dog Snoopy 1002 12
cat Nermal 1002 1

Where for both cats and dogs the first field specifies the pet is a cat or a dog, the second is the name of the pet, the third is the id number of it's owner, the last is the age of the pet.

Your program should be composed of the following classes: Person, Cat, Dog, Driver. The Driver class will have the main() function and it will read the two files. First it will read in the people.txt file and create a List of Person objects. Then it will read the pets.txt file and for each pet create a Dog or Cat object and add it to the correct Person object in the people list. You should use an iterator over the people list to find the right Person object, based on the owner id of the pet, and then add the pet to the person object. Obviously the Person class will need addCat() and addDog() methods. Your Person class should have a list of dogs and a list of cats so that a Person can own many cats and dogs. After loading the files your program should print out the following:

  1. All the people. This should include all of their pets.
  2. All the cats owned by people over 25.
  3. All dogs who have an age less than 5 and are owned by people 25 and younger.
  4. All people over 50 who do not own pets.
  5. For each person who is aged 20-30 inclusive, print out their name, the number of cats they have, and the average age of their cats. For example, if there are two people, Joe who is 22 and Sue who is 30, Joe has a 10 year old cat and a 14 year old cat and Sue has three cats aged 4 6 and 8. Then, it would print out: Joe, 2 cats, average age = 12. Sue, 3 cats, average age = 6.