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)

 Given file: ColoredSquare.java package labSerialization; import java.awt.Color; public class ColoredSquare {

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 list; private final Set 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); } }

How do you acomplish this assignment?

Lab Serialization: Description: In this lab we create an instance of ListVsSetDemo. Then we are going to serialize and deserialize the instance Getting Started: 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 Output List: side : 4 #FF0000 side : 6 #0000FF side : 4 #FF0000 side :8 #FFFF00 Description: In ColoredSquare: . Implement interface Serializable from package java.io Set: side :6 #0000FF side :8 #FFFF00 side :4 #FF0000 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 In ListVsSetDemo: Implement interface Serializable Have Eclipse auto-generate a serialVersionUID In LabSerialization: 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 IOExceptions and FileNotFoundExceptions In main call the method deserialize Assign the ListVsSetDemo returned to a new variable called newDemo and pass it to the method testDemo

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!