tkgweight.java
来自「JAVA 2 入门经典 的 书上代码」· Java 代码 · 共 89 行
JAVA
89 行
//CHAPTER 5, SOLUTION 4
/*
The only change to the class definition is the addition of the package statement.
To compile the package, the current directory should be directory containing
the Measures directory and the command should be:
javac -source 1.4 Measures/tkgWeight
*/
package Measures;
public class tkgWeight {
public static final int KG_PER_TON = 1000;
public static final int GRAMS_PER_KG = 1000;
public static final int GRAMS_PER_TON = GRAMS_PER_KG*KG_PER_TON;
// private member variables
private int tons = 0;
private int kilograms = 0;
private int grams = 0;
// Constructors:
public tkgWeight(double kg) {
this((int)Math.round(kg*GRAMS_PER_KG));
}
public tkgWeight(int g) {
tons = g/GRAMS_PER_TON;
kilograms = (g - tons*GRAMS_PER_TON)/GRAMS_PER_KG;
grams = g - tons*GRAMS_PER_TON - kilograms*GRAMS_PER_KG;
}
public tkgWeight(int t, int kg, int g) {
this(t*GRAMS_PER_TON + kg*GRAMS_PER_KG + g);
}
public tkgWeight(){}
// Methods
public String toString() {
return Integer.toString(tons) + "t " + kilograms + "kg " + grams + "g";
}
public int toGrams() {
return tons*GRAMS_PER_TON + kilograms*GRAMS_PER_KG + grams;
}
public double toKilograms() {
return ((double)toGrams())/GRAMS_PER_KG;
}
public tkgWeight add(tkgWeight weight) {
return new tkgWeight(toGrams() + weight.toGrams());
}
public tkgWeight subtract(tkgWeight weight) {
return new tkgWeight(toGrams() - weight.toGrams());
}
public tkgWeight multiply(int n) {
return new tkgWeight(n*toGrams());
}
public tkgWeight divide(int n) {
return new tkgWeight(toGrams()/n);
}
// Compare two weights
// Return value is 1 if current greater than arg
// 0 if current equal to arg
// -1 if current less than arg
public int compare(tkgWeight weight) {
return greaterThan(weight) ? 1 : (equals(weight) ? 0 : -1);
}
public boolean equals(tkgWeight weight) {
return toGrams() == weight.toGrams();
}
public boolean lessThan(tkgWeight weight) {
return toGrams() < weight.toGrams();
}
public boolean greaterThan(tkgWeight weight) {
return toGrams() > weight.toGrams();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?