Answered You can hire a professional tutor to get the answer.
Color Point Class and Binary File 110 Continue working with the same project . In the file next to mail ; create two new* methods . public Static...
ColorPoint Class and InvalidRgbException a class named Colorpoint with the following fields, I attached the requirement, here is what I have, just want make sure is there any part that I miss, like the last requirement (In main() n 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.)
Program Code :-
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
public class ColorPointDemo {
public static void main(String[] args) {
try {
ArrayList<Colorpoint> cps = new ArrayList<>();
Colorpoint obj1 = new Colorpoint(100,100,20,20,20);
cps.add(obj1);
Colorpoint obj2 = new Colorpoint(0,0,10,10,10);
cps.add(obj2);
obj1.equals(obj2);
System.out.println("obj1:" + obj1.toString());
// call the wrtiters.
Colorpoint[] countries = cps.toArray(new Colorpoint[cps.size()]);
writeColorPointArray("colorPointsFile.dat", countries);
System.out.println("Finished saving objects into a binary file!");
Colorpoint[] readFromFile = readColorPointArray("colorPointsFile.dat");
System.out.println("Finished reading Objects from binary file...");
for (Colorpoint c : readFromFile) {
System.out.printf(">> %sn", c.toString());
}
} catch (Exception e) {
// e.printStackTrace();
System.err.println("ERROR: " + e.getMessage());
}
}
public static Colorpoint[] readColorPointArray(String fileName)
throws IOException {
ArrayList<Colorpoint> colorPoints = new ArrayList<>();
try {
File file = new File(fileName);
// open the binary file
if (file.exists()) {
ObjectInputStream inputStream
= new ObjectInputStream(new FileInputStream(file));
Colorpoint cp = null;
try {
while (true) {
cp = (Colorpoint) inputStream.readObject();
colorPoints.add(cp);
System.out.printf("reading ColorPoints:%sn",cp.toString());
}
} 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.");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//return (Colorpoint[]) colorPoints.toArray();
return colorPoints.toArray(new Colorpoint[colorPoints.size()]);
}
public static void writeColorPointArray(String fileName, Colorpoint[] points)
throws IOException {
ObjectOutputStream outputStream = null;
try {
File file = new File(fileName);
// open the binary file
if (file.exists()) {
file.delete();
}
if (file.createNewFile()) {
System.out.println("created a new file!");
} else {
System.out.println("failed to create a new file!");
}
outputStream
= new ObjectOutputStream(new FileOutputStream(file));
for (Colorpoint cp : points) {
if (Objects.nonNull(cp)) {
outputStream.writeObject(cp);
System.out.printf("object is non null and added! cp: %sn", cp);
}
}
} catch (FileNotFoundException e) {
System.out.println("Cannot find file.");
} catch (IOException e) {
System.out.println("Problem with input from file.");
}
if (outputStream != null) {
outputStream.close();
}
}
}
<<<<<------------------------------------------------------------------------------------------------------------------------->>>>>
import java.io.Serializable;
public class Colorpoint implements Serializable {
private int x; //x coordinate on a plane
private int y; //y coordinate on a plane
private int colorR; //red color component
private int colorB; //blue color component
private int colorG; //green color component
public Colorpoint() {
x = 0;
y = 0;
colorR = 0;
colorB = 0;
colorG = 0;
}
public Colorpoint(int cordX, int cordY, int colR, int colG, int colB) {
x = cordX;
y = cordY;
colorR = colR;
colorB = colB;
colorG = colG;
}
public void setXY(int x, int y) {
this.x = x;
this.y = y;
}
/**
* @return the x
*/
public int getX() {
return x;
}
/**
* @return the y
*/
public int getY() {
return y;
}
public void setRGB(int colorR, int colorG, int colorB) throws InvalidRgbException {
// specify the cause of the error in message by throwing Exception
if (colorR < 0 || colorR > 255) {
throw new InvalidRgbException("Invalid entry of R value . 0 To 255" + colorR);
} else if (colorG < 0 || colorG > 255) {
throw new InvalidRgbException("Ivalid entry of G number . 0 To 255" + colorG);
} else if (colorB < 0 || colorB > 255) {
throw new InvalidRgbException("Ivalid entry of B number . 0 To 255" + colorB);
} else {
this.colorR = colorR;
this.colorG = colorG;
this.colorB = colorB;
}
}
/**
* @return the colorR
*/
public int getColorR() {
return colorR;
}
/**
* @return the colorB
*/
public int getColorB() {
return colorB;
}
/**
* @return the colorG
*/
public int getColorG() {
return colorG;
}
@Override
public String toString() {
return "The cordination of X is: " + x + " and the cordination of Y is: " + y;
}
@Override
public boolean equals(Object o) {
if (o instanceof Colorpoint) {
Colorpoint cp = (Colorpoint) o;
if (this.x == cp.x && this.y == cp.y
&& this.colorB == cp.colorB
&& this.colorG == cp.colorG
&& this.colorR == cp.colorR) {
return true;
}
}
return false;
//return super.equals(o);
}
}
<<<<----------------------------------------------------------------------------------------------->>>>
public class InvalidRgbException extends Exception{
//Parameterized construction
public InvalidRgbException(String message){
super(message);
}
}
:
- Attachment 1
- Attachment 2
- Attachment 3