1.2 数据抽象(Part 2 -- Creative Problems)

State: Done!


第十六 & 十七题

package com.chenswe;


/**
 * Created by chen_swe on 3/13/16.
 */
public class Rational {
    private long numerator;
    private long denominator;

    public boolean isZero() {
        return numerator == 0;
    }

    public Rational(long Numerator, long Denominator) {

        assert Denominator != 0 : "Demominator can't be [Zero]!";

        long Gcd;
        if (Numerator != 0) {
            Gcd = gcd(Numerator, Denominator);
            numerator = Numerator / Gcd;
            if(Denominator < 0)
                numerator *= -1;
            denominator = Math.abs(Denominator) / Gcd;
        } else {
            numerator = 0;
            denominator = 1;
        }
    }

    public static long gcd(long n, long d) {
        long x = Math.abs(n);
        long y = Math.abs(d);
        if (x < y) {   //swap x, y; if x < y
            x += y;
            y = x - y;
            x -= y;
        }
        while (x % y != 0) {
            long tmp = y;
            y = x % y;
            x = tmp;
        }
        return y;
    }

    Rational plus(Rational b) {
        if (this.isZero())
            return b;
        if (b.isZero())
            return this;
        long newNumerator = this.numerator * b.denominator +
                b.numerator * this.denominator;
        long newDenominator = this.denominator * b.denominator;
        return new Rational(newNumerator, newDenominator);
    }

    Rational minus(Rational b){
        if(b.isZero())
            return this;
        if(this.isZero())
            return new Rational(-b.numerator,b.denominator);

        long newNumerator = this.numerator * b.denominator -
                b.numerator * this.denominator;
        long newDenominator = this.denominator * b.denominator;
        return new Rational(newNumerator, newDenominator);
    }

    Rational times(Rational b){
        if(this.isZero())
            return this;
        if(b.isZero())
            return b;
        long newNumerator = this.numerator * b.numerator;
        long newDenominator = this.denominator * b.denominator;
        return new Rational(newNumerator, newDenominator);
    }

    Rational divides(Rational b){
        if(this.isZero())
            return this;
        assert !b.isZero() : "/ by Zero!";
        if(b.isZero()){
            throw new ArithmeticException();
        }
        long newNumerator = this.numerator * b.denominator;
        long newDenominator = this.denominator * b.numerator;
        return new Rational(newNumerator, newDenominator);
    }

    boolean equals(Rational that){
        if(this == that)    return true;
        if(that == null)    return false;
        if(this.numerator != that.numerator ||
                this.denominator != that.denominator)
            return false;
        return true;
    }

    public String toString(){
        return "Rational: [" + numerator +"/" + denominator +"]";
    }
    public static void main(String[] args) {
        Rational rational1 = new Rational(1,10);
        System.out.println(rational1);
        Rational rational2 = new Rational(1,5);
        System.out.println(rational2);
        System.out.println();

        System.out.println("plus " + rational1.plus(rational2));
        System.out.println("minus " + rational1.minus(rational2));
        System.out.println("times " + rational1.times(rational2));
        System.out.println("divide " + rational1.divides(rational2));
        System.out.println("isEqual: " + rational1.equals(rational2));
    }
}

第十九题

//在之前的Transaction类内增加了解析字符串构造的构造函数

package com.chenswe;

import edu.princeton.cs.algs4.Date;

/**
 * Created by chen_swe on 3/11/16.
 */
public class Transaction {
    private final String name;
    private final Date date;
    private final double amount;

    public Transaction(String name,Date date, double amount)
            throws IllegalArgumentException{

        this.name = name;
        this.date = date;
        this.amount = amount;
        if(Double.isNaN(amount) || Double.isInfinite(amount))
            throw new IllegalArgumentException(amount + " is NaN or is infinite!");
    }

    public Transaction(String transaction){
        String[] fields = transaction.split(" ");
        name = fields[0];
        date = new Date(fields[1]);
        amount = Double.parseDouble(fields[2].trim());
    }

    /**
     * Returns the name of the customer involved in this transaction.
     *
     * @return the name of the customer involved in this transaction.
     */
    public String who(){return name;}

    /**
     * Returns the date of this transaction.
     *
     * @return the date of this transaction
     */
    public Date when(){return date;}

    /**
     * Returns the amount of this transaction.
     *
     * @return the amount of this transaction
     */
    public double amount() {return amount;}

    /**
     * Returns a string representation of this transaction.
     *
     * @return a string representation of this transaction
     */
    public String toString(){
        String str = "[" + name + "] had an [$" + amount + "]'s transaction " +
                "at [" + date + "]";
        return str;
    }

    /**
     * Compares this transaction to the specified object.
     *
     * @param  other the other transaction
     * @return true if this transaction is equal to <tt>other</tt>; false otherwise
     */
    public boolean equals(Object other){
        if(this == other)   return true;
        if(other == null)   return false;
        if(this.getClass() != other.getClass())     return false;

        Transaction that = (Transaction)other;
        if(!this.date.equals(that.date))            return false;
        if(this.name != that.name)                  return false;
        if(Math.abs(this.amount - that.amount) > 1e-5)
            return false;
        return true;
    }
    public static void main(String[] args) {
        Transaction transaction = new Transaction("Stefen 3/11/16 100.0");
        System.out.println(transaction);
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值