Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to store a bunch of triangles and squares in an arraylist, use o

ID: 3742834 • Letter: W

Question

Write a program to store a bunch of triangles and squares in an arraylist, use object serialization to write the arraylist to a file

And the code below has some errors; need help with it.

public static void main(String[] args) throws IOException, ClassNotFoundException {
ArrayList<Serializable> list = new ArrayList<>();
list.add(new Rectangle(2, 3));
list.add(new Square(5));
list.add(new Rectangle(1, 4));
list.add(new Square(1));
list.add(new Rectangle(7, 1));
list.add(new Rectangle(5, 6));
list.add(new Square(9));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("rectangleandsquares.txt"));
oos.writeObject(list);

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("rectangleandsquares.txt"));
System.out.println(ois.readObject());

ois.close();
oos.close();
}

}

errors:


----jGRASP exec: javac -g SerilizableMain.java
SerilizableMain.java:1: error: class, interface, or enum expected
public static void main(String[] args) throws IOException, ClassNotFoundException {
^
SerilizableMain.java:3: error: class, interface, or enum expected
list.add(new Rectangle(2, 3));
^
SerilizableMain.java:4: error: class, interface, or enum expected
list.add(new Square(5));
^
SerilizableMain.java:5: error: class, interface, or enum expected
list.add(new Rectangle(1, 4));
^
SerilizableMain.java:6: error: class, interface, or enum expected
list.add(new Square(1));
^
SerilizableMain.java:7: error: class, interface, or enum expected
list.add(new Rectangle(7, 1));
^
SerilizableMain.java:8: error: class, interface, or enum expected
list.add(new Rectangle(5, 6));
^
SerilizableMain.java:9: error: class, interface, or enum expected
list.add(new Square(9));
^
SerilizableMain.java:10: error: class, interface, or enum expected
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("rectangleandsquares.txt"));
^
SerilizableMain.java:11: error: class, interface, or enum expected
oos.writeObject(list);
^
SerilizableMain.java:13: error: class, interface, or enum expected
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("rectangleandsquares.txt"));
^
SerilizableMain.java:14: error: class, interface, or enum expected
System.out.println(ois.readObject());
^
SerilizableMain.java:16: error: class, interface, or enum expected
ois.close();
^
SerilizableMain.java:17: error: class, interface, or enum expected
oos.close();
^
SerilizableMain.java:18: error: class, interface, or enum expected
}
^
15 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Explanation / Answer

/***********************************

Wrap the main function in a class Wrapper

Rename the file as Serializable.java

import the classes used

Since Rectangle and Square hasn't been supplied here

I'm not implementing them

so use it like you were using them

Comment here for doubts

************************************/

import java.util.ArrayList;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.ObjectInputStream;

import java.lang.ClassNotFoundException;

import java.io.Serializable;

import java.io.FileInputStream;

import java.io.FileOutputStream;

class SerializableMain {

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

ArrayList<Serializable> list = new ArrayList<>();

list.add(new Rectangle(2, 3));

list.add(new Square(5));

list.add(new Rectangle(1, 4));

list.add(new Square(1));

list.add(new Rectangle(7, 1));

list.add(new Rectangle(5, 6));

list.add(new Square(9));

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("rectangleandsquares.txt"));

oos.writeObject(list);

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("rectangleandsquares.txt"));

System.out.println(ois.readObject());

ois.close();

oos.close();

}

}

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