File IO in Class Activities with SOLUTIONS
Complex Class
For these activities you will be using this Complex
class.
Activity 1 [solution]
Implement a write(DataOutputStream out)
method in the Complex
class that will write the fields of the Complex
object to a binary output
stream.
Activity 2 [solution]
Implement a read(DataInputStream in)
method in the Complex
class that will read a complex number from a binary input stream and return
a Complex
object with the value read from the stream.
Activity 3 [solution]
Write a driver program that creates four complex numbers: 1.0 + 0.0i, -2.3 - 5.0i, 0.0 + 3.0i, and 8.0 + 8.0i and writes them to a binary file.
Activity 4 [solution]
Write another driver program that reads two complex numbers from a binary file and displays them to the console in both cartesian and polar form.
Activity 5 [solution]
Implement a write(PrintWriter out)
method in the Complex
class that will write the fields of the Complex
object to a text output
stream.
Activity 6 [solution]
Implement a read(Scanner in)
method in the Complex
class that will read a complex number from a text input stream and return
a Complex
object with the value read from the stream.
Activity 7 [solution]
Write another driver program that creates four complex numbers: 1.0 + 0.0i, -2.3 - 5.0i, 0.0 + 3.0i, and 8.0 + 8.0i and writes them to a text file. All numbers should be on one line and separated by commas.
Activity 8 [solution]
Write another driver program that reads all complex numbers in a text file (assume no other data is in the file and that the format described in the previous activity is used) and displays them to the console in cartesian form.
Activity 9 [solution]
Modify the Complex
class so that it can be written to an ObjectOutputStream
,
and write a driver program that creates four complex numbers: 1.0 + 0.0i,
-2.3 - 5.0i, 0.0 + 3.0i, and 8.0 + 8.0i and writes them to a binary file
as objects.
Activity 10 [solution]
Write another driver program that reads four complex numbers that have been written to a binary file as objects, and displays them to the console in polar form.