Class Exercise: CSC162 Review


Use eclipse for this exercise.

  1. Create a class for pets. There should be fields for name, name of owner, species (dog, cat, etc.), breed (basset hound, domestic shorthair, etc.), age (a double), and weight (a double).
  2. Create a default constructor that does nothing and a constructor that will initialize all fields using values passed in as parms.
  3. Create a method isSpecies which has one parm, a String. Return true if the species in this is the same as the parm, otherwise return false. For example, if the parm is "hamster", then the method will return true if this is a hamster, and will return false if it's not.
  4. Create a toString method that returns a String containing all fields. You can create a multi-line String by including newlines; make sure the String is nicely formatted.
  5. Do NOT create any set or get methods.
  6. Client/demo/driver code (in a separate class):
    1. Write a method to read Pets from a file. The method will have one parm which is an array of Pets. The input file is named pets.dat. The data for each field of Pet will be on a separate line. Read in the fields for a Pet, create a Pet object, and add it to the array. Read till end of file. The method will return the number of Pets that were read in and stored in the array. You can assume that the array is big enough to hold all of the Pets in the file.
    2. Write a method to print all Pets in the array. The parms are the array and the number of Pets in the array. Don't print array elements that don't contain a Pet.
    3. Write a method to print all Pets of a given species. The species, the array, and the number of Pets in the array are parms.
    4. In main, create an array of Pets; make your array large enough to hold 25 Pets. Call the method to read Pets, then use the previous method to print all cats and all dogs. Print a heading for the cats and for the dogs. Then print a heading for all Pets and print all of the Pets in the array.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2020