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: 3786784 • Letter: S

Question

Serialization and Binary I/O

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

Write the following programs based that are based on Programming Exercises 17.6, 17.7, 17.14, and 17.15:

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

17.6 (Store Loan objects The Loan class in Listing 10.2 does not implement Serializable. Rewrite the Loan class to implement Serializable. Write a program that creates five Loan objects and stores them in a file named Exercise 17 06.dat. 17.7 (Restore objects from a file) Suppose a file named Exercise17 07.dat has been created using the 0bject0utputstream. The file contains Loan objects. The Loan class in Listing 10.2 does not implement Serializable. Rewrite the Loan class to implement Serializable. Write a program that reads the Loan objects from the file and displays the total loan amount. Suppose you don't know how many Loan objects are there in the file, use EOFException to end the loop.

Explanation / Answer

// 17.6

// Loan Object

import java.io.Serializable;

// Class Loan is implementing Serializible
public class Loan implements Serializable{

   //Attributes of Loan object
   String accNo;
   String holderName;
   String loanId;
   double loanAmount;
   double rateOfInterest;
  
   //Setters and Getters of Loan
  
   public String getAccNo() {
       return accNo;
   }
   public void setAccNo(String accNo) {
       this.accNo = accNo;
   }
   public String getLoanId() {
       return loanId;
   }
   public void setLoanId(String loanId) {
       this.loanId = loanId;
   }
   public double getLoanAmount() {
       return loanAmount;
   }
   public void setLoanAmount(double loanAmount) {
       this.loanAmount = loanAmount;
   }
   public double getRateOfInterest() {
       return rateOfInterest;
   }
   public void setRateOfInterest(double rateOfInterest) {
       this.rateOfInterest = rateOfInterest;
   }
   public String getHolderName() {
       return holderName;
   }
   public void setHolderName(String holderName) {
       this.holderName = holderName;
}
}

//StoreLoanObjects.java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class StoreLoanObjects {

   public static void main(String[] args) throws IOException {

       //Creating a FileInputStream Object to create a file
       FileOutputStream fos = new FileOutputStream("D:\Exercise17_06.dat");
       //ObjectOutputStream is instantiated to write the object in file
       ObjectOutputStream oos = new ObjectOutputStream(fos);
      
       //Creating Loan Objects
       Loan loan1 = new Loan(); loan1.setHolderName("Jack");
       loan1.setAccNo("3501458900");loan1.setLoanAmount(500000);
       loan1.setLoanId("LOAN1");loan1.setRateOfInterest(13.5);
      
       Loan loan2 = new Loan();loan2.setHolderName("James");
       loan2.setAccNo("3501458901");loan2.setLoanAmount(600000);
       loan2.setLoanId("LOAN2");loan2.setRateOfInterest(13.5);
      
       Loan loan3 = new Loan();loan3.setHolderName("Jane");
       loan3.setAccNo("3501458902");loan3.setLoanAmount(400000);
       loan3.setLoanId("LOAN3");loan3.setRateOfInterest(13.5);
      
       Loan loan4 = new Loan();loan4.setHolderName("Jacob");
       loan4.setAccNo("3501458903");loan4.setLoanAmount(650000);
       loan4.setLoanId("LOAN4");loan4.setRateOfInterest(13.5);
      
       Loan loan5 = new Loan();loan5.setHolderName("Javed");
       loan5.setAccNo("3501458904");loan5.setLoanAmount(550000);
       loan5.setLoanId("LOAN5");loan5.setRateOfInterest(13.5);
      
       //Creating a Collection to add Loan objects to ArrayList
      
       ArrayList<Loan> loanList = new ArrayList<Loan>();
       loanList.add(loan1);loanList.add(loan2);
       loanList.add(loan3);loanList.add(loan4);
       loanList.add(loan5);
      
       //Writing Loann Collection into List
       oos.writeObject(loanList);
      
       // Successfully Serialized
       System.out.println("Loan Data is stored..");
      
       oos.close(); // Closing Streams
       fos.close();
      
   }

}

Output:

Loan Data is stored

//17.7

// Loan.java

import java.io.Serializable;

// Class Loan is implementing Serializible
public class Loan implements Serializable{

   //Attributes of Loan object
   String accNo;
   String holderName;
   String loanId;
   double loanAmount;
   double rateOfInterest;
  
   //Setters and Getters of Loan
  
   public String getAccNo() {
       return accNo;
   }
   public void setAccNo(String accNo) {
       this.accNo = accNo;
   }
   public String getLoanId() {
       return loanId;
   }
   public void setLoanId(String loanId) {
       this.loanId = loanId;
   }
   public double getLoanAmount() {
       return loanAmount;
   }
   public void setLoanAmount(double loanAmount) {
       this.loanAmount = loanAmount;
   }
   public double getRateOfInterest() {
       return rateOfInterest;
   }
   public void setRateOfInterest(double rateOfInterest) {
       this.rateOfInterest = rateOfInterest;
   }
   public String getHolderName() {
       return holderName;
   }
   public void setHolderName(String holderName) {
       this.holderName = holderName;
   }
  
}

//TotalLoanAmount.java

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Iterator;

public class TotalLoanAmount {

   // main() throwing IOException for input file reading
   // throwing ClassNotFoundException for Reading object from file
   //EOFException for checking end of file to read
   public static void main(String[] args) throws IOException, ClassNotFoundException,EOFException{
  
       //InputStream for creating file instance
       FileInputStream fis = new FileInputStream("D:\Exercise17_07.dat");
       //Creating Object input stream for reading objects
       ObjectInputStream ois = new ObjectInputStream(fis);
      
       //readObject to read the object and cast with Object
       ArrayList<Loan> loanData = (ArrayList<Loan>) ois.readObject();
      
       //Iterator to Iterate ArrayList object
       Iterator<Loan> loans = loanData.iterator();
      
       double sum=0.0;
       while(loans.hasNext()){
           Loan loan = loans.next(); // Getting each Loan Object
           sum = sum+loan.getLoanAmount(); // Summation of Loan amount
       }
      
       System.out.println("Total Loan Amount is:"+sum);
      
       ois.close(); // Closing Streams
       fis.close();

   }

}

Output:

Total Loan Amount is:2700000.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