Question: In my intro to Java class, we were given the assignment to convert RGB to CMYK, the RGB values were read from a file. It

In my intro to Java class, we were given the assignment to convert RGB to CMYK, the RGB values were read from a file. It had to include a boolean method to validate the number was within range (0-255), a method which returned the maximum value in the array, and a method that received the array RGB and placed the converted values into array CMYK. Lastly, we had to create a print stream that sent the output to a file. However, the output I am receiving is incorrect. I want array RGB and CMYK to print to the same file in two separate columns that are next to each other. but all I get is three digits printing out (255, 0, 0). I don't think the program is reading the whole input file or perhaps my printing methods are off. can anyone help me figure out how to fix my program? Thank you.

public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new File("colors.txt")); PrintStream ps = new PrintStream("ArrayResults.txt"); int [] RGB = new int[3]; double [] CMYK = new double [4]; int i=0; while (sc.hasNextInt() && validateRGB(RGB)==true) { RGB[i]=sc.nextInt(); } printCMYK(CMYK); printRGB(RGB); } public static boolean validateRGB(int[] RGB) throws Exception { Scanner sc = new Scanner(new File("colors.txt")); int i = 0; while (sc.hasNextInt()) { if (sc.nextInt() >= 0 && sc.nextInt() <= 255){ return true; } } return false; } public static int maximum(int x[]) { int maximum, i; maximum = x[0]; for (i=1; i<4; i++) { if (x[i] > maximum) { maximum = x[i]; } } return maximum; } public static void RGBtoCMYK(int RGB[], double CMYK[]) { int white; white = maximum(RGB); CMYK[0]=(white-(RGB[0]/255))/white; CMYK[1]=(white-(RGB[0]/255))/white; CMYK[2]=(white-(RGB[2]/255))/white; CMYK[3]=1-white; } public static void printRGB(int RGB[]) throws Exception { PrintStream ps = new PrintStream("ArrayResults.txt"); for (int i=0; i<3; i++) { ps.println(RGB[i]); } } public static void printCMYK(double CMYK[]) throws Exception { PrintStream ps = new PrintStream("ArrayResults.txt"); for (int i=0; i<4; i++) { ps.println(CMYK[i]); } } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!