我已将此代码编写为我在AP Computer Science中收到的作业的答案,而我的老师正在重做此操作,因为答案不起作用。我只能在我的机器上编译它,所以我要依靠老师确定现在产生的结果如下。这项工作是在今晚午夜之前完成的,我等到现在的拖延症患者在这里寻求帮助,但还是非常感谢。另外,这是我第一次来,很抱歉,某些代码的格式不正确。谢谢您的帮助!
结果ATM
if a = 3/4 and b = 5/6
a.add(b) produces 35/24; This should be 19/12
a.subtract(b) produces 1/0; This should be -1/12
a.divide(b) produces 11/1; This should be 9/10
a.multiply(b) produces 6/-5; This should be 5/8
这是代码。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
class Fraction{
private int numer;
private int denom;
private Fraction answer;
//Definition
public Fraction(int num, int den)
{
numer = num;
denom = den;
simplify();
}
//Simplify
void simplify()
{
int gcd = findGCD(numer, denom);
numer /= gcd;
denom /= gcd;
}
//GCD Function
int findGCD(int a, int b)
{ int temp;
while(b != 0)
{
temp = b;
b = a % b;
a = temp;
}
return a;
}
//GetNumerator
public int getNumer()
{
return numer;
}
//GetDenominator
public int getDenom()
{
return denom;
}
//Fraction Add Method
Fraction add(Fraction x)
{
Fraction answer;
if(denom == x.denom)
{
answer = new Fraction(x.numer + numer, denom);
}
else
{
int den = denom * x.getDenom();
int num = numer * x.getNumer();
num += x.getNumer() * denom;
answer = new Fraction(num, den);
}
return answer;
}
//Fraction Subtract Method
Fraction subtract(Fraction x)
{
Fraction answer;
if(x.denom == denom)
{
answer = new Fraction(numer - x.numer, denom);
}
else
{
int den = denom / x.getDenom();
int num = numer / x.getNumer();
num -= x.getNumer() * denom;
answer = new Fraction(num, den);
}
return answer;
}
//Fraction Multiply Method
Fraction multiply(Fraction x)
{
Fraction answer;
if(x.denom == denom)
{
answer = new Fraction(numer + x.numer, denom);
}
else
{
int den = denom + x.getDenom();
int num = numer + x.getNumer();
num -= x.getNumer() * denom;
answer = new Fraction(num, den);
}
return answer;
}
//Fraction Divide Method
Fraction divide(Fraction x)
{
Fraction answer;
if(x.denom == denom)
{
answer = new Fraction(numer - x.numer, denom);
}
else
{
int den = denom - x.getDenom();
int num = numer - x.getNumer();
num -= x.getNumer() * denom;
answer = new Fraction(num, den);
}
return answer;
}
//@Override
public boolean equals(Fraction x)
{
boolean answer = false;
if(numer == x.numer && denom == x.denom)
{
answer = true;
}
return answer;
}
//ToString
public String toString()
{
return (Integer.toString(numer) + "/" +
Integer.toString(denom));
}
//Main Method
public void main(String[]args)
{
Fraction a = new Fraction(3,4);
Fraction b = new Fraction(5,6);
System.out.println(a.toString());
System.out.println(b.toString());
Fraction c = a.add(b);
System.out.println(c.toString());
c = a.subtract(b);
System.out.println(c.toString());
c = a.divide(b);
System.out.println(c.toString());
c = a.multiply(b);
System.out.println(c.toString());
}
}
最佳答案
因此,开始就只有一件事:一直使用getDenom(),因为在您的代码中有时是getDenom,有时是denom所以..
对于add()方法:
在标称不同的情况下,您错了。
你所要做的 :
denom = this.getDenom() * x.getDenom();
num = this.getNum() * x.getDenom() + x.getNumer() * this.getDenom();
//and check if simplification is possible.
return new Fraction(num,denom);
对于其他操作来说,这是相同的事情。检查您是否乘以良好的分母或分子,因为事实并非如此,并且不要忘记简化。
希望能帮助到你
关于java - Java中的分数加法,减法,乘法和除法(作业),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8453485/