lab21
subdirectory of your personal repo, the other submits a file report.txt
in the lab21
subdirectory of your personal repo.Person
class:
import java.util.*; public class Person { public Person(String name) { this.name = name; } public void befriend(Person p) { friends.add(p); } public Person getFriend(int i) { return 0 <= i && i < friends.size() ? friends.get(i) : null; } public String toString() { String result = name + ", friends ="; for (Person f : friends) result += " " + f.name; return result; } private String name; private ArrayList<Person> friends = new ArrayList<>(); }
public class Prog1 { public static void main(String[] args) throws Exception { Person fred = new Person("Fred"); Person barney = new Person("Barney"); Person cloneOfFred = fred.clone(); fred.befriend(barney); barney.befriend(fred); System.out.println(cloneOfFred); } }What happens? Why?
clone
public. Add this to the Person
class:
public Person clone() { return (Person) super.clone(); }What happens when you compile? Why?
clone
declare that it might throw the exception rather than to catch the exception. Do that. Your program should now compile.Person
implement the appropriate interface. Now your program should compile and run. What friends does cloneOfFred
have? Why?cloneOfFred
has its own list of friends, by cloning the original list. How did you do that? Run the program again and verify that the program has been fixed.clone
method looks like this:
public Person clone() throws CloneNotSupportedException { Person cloned = (Person) super.clone(); cloned.friends = new ArrayList<>(); for (Person f : friends) cloned.friends.add(f.clone()); return cloned; }Run the program. What happens? Why?
Person
class to be Serializable
. Run this program:
import java.io.*; public class Prog2 { public static void main(String[] args) throws Exception { Person fred = new Person("Fred"); Person barney = new Person("Barney"); fred.befriend(barney); barney.befriend(fred); ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("people.ser")); out.writeObject(fred); out.close(); fred = null; barney = null; ObjectInputStream in = new ObjectInputStream( new FileInputStream("people.ser")); Person p = (Person) in.readObject(); System.out.println(p); System.out.println(p.getFriend(0)); } }Which objects are contained in
people.ser
? Why?Person
class:
private java.awt.image.BufferedImage image; public Person(String name, String imageURL) throws java.io.IOException { this(name); image = javax.imageio.ImageIO.read(new java.net.URL(imageURL)); } public void show() { if (image == null) javax.swing.JOptionPane.showMessageDialog(null, name); else javax.swing.JOptionPane.showMessageDialog(null, name, "", 1, new javax.swing.ImageIcon(image)); }In
Prog2
, change the declaration of fred
to
Person fred = new Person("Fred", "https://upload.wikimedia.org/wikipedia/en/thumb/a/ad/Fred_Flintstone.png/165px-Fred_Flintstone.png"); fred.show();Run the program. What happens? Why?
Make the image transient. The program should now run.
Add p.show()
to the end. Why doesn't the image show?
Extra credit: How can you fix it?