Part 3 asks you to understand a written description of a simple problem and translate the description into a Java program. The problem solution requires some arithmetic involving int and double types.
You should be able to complete Part 2 in 25 minutes or less.
The RGB colour space is a common representation of colour where red, green, and blue light are added together to produce a spectrum of colours. It is commonly used in video cameras, digital cameras, television and computer displays, and mobile phone displays. Colours are described by indicating the amount of red (r), blue (b), and green (g) light. A common way of specifying the r, g, and b values is to use an integer between 0 and 255 for each of the values.
The CMYK colour space is a common representation of colour used in the printing industry where cyan, magenta, yellow, and black ink are combined to produce a spectrum of colours. Colours are described by indicating the amount of cyan (c), magenta (m), yellow (y), and black (k). A common way of specifying the c, m, y, and k values is to use a real number between 0.0 and 1.0 for each of the values.
Here are some examples of colour representations in both colour spaces:
colour | r | g | b | c | m | y | k | |
---|---|---|---|---|---|---|---|---|
black | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | 1.0 | |
white | 255 | 255 | 255 | 0.0 | 0.0 | 0.0 | 0.0 | |
red | 255 | 0 | 0 | 0.0 | 1.0 | 1.0 | 0.0 | |
green | 0 | 255 | 0 | 1.0 | 0.0 | 1.0 | 0.0 | |
blue | 0 | 0 | 255 | 1.0 | 1.0 | 0.0 | 0.0 | |
crimson (web colour) | 220 | 20 | 60 | 0.0 | 0.9090909090909092 | 0.7272727272727272 | 0.13725490196078427 |
You can convert RGB values to CMYK values using the
following formulas:
w = max(r / 255, g / 255, b / 255)
c = (w - (r / 255)) / w
m = (w - (g / 255)) / w
y = (w - (b / 255)) / w
k = 1 - w
Write a program that reads r, g, b values as integers from the command line and prints the equivalent c, m, y, k values. The required output is described in the next section. A possible starting point is shown below that computes w for you (because you don't know how to compute the maximum of three numbers yet), but there is a logical error on the 3 lines highlighted that you will need to fix.
Your program should print the values of c, m, y, and k on separate lines; for example:
java ColourConvert 255 0 0
0.0
1.0
1.0
0.0
NOTE: If you try java ColourConvert 0 0 0, your program should output something unusual.
Compile, debug, and run your program. Repeat these steps until you are convinced that your program is correct.
Submit your program using the command:
submit 1020 L02 ColourConvert.java