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

Serialization and Binary I/O Can you please help me the JAVA program? Here is th

ID: 3786803 • Letter: S

Question

Serialization and Binary I/O

Can you please help me the JAVA program? Here is the requirement:

1. Modify DataSetBook and Book to be serializable

2. 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.

3. 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 filesophie.dat.

4. 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.

5. 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());
   }
}

Explanation / Answer

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.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 = (Book) super.get(0);
           for (int i = 0; i < size; i++) {
               Book b = (Book) super.get(i);
               if (minBook.getPages() > b.getPages()) {
                   minBook = (Book) super.get(i);
               }
           }
           return minBook;
       }
   }

   public Book getMax() {
       int size = super.size();

       if (size == 0) {
           return null;
       } else {
           Book maxBook = (Book) super.get(0);
           for (int i = 0; i < size; i++) {
               Book b = (Book) super.get(i);
               if (maxBook.getPages() < b.getPages()) {
                   maxBook = (Book) 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) {
       int noOfBooks;
       String fileName = "";
      
       if(args.length != 2) {
           throw new IllegalArgumentException("Exactly 2 arguments should be presnt.");
       } else {
          
           try {
               noOfBooks = Integer.parseInt(args[0]);
           } catch (NumberFormatException e) {
               throw new IllegalArgumentException("1st argument is not a number");
           }
           if(noOfBooks <= 0) {
               throw new IllegalArgumentException("1st argument should be greater than 0");
           }

           fileName = args[1];
       }
      
       Random random = new Random();
       DataSetBook dataSetBook = new DataSetBook();
      
       for(int i=0; i< noOfBooks; i++) {
           int pageNo = random.nextInt(200 - 100 + 1) + 100;
           Book book = new Book("auth" + i, "book"+i, pageNo);
           dataSetBook.add(book);
       }
      

       FileOutputStream fileOutputStream;
       ObjectOutputStream objectOutputStream;
       try {
           fileOutputStream = new FileOutputStream(fileName, true);
       objectOutputStream = new ObjectOutputStream(fileOutputStream);
       objectOutputStream.writeObject(dataSetBook);
       objectOutputStream.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
  
   }

}

Enigma.java

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


public class Enigma {

   public static void main(String[] args) {
       String fileName = "";

       if(args.length != 1) {
           throw new IllegalArgumentException("Exactly 1 argument should be presnt.");
       } else {
           fileName = args[0];
       }
      
   try {
           Path path = Paths.get(fileName);
          
           byte[] byteData = Files.readAllBytes(path);
          
           for(int i=0; i<byteData.length; i++) {
               byteData[i] += 5;
           }
          
           Files.write(path, byteData);
          
       } catch (IOException e) {
           e.printStackTrace();
       }

   }

}

Bombe.java

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Bombe {

   public static void main(String[] args) {
       String fileName = "";

       if (args.length != 1) {
           throw new IllegalArgumentException(
                   "Exactly 1 argument should be presnt.");
       } else {
           fileName = args[0];
       }

       try {
           Path path = Paths.get(fileName);

           byte[] byteData = Files.readAllBytes(path);

           for (int i = 0; i < byteData.length; i++) {
               byteData[i] -= 5;
           }

           Files.write(path, byteData);

       } catch (IOException e) {
           e.printStackTrace();
       }

   }
}

Reconstitute.java:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Reconstitute {

   public static void main(String[] args) {
       String fileName = "";

       if (args.length != 1) {
           throw new IllegalArgumentException(
                   "Exactly 1 argument should be presnt.");
       } else {
           fileName = args[0];
       }

       DataSetBook dataSetBook = null;

       FileInputStream fileInputStream;
       ObjectInputStream objectInputStream;
       try {
           fileInputStream = new FileInputStream(fileName);
           objectInputStream = new ObjectInputStream(fileInputStream);
           dataSetBook = (DataSetBook) objectInputStream.readObject();
           objectInputStream.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }

       if (dataSetBook != null) {
           System.out.println(dataSetBook);
       }
   }

}

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