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.


  1. Color data type. Write a 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.


    A color in HSB format is composed of three components:

    Implement the following public API:

    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)
    
    }
    
    Here is some more information about the required behavior:


  2. Clock data type. Write a data type 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:
    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)
    }
    
    Here is some more information about the required behavior:


    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).


    This assignment was developed by Kevin Wayne.
    Copyright © 2019.