Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is a Binary file I/O example problem. This is a pretty beginner cla

ID: 3837934 • Letter: T

Question

This program is a Binary file I/O example problem. This is a pretty beginner class so we haven’t discussed any major topics. We are using abstract methods and exception handling to deal with this problem/ arrays and other basic java topics binary object files.I will include an example as reference to how much we have used. Essentially this is an excersie on Binary file I/O

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 (String), age (int), and weight (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 Pet’s  age and for the Pet’s 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, then copy the contents of the original file to a temporary file, close original file and delete, and rename the temporary file to the name specified by the user

Prompt for the new pet’s 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 all objects from the file (assume that they are  Pet objects).

Display all pets in the file, pausing after every 6-8 pets.

Please Submit listings of your Pet  class and the class containing main. Also provide PrintScreens (or listings) of a session in which several Pets are entered, and those pets are then displayed.

EXAMPLE OF CODE NOT PART OF THE QUESTION FOR CONTEXT

Explanation / Answer

public class BinaryWriteDemo {

private static final String FILE_NAME = "binary.dat" ;

public static void main(String[] args) {

ObjectOutputStream outputStream = null ;

        try {

            outputStream = new ObjectOutputStream(new FileOutputStream(FILE_NAME));

        }

        catch (IOException e) {

            System.out.println("Couldn't create output stream -- aborting...") ;

            System.exit(0) ;

        }

try {

for (int i = 1 ; i <= 10 ; i++) {

                outputStream.writeInt(i * 10) ;    // 10, 20, 30...

            }

            for (int i = 1 ; i <= 10 ; i++) {

                outputStream.writeByte(i * 5) ;    // 5, 10, 15...

            }

            for (int i = 1 ; i <= 10 ; i++) {

                outputStream.writeDouble(i * -1) ; // -1.0, -2.0, -3.0...

            }

            // Write two Strings onto the file

            outputStream.writeUTF("This is the first string written to the file.") ;

            outputStream.writeUTF("And another string...") ;

outputStream.close() ;

        }

        catch (IOException e) {

            System.out.println("Error writing to or closing the file -- aborting...") ;

            System.exit(0) ;

        }

    }

}

import java.io.ObjectInputStream;

import java.io.FileInputStream;

import java.io.IOException;

public class BinaryReadDemo {

    private static final String FILE_NAME = "binary.dat" ;

    public static void main(String[] args) {

ObjectInputStream inputStream = null ;

        try {

            inputStream = new ObjectInputStream(new FileInputStream(FILE_NAME));

        }

        catch (IOException e) {

            System.out.println("Couldn't open input stream -- aborting...") ;

            System.exit(0) ;

        }

        try {

System.out.print("Reading ten integers    ") ;

            for (int i = 1 ; i <= 10 ; i++) {

                System.out.print(inputStream.readInt() + " ") ;

            }

            System.out.print(" Reading ten bytes    ") ;

            for (int i = 1 ; i <= 10 ; i++) {

                System.out.print(inputStream.readByte() + " ") ;

            }

            System.out.print(" Reading ten doubles     ") ;

            for (int i = 1 ; i <= 10 ; i++) {

                System.out.print(inputStream.readDouble() + " ") ;

            }

System.out.print(" Reading two strings ") ;

            for (int i = 1 ; i <= 2 ; i++) {

                System.out.println("   " + inputStream.readUTF()) ;

            }

            inputStream.close() ;

        }

        catch (IOException e) {

            System.out.println("Error reading from file -- aborting...") ;

            System.exit(0) ;

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote