Question: You can code this as one program that does all of these parts. First create the binary file containing all the information below, then read
You can code this as one program that does all of these parts. First create the binary file containing all the information below, then read back the various parts as described below. The ObjectOutputStream and ObjectInputStream classes can be used to complete all parts. 1. If necessary, fix the Animal, Hawk, Snake and Elephant classes, and the driver program from Assignment #3. All parts below must work with the Animal array. 2. Write out to a binary file the individual instance variables from one of the animals in the Animal array from Assignment 3. You must do this one instance variable at a time, and 2 different data types must be written. 3. Write out to the binary file one object in the Animal array from Assignment 3. (modify all the classes to support serialization) You must do this by writing the entire object to the file, with a single statement. 4. Write out to the binary file the entire Animal array from Assignment 3. That is write the entire Animal array with a single statement (ie NO LOOP IS TO BE USED). Close the binary file, then read data out of it, in the same order you wrote data to it: 5. Read the data for the single object from part 2 above, and use that data to create an object containing that data. Print out the object to verify that you were able to read the data successfully. 6. Read in the object from the binary file from part 3 above. Print out the object to verify that you were able to read the data successfully. 7. Read the contents of the Animals array from part 4, you must read in the entire array of Animal objects with a single statement (ie no loop). Print out the array to verify that you were able to read the data successfully (you will need a loop to do this). java program
Classes of Assignment 3 are :- 1:-
public abstract class Animal { private String species;
public Animal(String speciesin) { species=speciesin; } public abstract String sound(); public abstract String movement();
public String get_species() { return species; } public void set_species(String speciesin) { species=speciesin; }
public boolean equals(Object o) { if (this == o) // self check return true; if (o == null) // null check return false; if (!(o instanceof Animal)) // type check and cast return false;
Animal xzz = (Animal) o;
return (this.species.equals(xzz.species)); }
public String toString() { return species; }}
2:- import java.io.Serializable; public class Driver_Animal implements Serializable { public static void main(String[] args) { //Declaring the objects for Hawk Class Hawk h1= new Hawk("Bird", 67.0); Hawk h2= new Hawk("bird", 65.0); System.out.println("------------------------------------"); System.out.println(h1.toString() + " " + " " + h2.toString());//For printing the data h1.setwings(96.11); System.out.println(" " + h1.equals(h2));//for checking whether true or false //Declaring the objects for Elephant Class Elephant e1=new Elephant("big Mammal",112.1); Elephant e2=new Elephant("Mammal",112.1); System.out.println("-----------------------------------"); e1.setweight(96.22); System.out.println(e1.toString() + " " + " " + e2.toString());//For printing the data System.out.println(" " + e1.equals(e2));//for comparing the two objects //Declaring the objects for Snake Class Snake s1=new Snake("Reptile",11.1); Snake s2=new Snake("Reptile",11.1); System.out.println("------------------------------------------"); s1.setlength(8.66); System.out.println(s1.toString() + " " + " " + s2.toString());//For printing the data System.out.println(" " + s1.equals(s2));//for comparing the two objects } }
3:-
//Represents Elephant class, which is a child class of Animal class public class Elephant extends Animal { //Declaring Variables private double weight;
//Constructor public Elephant(String speciesin, double weightin) { super(speciesin); weight=weightin; }
//Calling methods for abstract class public String sound() { return "trumpet"; } public String movement() { return "walk"; }
// Weight accessor public double getweight() { return weight; }
// Weight mutator public void setweight(double weightin) { weight=weightin; }
//Function for comparing data public boolean equals(Object o) { if (this == o) // self check return true; if (o == null) // null check return false; if (!(o instanceof Elephant)) // type check and cast return false;
Elephant mammal = (Elephant) o;
return this.weight==(mammal.weight); } public String toString() { return super.toString() + " " + "Elephant's sound is " + sound() + " " +"Elephant's movement is " + movement() + " "+ "Elephant's weight is " + weight; } }
4:-
//Represents Hawk class, which is a child class of Animal class public class Hawk extends Animal { //Declaring Variables double wingspan; //Constructor public Hawk(String speciesin, double wingspanin) { super(speciesin); wingspan=wingspanin; }
//Calling methods for abstract class public String sound(){ return "shriek"; } public String movement() { return "fly"; } // Wingspan accessor public double getwings() { return wingspan; }
// Wingspan mutator public void setwings(double wingspanin) { wingspan=wingspanin; }
//Function for Printing public String toString() { return super.toString() + " " + "Hawk's sound is " + sound() + " " +"Hawk's movement is " + movement() + " "+ "Hawk's wingspan is " + wingspan; }
//Function for comparing two objects public boolean equals(Object o) { if (this == o) // self check return true; if (o == null) // null check return false; if (!(o instanceof Hawk)) // type check and cast return false;
Hawk bird = (Hawk) o;
return (super.equals(o)) && this.wingspan==(bird.wingspan); } }
5:-
//Represents Snake class, which is a child class of Animal class public class Snake extends Animal { //Declaring Variables private double length; //Constructor public Snake(String species, double lengthin) { super(species); length=lengthin; } //Calling methods for abstract class public String sound() { return "hiss"; }
public String movement() { return "slither"; }
//length accessor public double getlength() { return length; }
//length mutator public void setlength(double lengthin) { length=lengthin; }
// Function for output public String toString() { return super.toString() + " " + "Snake's sound is " + sound() + " " +"Snake's movement is " + movement() + " "+ "Snake's length is " + length; }
//Function for comparing data public boolean equals(Object o) { if (this == o) // self check return true; if (o == null) // null check return false; if (!(o instanceof Snake)) // type check and cast return false;
Snake reptile = (Snake) o;
return (super.equals(o)) && this.length==(reptile.length); } }
(All in Java Coding).
Thankyou
The program can be like this example:-
import java.io.*;
// writes the contents of an int array to a binary file.
public class WriteBinaryFile
{
public static void main(String[] args) throws IOException
{
// An array to write to the file
int[] numbers = { 2, 4, 6, 8, 10, 12, 14 };
// Create the binary output objects.
FileOutputStream fstream =
new FileOutputStream("Numbers.dat");
DataOutputStream outputFile =
new DataOutputStream(fstream);
System.out.println("Writing the numbers to the file...");
// Write the array elements to the file.
for (int i = 0; i < numbers.length; i++)
outputFile.writeInt(numbers[i]);
System.out.println("Done.");
// Close the file.
outputFile.close();
}
}
import java.io.*;
/**
This program opens a binary file, reads
and displays the contents.
*/
public class ReadBinaryFile
{
public static void main(String[] args) throws IOException
{
int number; // A number read from the file
boolean endOfFile = false; // EOF flag
// Create the binary file input objects.
FileInputStream fstream =
new FileInputStream("Numbers.dat");
DataInputStream inputFile =
new DataInputStream(fstream);
System.out.println("Reading numbers from the file:");
// Read the contents of the file.
while (!endOfFile)
{
try
{
number = inputFile.readInt();
System.out.print(number + " ");
}
catch (EOFException e)
{
endOfFile = true;
}
}
System.out.println(" Done.");
// Close the file.
inputFile.close();
}
}
we have to create the array in our new program
the proffesor did not give me any file, i thnk we have to create
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
