【发布时间】:2019-10-17 08:34:26
【问题描述】:
这是打印我在内存中的位置,为什么会发生这种情况以及如何解决它。
这是作业或考试的问题:
- 方法将这个 RationalNumber 的分子乘以 r 的分子,并将这个 RationalNumber 的分母乘以 r 的分母。以下哪项可用于替换 /* 缺少的代码 */ 以使 multiply() 方法按预期工作?
这些是解决方案,我需要选择:
num = num * r.num;
den = den * r.den;
this.num = this.num * r.num;
this.den = this.den * r.den;
num = num * r.getNum();
den = den * r.getDen();
我尝试了一切,但没有任何效果。
这是我的代码:
public class RationalNumber {
private int num;
private int den; // den != 0
/** Constructs a RationalNumber object.
* @param n the numerator
* @param d the denominator
* Precondition: d != 0
*/
public RationalNumber(int n, int d) {
num = n;
den = d;
}
/** Multiplies this RationalNumber by r.
* @param r a RationalNumber object
* Precondition: this.den() != 0
*/
public void multiply(RationalNumber r) {
/* missing code */
num = num * r.num;
den = den * r.den;
//this.num = this.num * r.num;
//this.den = this.den * r.den;
//num = num * r.getNum();
//den = den * r.getDen();
}
/** @return the numerator
*/
public int getNum() {
/* implementation not shown */
return num;
}
/** @return the denominator
*/
public int getDen() {
/* implementation not shown */
return den;
}
public static void main(String[] args){
RationalNumber num = new RationalNumber(10, -1);
System.out.println(num);
}
// Other methods not shown.
}
【问题讨论】:
标签: java object methods printing main