Doing a course on Code HS, errors say "Bad operator types for binary operator" for lines with the public void. Not sure what I'm doing wrong

huangapple go评论85阅读模式
英文:

Doing a course on Code HS, errors say "Bad operator types for binary operator" for lines with the public void. Not sure what I'm doing wrong

问题

练习说明

在这个练习中,你需要从之前的 Fraction 类开始,并通过添加一些实用的方法来扩展它。

public void add(Fraction other)
public void subtract(Fraction other)
public void multiply(Fraction other)
public int getNumerator();
public int getDenominator();
public void setNumerator(int x);
public void setDenominator(int x);
public String toString();

请使用 FractionTester 文件逐步进行测试。

请注意:

public void add(Fraction other)
public void subtract(Fraction other)
public void multiply(Fraction other)

这些是无返回值的方法。它们不会返回任何内容。这些方法不应该创建一个新的 Fraction 对象并返回它。

相反,这些方法应该修改实例变量,使其加、减或乘以另一个 Fraction 对象。

例如,以下代码:

Fraction first = new Fraction(1, 2);
Fraction second = new Fraction(1, 3);
System.out.println();

System.out.println("BEFORE:");
System.out.println("first: " + first);
System.out.println("second: " + second);

first.multiply(second);

System.out.println("AFTER:");

System.out.println("first: " + first);
System.out.println("second: " + second);

应该输出:

BEFORE:
first: 1 / 2
second: 1 / 3

AFTER:
first: 1 / 6
second: 1 / 3

Fraction 对象 first 被修改,通过乘以 Fraction 对象 second。first 被影响,而 second 没有。1/2 变为 1/6,因为它乘以了 1/3。

这是我的代码:

public class Fraction
{
    // 在这里创建你的实例变量和构造方法
    
    // 实例变量
    private int num;
    private int den;
    
    // 构造方法
    public Fraction(int nume, int dene)
    {
        num = nume;
        den = dene;
    }
    

    public void add(Fraction other) 
    {
        Fraction a = num / den + other;
    }
    
    public void subtract(Fraction other) 
    {
        Fraction b = num / den - other;
    }
    
    public void multiply(Fraction other) 
    {
        Fraction c = num / den * other;
    }
    
    public String toString() 
    {
        return "";
    }
}
英文:

EXERCISE DIRECTIONS

In this exercise, you must take your Fraction class
from earlier and extend it by adding a few handy methods.

public void add(Fraction other)
public void subtract(Fraction other)
public void multiply(Fraction other)
public int getNumerator();
public int getDenominator();
public void setNumerator(int x);
public void setDenominator(int x);
public String toString();

Use the FractionTester file to test as you go along.

Note that

public void add(Fraction other)
public void subtract(Fraction other)
public void multiply(Fraction other)

are void methods. They do not return anything. These methods should not create a new Fraction and return it.

Instead, these methods should modify the instance variables to be added, subtracted, or multiplied by the Fraction other.

For example, the following code:

Fraction first = new Fraction(1, 2);
Fraction second = new Fraction(1, 3);
System.out.println();

System.out.println("BEFORE:");
System.out.println("first: " + first);
System.out.println("second: " + second);

first.multiply(second);

System.out.println("AFTER:");

System.out.println("first: " + first);
System.out.println("second: " + second);

Should print out:

BEFORE:
first: 1 / 2
second: 1 / 3

AFTER:
first: 1 / 6
second: 1 / 3
The Fraction first was modified by being multiplied by the Fraction second. first was affected, second was not. 1/2 became 1/6 because it was multiplied by 1/3.

This is my code:

public class Fraction
{
    // Create your instance variables and constructor here
    
    //Instance variables
    private int num;
    private int den;
    
    //Constructor
    public Fraction(int nume, int dene)
    {
        num = nume;
        den = dene;
    }
    

    public void add(Fraction other) 
    {
        Fraction a = num/den + other;
    }
    
    public void subtract(Fraction other) 
    {
        Fraction b = num/den - other;
    }
    
    public void multiply(Fraction other) 
    {
        Fraction c = num/den * other;
    }
    
    public String toString() 
    {
        return "";
    }
}

答案1

得分: 1

你不能直接将int(例如dennum)与Fraction对象相乘。您需要“解引用”传递的分数参数,然后更新“调用实例”的dennum组件。

原始代码:

public void multiply(Fraction other)  {
        Fraction c = num/den * other;
 }

应替换为:

public void multiply(Fraction other)  {
        num = num * other.num;
        den = den * other.den;
 }

在执行加法或减法时,您需要找到共同的分母。

英文:

You can't multiply an int (e.g. den or num) directly by a Fraction object. You need to dereference the passed fraction argument and then update the den and num components of the calling instance.

This


public void multiply(Fraction other)  {
        Fraction c = num/den * other;
 }

Needs to be replaced with this.

public void multiply(Fraction other)  {
        num = num * other.num;
        den = den * other.den;
 }

When you add or subtract, you need to find common denominators.

huangapple
  • 本文由 发表于 2020年5月5日 04:01:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61600619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定