Question: 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
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()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
