Programming Assignment 9: Creating Data Types

Here is the original link

  • ColorHSB.java(颜色数据的HSB格式,hue–saturation–brightness,换种角度来思考这种数据类型)
public class ColorHSB {

    private final int h, s, b;

    // Creates a color with hue h, saturation s, and brightness b.
    public ColorHSB(int h, int s, int b) {
        if (h < 0 || h > 359 || s < 0 || s > 100 || b < 0 || b > 100)
        {
            throw new IllegalArgumentException();
        }
        this.h = h;
        this.s = s;
        this.b = b;
    }

    // Returns a string representation of this color, using the format (h, s, b).
    public String toString() {
        return "(" + h + ", " + s + ", " + b + ")";
    }

    // Is this color a shade of gray?
    public boolean isGrayscale() {
        return s == 0 || b == 0;
    }

    // Returns the squared distance between the two colors.
    public int distanceSquaredTo(ColorHSB that) {
        if (that == null) {
            throw new IllegalArgumentException();
        }
        int res = 0;
        int h1 = this.h, h2 = that.h;
        int s1 = this.s, s2 = that.s;
        int b1 = this.b, b2 = that.b;
        res += Math.min((h1 - h2) * (h1 - h2), (360 - Math.abs(h1 - h2)) * (360 - Math.abs(h1 - h2)));
        res += (s1 - s2) * (s1 - s2);
        res += (b1 - b2) * (b1 - b2);
        return res;
    }
    
    // Sample client (see below).
    public static void main(String[] args) {
        int h = Integer.parseInt(args[0]);
        int s = Integer.parseInt(args[1]);
        int b = Integer.parseInt(args[2]);
        ColorHSB color = new ColorHSB(h, s, b);
        String name = null;
        ColorHSB ans = null;
        int min = Integer.MAX_VALUE;

        while (!StdIn.isEmpty())
        {
            String tmpName = StdIn.readString();
            h = Integer.parseInt(StdIn.readString());
            s = Integer.parseInt(StdIn.readString());
            b = Integer.parseInt(StdIn.readString());
            ColorHSB tmpColor = new ColorHSB(h, s, b);
            int dist = color.distanceSquaredTo(tmpColor);
            if (min > dist)
            {
                min = dist;
                name = tmpName;
                ans = tmpColor;
            }
        }
        StdOut.println(name + " " + ans);
    }
}
  • Clock.java(简单的时钟模拟,但是处理入参可不简单啊,各种奇奇怪怪的输入,还有格式化输出HH:MM的标准时间)
public class Clock {

    private int h;
    private int m;

    // Creates a clock whose initial time is h hours and m minutes.
    public Clock(int h, int m) {
        if (h < 0 || h > 23 || m < 0 || m > 59) {
            throw new IllegalArgumentException();
        }
        this.h = h;
        this.m = m;
    }

    // Creates a clock whose initial time is specified as a string, using the format HH:MM.
    public Clock(String s) {
        if (s.length() != 5 || s.charAt(2) != ':') {
            throw new IllegalArgumentException();
        }
        // 输入的参数有非数字
        for (int i = 0; i < s.length(); i++)
        {
            if (i != 2 && !Character.isDigit(s.charAt(i))) {
                throw new IllegalArgumentException();
            }
        }
        this.h = Integer.parseInt(s.substring(0, 2));
        this.m = Integer.parseInt(s.substring(3, 5));
        if (h < 0 || h > 23 || m < 0 || m > 59) {
            throw new IllegalArgumentException();
        }
    }

    // Returns a string representation of this clock, using the format HH:MM.
    public String toString() {
        return String.format("%02d:%02d", h, m);
    }

    // Is the time on this clock earlier than the time on that one?
    public boolean isEarlierThan(Clock that) {
        if (this.h == that.h) {
            return this.m < that.m;
        }
        return this.h < that.h;
    }

    // Adds 1 minute to the time on this clock.
    public void tic() {
        m += 1;
        h += m / 60;
        m %= 60;
        h %= 24;
    }


    // Adds Δ minutes to the time on this clock.
    public void toc(int delta) {
        if (delta < 0) {
            throw new IllegalArgumentException();
        }
        m += delta;
        h += m / 60;
        m %= 60;
        h %= 24;
    }

    // Test client (see below).
    public static void main(String[] args) {
        Clock clock = new Clock(args[0]);
        StdOut.println(clock.toString());
        clock.tic();
        StdOut.println(clock.toString());
        clock.toc(60);
        StdOut.println(clock.toString());
    }
}
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值