これが私のRational
クラスです:
package lab18f;
import static java.lang.System.*;
class Rational implements Comparable<Rational>
{
//add two instance variables
private int num, den;
//write two constructors
public Rational ()
{
num = 1;
den = 1;
}
public Rational (int numer, int deno)
{
num = numer;
den = deno;
}
//write a setRational method
public void setRational(int numer, int deno)
{
num = numer;
den = deno;
}
//write a set method for numerator and denominator
public void setNumerator(int numer)
{
num = numer;
}
public void setDenominator(int deno)
{
den = deno;
}
public void add(Rational other)
{
//num1/den1 + num2/den2
//new numerator = (num1 * den2 + num2 * den1)
//new denominator = (den1 * den2)
int newNumer = (this.getNumer()*other.getDeno() + other.getNumer()*this.getDeno());
int newDeno = this.getDeno()*other.getDeno();
this.setRational(newNumer, newDeno);
reduce();
}
private void reduce()
{
int gcd = 0;
int numer = num;
int deno = den;
if(numer > deno)
gcd = gcd(numer,deno);
else if(numer < deno)
gcd = gcd(deno, numer);
else
gcd = numer;
this.setRational(num/gcd, den/gcd);
}
private int gcd(int numOne, int numTwo)
{
int one = numOne;
int two = numTwo;
int gcd = two;
if(two==0)
return one;
return gcd(two, one%two);
}
public Object clone ()
{
return new Rational(num, den);
}
//ACCESSORS
//write get methods for numerator and denominator
public int getNumer()
{
return num;
}
public int getDeno()
{
return den;
}
public boolean equals( Object obj)
{
Rational lhs = (Rational)obj;
if((getNumer()/getNumer()== lhs.getNumer()/lhs.getNumer()) && getDeno()/getNumer() == lhs.getDeno()/lhs.getNumer())
return true;
return false;
}
public int compareTo(Rational other)
{
return -1;
}
//write toString() method
public String toString()
{
return "" + getNumer() + "/" + getDeno();
}
}
クラスのランナーRational
クラス:
import static java.lang.System.*;
public class Lab18f
{
public static void main( String args[] )
{
Rational test = new Rational();
out.println("test = " + test);
Rational newOne = new Rational(3,4);
out.println("newOne = "+newOne);
out.println("test.equals(newOne) = "+test.equals(newOne));
newOne = (Rational)test.clone();
out.println("\n\nnewOne after test.clone() = "+newOne);
out.println("test.equals(newOne) = "+test.equals(newOne));
Rational rOne = new Rational(1,2);
Rational rTwo = new Rational(2,3);
out.println("1/2.equals(2/3) = "+rOne.equals(rTwo));
test.setRational(4,6);
out.println("2/3.equals(4/6) = "+rTwo.equals(test));
out.println("\n\nrOne = "+rOne);
out.println("rTwo = "+rTwo);
out.println("rOne.compareTo(rTwo) = "+rOne.compareTo(rTwo));
out.println("rTwo.compareTo(rOne) = "+rTwo.compareTo(rOne));
rOne.add(rTwo);
out.println("\n\nrOne.add(rTwo) = "+ rOne);
rOne.setRational(1,2);
rTwo.setRational(1,3);
rOne.add(rTwo);
out.println("\n\n1/2.add(1/3) = "+ rOne);
rOne.setRational(4,10);
rTwo.setRational(3,5);
rOne.add(rTwo);
out.println("\n\n4/10.add(3/5) = "+ rOne);
rOne.setRational(2,10);
rTwo.setRational(3,6);
rOne.add(rTwo);
out.println("\n\n2/10.add(3/6) = "+ rOne);
//1/4 + 2/8 = 1/2
rOne.setRational(1,4);
rTwo.setRational(2,8);
out.println("\n\n1/4.equals(2/8) = "+rOne.equals(rTwo));
rOne.add(rTwo);
out.println("\n\n1/4.add(2/8) = "+ rOne);
//1/6 + 2/8 = 5/12
rOne.setRational(1,6);
rTwo.setRational(2,8);
out.println("\n\n1/6.equals(2/8) = "+rOne.equals(rTwo));
rOne.add(rTwo);
out.println("\n\n1/6.add(2/8) = "+ rOne);
}
}
私の問題は、ランナークラス(明らかにクラスを実行するRational
)を実行すると、出力に次のようなステートメントがあることです。
テスト = 1/1
newOne = 3/4
test.equals(newOne) = true
明らかに分数 1/1 は 3/4 に等しくありません