The purpose of this assignment is to give you practice creating data types. The first exercise involves an immutable data type; the second exercise considers a mutable data type.
ColorHSB.java
that represents a color in
hue–saturation–brightness (HSB) format,
along with a sample client.
The HSB color format is widely used in color pickers.
Implement the following public API:
Here is some more information about the required behavior:public class ColorHSB { // Creates a color with hue h, saturation s, and brightness b. public ColorHSB(int h, int s, int b) // Returns a string representation of this color, using the format (h, s, b). public String toString() // Is this color a shade of gray? public boolean isGrayscale() // Returns the squared distance between the two colors. public int distanceSquaredTo(ColorHSB that) // Sample client (see below). public static void main(String[] args) }
IllegalArgumentException
in the constructor
if any component is outside its prescribed range (0 to 359 for the hue,
0 to 100 for the saturation and brightness);
throw an IllegalArgumentException
in distanceSquaredTo()
if its argument is null
.
(26, 85, 96)
.
\( \min \left \{(h_1 - h_2)^2 \;,\;\; (360 - |h_1 - h_2|)^2 \right \} \;\;+\;\; (s_1 - s_2)^2 \;\;+\;\; (b_1 - b_2)^2 \)For example, the squared distance between (350, 100, 45) and (0, 100, 50) is \(10^2 + 0^2 + 5^2 = 125\).
main()
method should take three integer command-line
arguments h, s, and b; read a list of pre-defined colors from
standard input; and print to standard output
the pre-defined color that is closest to \((h, s, b)\).
~/Desktop/oop2> java-introcs ColorHSB 25 84 97 < web.txt Red (0, 100, 100) ~/Desktop/oop2> java-introcs ColorHSB 350 100 45 < web.txt Maroon (0, 100, 50) ~/Desktop/oop2> java-introcs ColorHSB 25 84 97 < wiki.txt Princeton_Orange (26, 85, 96)
Clock.java
that represents time on a 24-hour clock,
such as 00:00, 13:30, or 23:59. Time is measured in hours (00–23) and
minutes (00–59). To do so, implement the following public API:
Here is some more information about the required behavior:public class Clock { // Creates a clock whose initial time is h hours and m minutes. public Clock(int h, int m) // Creates a clock whose initial time is specified as a string, using the format HH:MM. public Clock(String s) // Returns a string representation of this clock, using the format HH:MM. public String toString() // Is the time on this clock earlier than the time on that one? public boolean isEarlierThan(Clock that) // Adds 1 minute to the time on this clock. public void tic() // Adds Δ minutes to the time on this clock. public void toc(int delta) // Test client (see below). public static void main(String[] args) }
IllegalArgumentException
if either integer argument is outside its prescribed bounds (hours between 0 and 23, minutes
between 0 and 59).
IllegalArgumentException
if either the string argument is not in this format or if it does not
correspond to a valid time between 00:00 and 23:59.
00:00
and 23:59
.
IllegalArgumentException
if Δ is negative.
main()
method must call each instance method directly
and help verify that they work as prescribed.
Submission.
Submit a .zip
file containing
ColorHSB.java
and
Clock.java
.
You may not call library functions except those in the java.lang
(such as Integer.parseInt()
and Math.sqrt()
).
Use only Java features that have already been introduced in the course
(e.g., objects but not interfaces).