在主方法对象java中打印时在内存中的打印位置

在主方法对象java中打印时在内存中的打印位置,java,object,methods,printing,main,Java,Object,Methods,Printing,Main,这是打印我在内存中的位置,为什么会发生这种情况,我如何修复它 这是家庭作业或考试的问题: 该方法将此有理数的分子乘以r的分子,并将此有理数的分母乘以r的分母。以下哪项可用于替换/*缺失的代码*/以便乘法方法按预期工作? 这些是解决方案,我需要选择: num = num * r.num; den = den * r.den; this.num = this.num * r.num; this.den = this.den * r.den; num = num * r.get

这是打印我在内存中的位置,为什么会发生这种情况,我如何修复它

这是家庭作业或考试的问题:

该方法将此有理数的分子乘以r的分子,并将此有理数的分母乘以r的分母。以下哪项可用于替换/*缺失的代码*/以便乘法方法按预期工作? 这些是解决方案,我需要选择:

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.
}

您需要重写RationalNumber类的toString方法

System.out.println(num);
如果我们没有指定类的任何特定属性或方法,上面的代码将打印所提供类的toString方法的返回值

因为您的RationalNumber类没有重写toString,所以它会查找其超类toString对象类

您可以通过添加

@Override
public String toString(){
    return num + "/" + den;
}
之后,您将需要有2个RationalNumber对象

例如,在数学中,1⁄2*1⁄3=1⁄6

在你的主要方法中会有这样的东西

public static void main(String[] args){
    RationalNumber rn1 = RationalNumber(1,2); //This represent 1/2
    RationalNUmber rn2 = RationalNumber(1,3); //This represent 1/3
    rn1.multiply(rn2); //This equals to 1/2 * 1/3, it multiply the num and den variable of rn1 object with rn2
    System.out.println(rn1); //This will invoke the toString() of rn1
}

谢谢,但现在它只是打印分子和分母,仍然不是上面给出的运算结果。这是因为您没有调用multiplyRationalNumber r方法。我编辑了将调用乘法运算的答案。