package com.chenswe;
/**
* Created by chen_swe on 3/13/16.
*/publicclassRational {privatelong numerator;
privatelong denominator;
publicbooleanisZero() {
return numerator == 0;
}
publicRational(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;
}
}
publicstaticlonggcd(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())
returnthis;
long newNumerator = this.numerator * b.denominator +
b.numerator * this.denominator;
long newDenominator = this.denominator * b.denominator;
returnnew Rational(newNumerator, newDenominator);
}
Rational minus(Rational b){
if(b.isZero())
returnthis;
if(this.isZero())
returnnew Rational(-b.numerator,b.denominator);
long newNumerator = this.numerator * b.denominator -
b.numerator * this.denominator;
long newDenominator = this.denominator * b.denominator;
returnnew Rational(newNumerator, newDenominator);
}
Rational times(Rational b){
if(this.isZero())
returnthis;
if(b.isZero())
return b;
long newNumerator = this.numerator * b.numerator;
long newDenominator = this.denominator * b.denominator;
returnnew Rational(newNumerator, newDenominator);
}
Rational divides(Rational b){
if(this.isZero())
returnthis;
assert !b.isZero() : "/ by Zero!";
if(b.isZero()){
thrownew ArithmeticException();
}
long newNumerator = this.numerator * b.denominator;
long newDenominator = this.denominator * b.numerator;
returnnew Rational(newNumerator, newDenominator);
}
boolean equals(Rational that){
if(this == that) returntrue;
if(that == null) returnfalse;
if(this.numerator != that.numerator ||
this.denominator != that.denominator)
returnfalse;
returntrue;
}
public String toString(){
return"Rational: [" + numerator +"/" + denominator +"]";
}
publicstaticvoidmain(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.
*/publicclassTransaction {privatefinal String name;
privatefinal Date date;
privatefinaldouble amount;
publicTransaction(String name,Date date, double amount)
throws IllegalArgumentException{
this.name = name;
this.date = date;
this.amount = amount;
if(Double.isNaN(amount) || Double.isInfinite(amount))
thrownew IllegalArgumentException(amount + " is NaN or is infinite!");
}
publicTransaction(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
*/publicdoubleamount() {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
*/publicbooleanequals(Object other){
if(this == other) returntrue;
if(other == null) returnfalse;
if(this.getClass() != other.getClass()) returnfalse;
Transaction that = (Transaction)other;
if(!this.date.equals(that.date)) returnfalse;
if(this.name != that.name) returnfalse;
if(Math.abs(this.amount - that.amount) > 1e-5)
returnfalse;
returntrue;
}
publicstaticvoidmain(String[] args) {
Transaction transaction = new Transaction("Stefen 3/11/16 100.0");
System.out.println(transaction);
}
}