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

Design a class named Colorpoint with the following fields: x – x coordinate on a

ID: 3881058 • Letter: D

Question

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 (4 parameters) and initializes fields. Write appropriate accessor and mutator methods for these fields: o setXY(int x, iny); setRGB(int r, int g, int b) o getR(); getG; getB(); getX(); getY(); Override toString() and 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 225 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. 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. The text file we are reading is supposed to be filled with integers, one integer per line. If one of the lines contains a corrupted piece of data (not an integer), the MultiCatch program stops, reports an error and exits. Change the flow of control in the sample code in such a way that a corrupted line (or multiple lines) of text file does not prevent the program to continue printing out the rest of the numbers from the file. Call your project FixedMultiCatch.java

Explanation / Answer

/* package codechef; // don't place package name! */

import java.util.*;

import java.lang.*;

import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

class Colorpoint

{

int x, y, colorR, colorG, colorB;

Colorpoint(){

x=0;

y=0;

colorR=0;

colorG=0;

colorB=0;

}

Colorpoint(int x,int y, int colorR, int colorG, int colorB) {

this.x=x;

this.y=y;

try{

throw new InvalidRgbException(colorR, colorB, colorG);

}

catch(InvalidRgbException in){

System.out.println(in.getMessage());

}

this.colorR=colorR;

this.colorG=colorG;

this.colorB=colorB;

}

//getter and setter methods

void setXY(int x, int y)

{

this.x=x;

this.y=y;

}

void setRGB(int colorR, int colorG, int colorB){

try{

throw new InvalidRgbException(colorR, colorB, colorG);

}

catch(InvalidRgbException in){

System.out.println(in.getMessage());

}

this.colorR=colorR;

this.colorG=colorG;

this.colorB=colorB;

}

int getX(){

return this.x;

}

int getY(){

return this.y;

}

int getR(){

return this.colorR;

}

int getG(){

return this.colorG;

}

int getB(){

return this.colorB;

}

}  

class colorPointDemo implements Serializable{

public static void main (String[] args) throws java.lang.Exception

{

// your code goes here

Colorpoint[] array = new Colorpoint[10];

Random rand = new Random();

for(int i=0; i<10; i++){

Colorpoint cp = new Colorpoint(i, i, rand.nextInt(), rand.nextInt(), rand.nextInt()); // using random class to generate random number

array[i]=cp;

}

String filename = "colorPoints.dat";

writeColorPointArray(filename, array);

}

public static void readColorPointArray(String fileName) throws IOException{

ObjectOutputStream myStream = new ObjectOutputStream(new FileOutputStream("colorPoints.dat"));

//ArrayList<colorPoint> al = new ArrayList<>();

//boolean eof = false;

Colorpoint[] array = (Colorpoint[]) myStream.readObject();

try{

for(int i=0; i<array.length; i++)

{

System.out.println(array[i].getX()); //similarly all fields

}

}

catch(FileNotFoundException e){

System.out.println("error in readine file");

}

}

public static void writeColorPointArray(String fileName, Colorpoint[] points) throws IOException{

try

{

ObjectOutputStream myStream = new ObjectOutputStream(new FileOutputStream(fileName));

myStream.writeObject(points);

myStream.close();

}

catch (FileNotFoundException e)

{

  

}

}

}

class InvalidRgbException extends Exception{ // making my own exception class by inheriting the properties of exception class

  

InvalidRgbException(int colorR, int colorB, int colorG){

if(colorR<0 || colorR>225 || colorG<0 || colorG>225 || colorB<0 || colorG>225)

super("invalid color");

}

}

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