Question: (JAVA) In this project, you will create a class named Pet , then write and read several objects of that class. Here is a description

(JAVA) In this project, you will create a class named Pet, then write and read several objects of that class.

Here is a description of the Pet class.

  • Instance variables will be name (a String), age (an int), and weight (a double)
  • Provide all setters and getters
    • In the setter for name, make sure that it is not all blanks or the empty string
    • In the setter for the Pets age and for the Pets weight, make sure that the value is non-negative (if it is negative, then print an error message and exit).
  • Provide equals and toString methods
  • Provide a no-arg constructor and a full constructor. Both constructors should use the appropriate setters to set the value of instance variables rather than accessing the instance variables directly.
  • Make this class Serializable.

In main, prompt the user to enter the name of a file, and whether the user will be entering Pets into the file or displaying all Pets in the file.

If entering Pets and writing to a file:

  • If the file already exists, open the file so that new Pet objects will be appended to any existing Pet objects.
  • Prompt for the new pets name, age, and weight
  • Create a new Pet object
  • Write the new Pet object to the output file
  • Loop entering new pets until the user is done entering.

If reading from a file:

  • Verify that the file exists and open for reading objects
  • Read and display all objects from the file (assume that they are Pet objects), pausing after every 6-8 pets.

Submit program files for your Pet class the class containing main, and the class used to append to an existing ObjectOutputStream . Also provide PrintScreens (or listings) of a session in which several Pets are entered, and those pets are then displayed.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!