Lab Serialization and Binary IO Please use Serialization Thi
Lab Serialization and Binary I/O
Please use Serialization
This lab introduces students to reading and writing binary data and objects. After completing this exercise, students should know how to:
Read binary data
Write binary data
Read serialized objects
Write serialized objects
Write the following four programs based that are based on Programming Exercises 17.6, 17.7, 17.14, and 17.15:
Modify DataSetBook and Book to be serializable
Write a program Cereal that saves Book data to a file. Cereal takes two command line arguments: an int and a String. Cereal uses the int argument to control the number of Book objects that it creates and adds to a DataSetBook object. Cereal uses the String argument as a filename for a file containing the serialized data. Books are given some automatically assigned changing title and author values, (such as \"book0\" \"book1\" etc, and \"auth0\", \"auth1\" etc). The page values should be a random value between 100 and 200.
Write a program Enigma that takes a single String as a command line argument. Enigma should read the file specified by the String argument, add 5 to each byte, and leave the altered data values in a file whose name is the command line argument. Note that this \"updating in place\" is the most difficult part of this lab: java Enigma sophie.dat should read file sophie.dat, and upon completion, leave the modified data in file sophie.dat.
Write a program Bombe that takes a single String as a command line value. Bombe works just like Enigma, but subtracts 5 from each byte of the file. If the user runs java Enigma sophie.dat followed by java Bombe sophie.dat, the data read by Enigma will be exactly the data written by Bombe.
Write a program Reconstitute that takes a single String command line argument. Reconstitute uses the argument as a filename for a file that contains the data serialized by Cereal. Read the file, restore the DataSetBook and its Book contents, and print them.
--------------here is Book.java code-------------------
public class Book {
   
    private String author;
    private String title;
    private int pages;
   public Book(String auth, String titl, int pag) {
        author = auth;
        title = titl;
        pages = pag;
    }
   public int getPages() {
        return pages;
    }
   @Override
    public String toString() {
        return \"Book [author=\" + author + \", title=\" + title + \", pages=\" + pages + \"]\ \";
    }
   // this is a really poor way to compare Book objects, but it will work for us
    /* (non-Javadoc)
    * @see java.lang.Object#equals(java.lang.Object)
    */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Book other = (Book) obj;
        if (author == null) {
            if (other.author != null)
                return false;
        } else if (!author.equals(other.author))
            return false;
        if (pages != other.pages)
            return false;
        if (title == null) {
            if (other.title != null)
                return false;
        } else if (!title.equals(other.title))
            return false;
        return true;
    }
}
------------------------------------------------------------------------------------------------------------------------------
DataSetBook.java
 import java.util.ArrayList;
 import java.util.Arrays;
public class DataSetBook extends ArrayList {
    public DataSetBook(){
       
    }
    public boolean add(Book objToAdd){
        return super.add(objToAdd);
    }
    public int size(){
        return super.size();
    }
    public Book getMin(){
        int size = super.size();
       
        if(size == 0){
            return null;
        }
        else{
            Book minBook = super.get(0);
            for(int i=0; i                Book b = super.get(i);
                if(minBook.getPages() > b.getPages()){
                    minBook = super.get(i);
                }
            }
            return minBook;
        }
    }
    public Book getMax(){
 int size = super.size();
       
        if(size == 0){
            return null;
        }
        else{
            Book maxBook = super.get(0);
            for(int i=0; i                Book b = super.get(i);
                if(maxBook.getPages() < b.getPages()){
                    maxBook = super.get(i);
                }
            }
            return maxBook;
        }
    }
    public java.lang.String toString(){
        return \"Number of Books: \"+super.size()+\"\ \"+\"Minimum Book is \"+getMin().toString()+\"\ \"+\"Maximum Book is \"+getMax().toString()+\"\ \"+\"THe content of entire store \"+Arrays.toString(super.toArray());
    }
 }
Solution
Hi,
Please see below the java classes and sample output. Please comment for queries/feedbacks.
Thanks,
Anita Joseph
Book.java
import java.io.Serializable;
public class Book implements Serializable {
   
 private String author;
 private String title;
 private int pages;
 public Book(String auth, String titl, int pag) {
 author = auth;
 title = titl;
 pages = pag;
 }
 public int getPages() {
 return pages;
 }
 @Override
 public String toString() {
 return \"Book [author=\" + author + \", title=\" + title + \", pages=\" + pages + \"]\ \";
 }
 // this is a really poor way to compare Book objects, but it will work for us
 /* (non-Javadoc)
 * @see java.lang.Object#equals(java.lang.Object)
 */
 @Override
 public boolean equals(Object obj) {
 if (this == obj)
 return true;
 if (obj == null)
 return false;
 if (getClass() != obj.getClass())
 return false;
 Book other = (Book) obj;
 if (author == null) {
 if (other.author != null)
 return false;
 } else if (!author.equals(other.author))
 return false;
 if (pages != other.pages)
 return false;
 if (title == null) {
 if (other.title != null)
 return false;
 } else if (!title.equals(other.title))
 return false;
 return true;
 }
 }
DataSetBook.java
import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Arrays;
 public class DataSetBook extends ArrayList<Book> implements Serializable {
 public DataSetBook(){
   
 }
 public boolean add(Book objToAdd){
 return super.add(objToAdd);
 }
 public int size(){
 return super.size();
 }
 public Book getMin(){
 int size = super.size();
   
 if(size == 0){
 return null;
 }
 else{
 Book minBook = super.get(0);
 for(int i=0; i<super.size() ;i++){
        Book b = super.get(i);
 if(minBook.getPages() > b.getPages()){
 minBook = super.get(i);
 }
 }
 return minBook;
 }
 }
 public Book getMax(){
 int size = super.size();
   
 if(size == 0){
 return null;
 }
 else{
 Book maxBook = super.get(0);
 for(int i=0; i <super.size() ;i++){
    Book b = super.get(i);
 if(maxBook.getPages() < b.getPages()){
 maxBook =super.get(i);
 }
 }
 return maxBook;
 }
 }
 public java.lang.String toString(){
 return \"Number of Books: \"+super.size()+\"\ \"+\"Minimum Book is \"+getMin().toString()+\"\ \"+\"Maximum Book is \"+getMax().toString()+\"\ \"+\"THe content of entire store \"+Arrays.toString(super.toArray());
 }
 }
Cereal.java
import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.ObjectOutputStream;
 import java.util.Random;
 public class Cereal {
   public static void main(String [] args){
        //Reading the noOf books and fileName
        int noOfBokks = Integer.valueOf(args[0]);
        String fileName = args [1];
       DataSetBook dataSetBook = new DataSetBook();
        int counter= noOfBokks;
        Random rand = new Random();
        while(counter!=0){
            //creating DataSetBook object
            String bookTitle= \"book\"+counter;
            String author =\"auth\"+counter;
            int pages = rand.nextInt((200-100)+1)+99;
            Book book = new Book(author,bookTitle,pages);
dataSetBook.add(book);
counter= counter-1;
        }
        try {
            //Serializing DataSetBook object
            FileOutputStream fileOut;
fileOut = new FileOutputStream(fileName);
           ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(dataSetBook);
            out.close();
            fileOut.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
 }
Reconstitute.java
import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 public class Reconstitute {
   public static void main(String [] args){
        //Reading the fileName
        String fileName = args[0];
        DataSetBook dataSetBook =null;
        try{
            //deserializing DataSetBook object
            FileInputStream fileIn= new FileInputStream(fileName);
            ObjectInputStream in =new ObjectInputStream(fileIn);
           
            dataSetBook = (DataSetBook)in.readObject();
            in.close();
            fileIn.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
        catch(ClassNotFoundException c){
            c.printStackTrace();
            return;
        }
       
        System.out.println(dataSetBook.toString());
       
       
    }
 }
Enigma.java
import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 public class Enigma {
   public static void main(String [] args){
        //Reading the fileName
        String fileName = args[0];
        File file =new File(fileName);
        FileInputStream fileInputStream=null;
        byte b= 5;
        byte[] bFile = new byte[(int)file.length() ];
        try{
            fileInputStream =new FileInputStream(file);
            fileInputStream.read(bFile);
            fileInputStream.close();
            System.out.println(\"Reading the file:\");
            //Reading the file
            for(int i=0;i<bFile.length; i++){
                System.out.println((char) bFile[i]);
                //Adding 5 to every byte
                bFile[i] = (byte) (bFile[i]+ b);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
       
        try{
           
            //Writing into the file
            FileOutputStream fileOutputStream =new FileOutputStream(fileName);
            fileOutputStream.write(bFile);
            fileOutputStream.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
}
Bombe.java
import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 public class Bombe {
   public static void main(String [] args){
        //Reading the fileName
        String fileName = args[0];
        File file =new File(fileName);
        FileInputStream fileInputStream=null;
        byte b= 5;
        byte[] bFile = new byte[(int)file.length() ];
        try{
            fileInputStream =new FileInputStream(file);
            fileInputStream.read(bFile);
            fileInputStream.close();
            System.out.println(\"Reading the file:\");
            //Reading the file
            for(int i=0;i<bFile.length; i++){
                System.out.println((char) bFile[i]);
                //deducting 5 from every byte
                bFile[i] = (byte) (bFile[i]- b);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
       
        try{
            //Writing into the same file
            FileOutputStream fileOutputStream =new FileOutputStream(fileName);
            fileOutputStream.write(bFile);
            fileOutputStream.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
}
Sample output:
   Running Enigma
 G:\\Chegg_Serialize_Proj\\src>java Cereal 3 bookFile.dat
   
    Running Enigma
 G:\\Chegg_Serialize_Proj\\src>java Reconstitute bookFile.dat
Number of Books: 3
 Minimum Book is Book [author=auth1, title=book1, pages=105]
Maximum Book is Book [author=auth3, title=book3, pages=199]
THe content of entire store [Book [author=auth3, title=book3, pages=199]
 , Book [author=auth2, title=book2, pages=177]
 , Book [author=auth1, title=book1, pages=105]
 ]
   Before running Enigma , sophie.txt is
    123456789
    Running Enigma.
 G:\\Chegg_Serialize_Proj\\src>java Enigma sophie.txt
 1
 2
 3
 4
 5
 6
 7
 8
 9
   After running Enigma and Before Running Bombe , sophie.txt is
    6789:;<=>
    Running Enigma.
G:\\Chegg_Serialize_Proj\\src>java Bombe sophie.txt
 Reading the file:
 6
 7
 8
 9
 :
 ;
 <
 =
 >
    After Running Bombe , sophie.txt is
    123456789
   








