📄 measurement.java
字号:
/** * Returns a new <tt>Measurement</tt> object that is the quotient * of this object divided by the specified value. * * @param d The value that will be the divisor of * this object. * @return A new <tt>Measurement</tt> object that is the quotient * of this object divided by the specified value. * The error of the new object is computed. * The unit and time of the new object is set to the * <tt>Unit</tt> and time of this object. */ public Measurement div(double d) { return new Measurement(value / d, error / Math.abs(d), unit, time); } /** * Returns a new <tt>Measurement</tt> object that is the sum * of this object added to the specified object. * * The error and unit of the new object are computed. * The time of the new object is set to the time of this object. * * @param m The <tt>Measurement</tt> object that will be added with * this object. * @return A new <tt>Measurement</tt> object that is the sum of this and m. * @see Unit * @throws ArithmeticException If the <tt>Unit</tt> objects * of this object and the specified object cannot be added. */ public Measurement add(Measurement m) { return new Measurement(value + m.value, error + m.error, unit.add(m.unit), time); } /** * Returns a new <tt>Measurement</tt> object that is the sum * of this object added to the specified value. * * @param d The value that will be added with * this object. * @param u The <tt>Unit</tt> object of the specified value. * @return A new <tt>Measurement</tt> object that is the sum * of this object added to the specified value. * The unit of the new object is computed. * The error and time of the new object is set to the * error and time of this object. * @throws ArithmeticException If the <tt>Unit</tt> objects * of this object and the specified value cannot be added. * @see Unit */ public Measurement add(double d, Unit u) { return new Measurement(value + d, error, unit.add(u), time); } /** * Returns a new <tt>Measurement</tt> object that is the sum * of this object added to the specified value. * * @param d The value that will be added with * this object. * @return A new <tt>Measurement</tt> object that is the sum * of this object added to the specified value. * The error, unit, and time of the new object is set to the * error, <tt>Unit</tt> and time of this object. */ public Measurement add(double d) { return new Measurement(value + d, error, unit, time); } /** * Returns a new <tt>Measurement</tt> object that is the subtraction * of the specified object from this object. * * @param m The <tt>Measurement</tt> object that will be subtracted from * this object. * @return A new <tt>Measurement</tt> object that is the subtraction * of the specified object from this object. * The error and unit of the new object are computed. * The time of the new object is set to the time of this object. * @throws ArithmeticException If the <tt>Unit</tt> objects * of this object and the specified object cannot be subtracted. * @see Unit */ public Measurement sub(Measurement m) { return new Measurement(value - m.value, error + m.error, unit.sub(m.unit), time); } /** * Returns a new <tt>Measurement</tt> object that is the subtraction * of the specified value from this object. * * @param d The value that will be subtracted from * this object. * @param u The <tt>Unit</tt> object of the specified value. * @return A new <tt>Measurement</tt> object that is the subtraction * of the specified value from this object. * The unit of the new object is computed. * The error and time of the new object is set to the * error and time of this object. * @throws ArithmeticException If the <tt>Unit</tt> objects * of this object and the specified object cannot be subtracted. * @see Unit */ public Measurement sub(double d, Unit u) { return new Measurement(value - d, error, unit.sub(u), time); } /** * Returns a new <tt>Measurement</tt> object that is the subtraction * of the specified value from this object. * * @param d The value that will be subtracted from * this object. * @return A new <tt>Measurement</tt> object that is the subtraction * of the specified value from this object. * The error, unit and time of the new object is set to the * error, <tt>Unit</tt> object and time of this object. */ public Measurement sub(double d) { return new Measurement(value - d, error, unit, time); } /** * Returns a <tt>String</tt> object representing this <tt>Measurement</tt> object. * * @return a <tt>String</tt> object representing this <tt>Measurement</tt> object. */ public String toString() { if (name == null) { StringBuffer sb = new StringBuffer(); sb.append(value); if (error != 0.0d) { sb.append(" +/- "); sb.append(error); } String u = unit.toString(); if (u.length() > 0) { sb.append(" "); sb.append(u); } name = sb.toString(); } return name; } /** * Compares this object with the specified object for order. Returns a * negative integer, zero, or a positive integer if this object is less * than, equal to, or greater than the specified object. * * <p>Note: This class has a natural ordering that is * inconsistent with equals. For this method, another <tt>Measurement</tt> * object is considered equal if there is some <tt>x</tt> such that * <pre> * getValue()-getError() <= x <= getValue()+getError() * </pre> * for both <tt>Measurement</tt> objects being compared. * * @param obj The object to be compared. * @return A negative integer, zero, or a positive integer if this object * is less than, equal to, or greater than the specified object. * * @throws ClassCastException If the specified object is not of type <tt>Measurement</tt>. * @throws ArithmeticException If the unit of the specified <tt>Measurement</tt> object is * not equal to the <tt>Unit</tt> object of this object. */ public int compareTo(Object obj) { if (this == obj) { return 0; } Measurement that = (Measurement) obj; if (!unit.equals(that.unit)) { throw new ArithmeticException( "Cannot compare " + this + " and " + that); } if (value == that.value) { return 0; } if (value < that.value) { if ((value + error) >= (that.value - that.error)) { return 0; } else { return -1; } } else { if ((value - error) <= (that.value + that.error)) { return 0; } else { return 1; } } } /** * Returns a hash code value for this object. * * @return A hash code value for this object. */ public int hashCode() { long bits = Double.doubleToLongBits(value + error); return ((int)(bits ^ (bits >>> 32))) ^ unit.hashCode(); } /** * Returns whether the specified object is equal to this object. * Two <tt>Measurement</tt> objects are equal if they have same value, * error and <tt>Unit</tt>. * * <p>Note: This class has a natural ordering that is * inconsistent with equals. See {@link #compareTo}. * * @param obj The object to compare with this object. * @return <tt>true</tt> if this object is equal to the specified * object; <tt>false</tt> otherwise. */ public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Measurement)) { return false; } Measurement that = (Measurement)obj; return (value == that.value) && (error == that.error) && unit.equals(that.unit); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -