Question: One program to write objects to a file and one to read attributes in from a file and assign them to an arraylist of objects.
One program to write objects to a file and one to read attributes in from a file and assign them to an arraylist of objects.
Part One: Write data for a pet class to a text file in the correct format.
1) Create pet class file with three attributes, String name, int age and String type, include an overloaded constructor
2) Use a static variable in the class to keep track of the number of objects created
3) Create at least three pet objects using pets of your own or that you know
4) Write the attributes for all objects to a text file in the order of name, age, and type. Use space as a delimiter to make sure all the attributes can be read back in to create objects.
5) Here is an example of a text file output:
Sunny 3 bird Thor 3 dog Max 15 cat
Part Two: This program will read data in from a file and create an arraylist of pet objects. Once you have read all the file inputs, you will display only the correctly formatted objects to the user. For your program that reads in data, you will be demonstrating the program twice. Once using your own written text file and the provided input for this assignment. As you read in the data, you will verify the format. For data which has an error in format you will throw an exception, generate a message to the user, skip that data set, and continue the program execution to bring in the remaining data. You must demonstrate use of a try/catch block. The requirements for this assignment are specified in more detail below;
1) Create a Petclass with attributes for String name, int age, and String type.
2) Use the Java FileNotFoundException to provide a message to the user if the file is not available to be opened
3) Demonstrate your code twice using two input files, your file from part 1 and the mypet.txt provided below (which includes a format error for age).
4) Use try/catch block to verify if the input received is in the correct format. If not, then skip that data (you need to skip all the fields associated with the corrupted object in order to start at the beginning of the next object data) and send a message to the user to let them know there was a corrupted data field. Specifically identify an input mismatch if the data for age is not an integer.
5) If the data is good, then create a pet object, initialize it with the data from the file (use an overloaded constructor) and store it in an arraylist.
6) Once all the data is brought in from the file, then display out the contents of the arraylist for the user and list the number of good records (objects) brought in from the file
Hint: Keep in mind that you cannot create the same object over and over again in a loop (you can reset attributes, but not create the memory space location again). It is for that reason that the array list is such a good fit for this program. We can simply use the add method for array list within the loop and it will add a new object to the end of the list each time (no conflict with a previous created object). We can even create the object and initialize it all in the same step as adding it to the array list. Here is an example for an array list called "dogs"; dogs.add (new Dogclass (name, breed, age, AKCnum, vaccine));
The following shows a sample user display:
One entry was skipped due to a format error.
One entry was skipped due to a format error.
There were 3 good records in the file shown below;
My pet 1 is a Canary Toby age 5
My pet 2 is a Shepherd Zelda age 7
My pet 3 is a Pug Gidget age 3
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
