Given file : ColoredSquare.java package labSerialization; import java.awt.Color;
ID: 3559987 • Letter: G
Question
Given file:
ColoredSquare.java
package labSerialization;
import java.awt.Color;
public class ColoredSquare {
private final int side;
private final Color color;
public ColoredSquare(int s, Color c) {
side = s;
color = c;
}
public int area() {
return side * side;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + side;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof ColoredSquare))
return false;
ColoredSquare other = (ColoredSquare) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (side != other.side)
return false;
return true;
}
@Override
public String toString() {
return String.format("side:%d #%02X%02X%02X",
side, color.getRed(), color.getGreen(),
color.getBlue());
}}
LabSerialization.java
package labSerialization;
import java.awt.Color;
public class LabSerialization {
public static void main(String[] args) {
ListVsSetDemo demo = new ListVsSetDemo(
new ColoredSquare(4, Color.RED),
new ColoredSquare(6, Color.BLUE),
new ColoredSquare(4, Color.RED),
new ColoredSquare(8, Color.YELLOW));
testDemo(demo);
};
private static void testDemo(ListVsSetDemo demo) {
System.out.println("List:");
System.out.println(demo.getListElements());
System.out.println("Set:");
System.out.println(demo.getSetElements());
}}
ListvsSetDemo.java
package labSerialization;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ListVsSetDemo {
private final List<ColoredSquare> list;
private final Set<ColoredSquare> set;
public ListVsSetDemo(ColoredSquare... elements) {
list = new ArrayList<>(Arrays.asList(elements));
set = new HashSet<>(Arrays.asList(elements));
}
public String getListElements() {
StringBuilder sb = new StringBuilder();
for (ColoredSquare el : list) {
sb.append(el).append(" ");
}
return sb.toString();
}
public String getSetElements() {
StringBuilder sb = new StringBuilder();
for (ColoredSquare el : set) {
sb.append(el).append(" ");
}
return sb.toString();
}
public void addElement(ColoredSquare el) {
list.add(el);
set.add(el);
}
}
600 points and thank you very much
In this lab we create an instance of ListVsSetDemo. Then we are going to serialize and deserialize the instance. Download labSerialization.zip Unzip the file and import it into Eclipse. Check the checkbox Create top-level folder At this point you have a package called labSerialization with one java file inside. Run the program. You should get an output similar to the one described on the right Implement interface Serializable from package java.io Notice the warning from Eclipse: have Eclipse auto-generate a serialVersionUID Right-click the yellow warning icon > Quick Fix > Add Generated Serial Version ID A private static final field of type long is added Implement interface Serializable Have Eclipse auto-generate a serialVersionUID Add a method called serialize. It has no return value and two parameters: demo of type ListVsSetDemo and filename of type String Use a try with resource statement when you serialize the ListVsSetDemo instance Save the result in the file name specified In case of an exception print the message provided by the exception object In the main method comment out testDemo(demo); Then call the method serialize. For the file name choose a relative path that create the file Demo, ser in the directory labSerialization Print a brief message to let the user know that serialization is complete. Run the program and verify that the instance got serialized and that the file got created. Add a method called deserialize. It returns a ListVsSetDemo and has one parameter: a filename of type String Use a try with resource statement to implement the deserialize method Use a single catch block to catch both lOExceptions and FileNotFoundExceptions In main call the method deserialize. Assign the ListVsSetDemo returned to a new variable called newDemo and pass it to the method testDemoExplanation / Answer
//Do let me know if you have any doubts
package labSerialization;
import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class LabSerialization {
public static void main(String[] args) {
ListVsSetDemo demo = new ListVsSetDemo(
new ColoredSquare(4, Color.RED),
new ColoredSquare(6, Color.BLUE),
new ColoredSquare(4, Color.RED),
new ColoredSquare(8, Color.YELLOW));
String fileName = "Demo.ser";
serialize(demo, fileName);
ListVsSetDemo newDemo = deserialize(fileName);
testDemo(newDemo);
};
private static void testDemo(ListVsSetDemo demo) {
System.out.println("List:");
System.out.println(demo.getListElements());
System.out.println("Set:");
System.out.println(demo.getSetElements());
}
static void serialize(ListVsSetDemo demo, String fileName){
try(ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(fileName))){
objectOutputStream.writeObject(demo);
System.out.println("Created the File"+fileName);
}
catch (IOException e) {
System.err.println(e.getMessage());
}
}
static ListVsSetDemo deserialize(String fileName){
try(ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(fileName))){
ListVsSetDemo newDemo = (ListVsSetDemo) objectInputStream.readObject();
return newDemo;
}
catch (FileNotFoundException e) {
System.err.println(e.getMessage());
return null;
}
catch (IOException e) {
System.err.println(e.getMessage());
return null;
} catch (ClassNotFoundException e) {
System.err.println(e.getMessage());
return null;
}
}
}
package labSerialization;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ListVsSetDemo implements Serializable{
private static final long serialVersionUID = 1322038405592713464L;
private final List<ColoredSquare> list;
private final Set<ColoredSquare> set;
public ListVsSetDemo(ColoredSquare... elements) {
list = new ArrayList<>(Arrays.asList(elements));
set = new HashSet<>(Arrays.asList(elements));
}
public String getListElements() {
StringBuilder sb = new StringBuilder();
for (ColoredSquare el : list) {
sb.append(el).append(" ");
}
return sb.toString();
}
public String getSetElements() {
StringBuilder sb = new StringBuilder();
for (ColoredSquare el : set) {
sb.append(el).append(" ");
}
return sb.toString();
}
public void addElement(ColoredSquare el) {
list.add(el);
set.add(el);
}
}
package labSerialization;
import java.awt.Color;
import java.io.Serializable;
public class ColoredSquare implements Serializable{
private static final long serialVersionUID = -8141251154368602955L;
private final int side;
private final Color color;
public ColoredSquare(int s, Color c) {
side = s;
color = c;
}
public int area() {
return side * side;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + side;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof ColoredSquare))
return false;
ColoredSquare other = (ColoredSquare) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (side != other.side)
return false;
return true;
}
@Override
public String toString() {
return String.format("side:%d #%02X%02X%02X",
side, color.getRed(), color.getGreen(),
color.getBlue());
}}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.