Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io.ObjectInputStream; import java.io.FileInputStream; import java.io.EOFException; import java.io.IOException; import java.io.FileNotFoundException; public class EOFDemo { public static void main(String[] args) { try { ObjectInputStream inputStream =

import java.io.ObjectInputStream; import java.io.FileInputStream; import java.io.EOFException; import java.io.IOException; import java.io.FileNotFoundException;

public class EOFDemo { public static void main(String[] args) { try { ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("numbers.dat")); int number; System.out.println("Reading numbers in numbers.dat"); try { while (true) { number = inputStream.readInt( ); System.out.println(number); } } catch(EOFException e) { System.out.println("No more numbers in the file."); } inputStream.close( ); } catch(FileNotFoundException e) { System.out.println("Cannot find file numbers.dat."); } catch(IOException e) { System.out.println("Problem with input from file numbers.dat."); } } } ************************************* *ColorPoint Class and InvalidRgbException Design a class named ColorPoint with the following fields: x x coordinate on a plane y y coordinate on a plane colorR red color component colorG green color component colorB blue color component All member variables are of type int. Write a non-argument constructor that sets all variables to 0. Write a constructor that takes all the data about the point position and color as parameters (5 parameters) and initializes fields. Write appropriate accessor and mutator methods for these fields: o setXY(int x, iny); o setRGB(int r, int g, int b) o getR(); getG; getB(); getX(); getY(); Override toString() equals() methods for the class Create a custom exception class called InvalidRgbException that will be used when the color field of your new class is incorrectly set. As you know, each color component value must not be more than 255 and less than 0. Add data validation code to constructor that takes parameters and to setRGB() method. In each of the methods throw InvalidRgbException if the value for red, green, or blue is out of correct range. In the exception message specify the color field that was incorrectly set and the value it was set to.

*Name your project ColorPointDemo.java Test all methods you wrote in main. Place main() in a file separate from all the classes named ColorPointDemo.java. Make sure to use try/catch block to catch custom exceptions that are thrown by your methods. Write tests that demonstrate that the exceptions are being thrown and caught. /////////////////////////////////////////////////////////////////////////////////////// *ColorPoint Class and Binary File I/O Continue working with the same project. In the file next to main() create two new methods: public static ColorPoint[] readColorPointArray(String fileName) and public static void writeColorPointArray(String fileName, ColorPoint[] points)

Both methods must throw I/OExceptions. The I/OExceptions may originate in the methods but must not be handled. All exceptions must be propagated to the calling code. Please handle ClassNotFoundException in the readColorPointArray() method.

readColorPointArray()method reads the contents of a binary file and places it into an array of objects of type ColorPoint. Assume you do not know the amount of data in the file. When reading the file store the objects in ArrayList first. After the end of file has been reached, convert the ArrayList into a regular array and return it from method. Please study EOFDemo.java code example on the top (can be found next to this assignment) to learn how to use exceptions when checking for the end of file while reading from binary file.

writeColorPointArray()method writes the contents of the given array into the given binary file.

In main() create an array of ColorPoint objects of size 10. Populate the array with objects that have random values for colors use random number generator to set RGB fields, and (x, y) set to (0, 0)(9, 9).

Pass that array to the method writeColorPointArray() to be stored in a file colorPoints.dat. Then use readColorPointArray()method to read the stored data from file and display it into the screen.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions