我想实现Comparable接口并重写compareTo。所有这些似乎都工作正常,但我对我的测试结果感到非常困惑。如果我尝试使用 2 个有理数调用compareTo,我会收到“未找到符号”错误。我已经查过了,有人建议将有理数转换为字符串。当我这样做时,fsr似乎我什至没有调用我自己的函数(我更改了函数内的参数,这没有任何改变),相反,程序总是返回2。如果我与等于它比较,它工作得很好。
这是我的代码,如果需要我可以添加包。
`package app.exercise.testing;
import app.exercise.algebra.*;
import java.lang.Comparable;
public class compRational extends Rational implements Comparable<Rational>{
public int compareTo(Rational y){
System.out.println("Troubleshoot: "+(int)this.getN()*(int)y.getD()+ " "+(int)y.getN()*(int)this.getD());
return Integer.compare((int)this.getN()*(int)y.getD(),(int)y.getN()*(int)this.getD());
}
//Testing
public static void main(String args[]){
Rational test=new Rational(3,8);
Rational test2=new Rational(10,3);
Rational test3=new Rational(3,8);
// Compare
System.out.println("Compare Test: "+test.compareTo(test2));
if((test.toString().compareTo(test3.toString())==0) == test.equals(test3)){
System.out.println("compare test 2: "+true);
}
}
}`
由于可比较函数的第一行从未工作过(“故障排除”打印),我假设使用两个字符串调用compareTo不会调用我自己的compareTo,这让我很困惑我什至会如何调用我自己的函数
编辑:解决了问题。 compRational 类简单地实现了 Comparable,但我现在重写了 Rational 中的compareTo() 函数。谢谢大家的帮助。
更详细的修复说明: Rational 类 - 请注意,我在第二个函数中覆盖了compareTo,但没有实现它。
public class Rational extends BasisFraction{
private long numerator;
private long denominator;
/**
Implementierung von setND aus @see BasisFraction um Parameter eines @see Rational Objekts zu verändern
Parameter
@param numerator neuer Zähler des Objekts
@pram denominator neuer Nenner des Objekts
*/
/**
@Override
*/
public int compareTo(Rational y){
return Integer.compare((int)this.getN()*(int)y.getD(),(int)y.getN()*(int)this.getD());
}
protected void setND (long numerator, long denominator ){
long teiler=ggT(numerator, denominator);
this.numerator=numerator/teiler;
this.denominator=denominator/teiler;
}
/**Default Konstruktor für @see Rational
*/
public Rational(){
setND(1,1);
}
/**
Konstruktor für @see Rational
kürzt mit @see ggT weitmöglichst, setzt nur Zähler negativ falls notwendig
Parameter
@param n Nenner
@param d Zähler
*/
public Rational(long n,long d){
long teiler=ggT(n,d);
n=n/teiler;
d=d/teiler;
if(d < 0){
setND(-n,-d);
}
else{
setND(n, d);
}
}
/**
Kehrwert
@return Kehrwert übergebender rationaler Zahl
*/
public Fractional reciprocal(){
Rational rückgabe= new Rational(this.denominator, this.numerator);
return rückgabe;
}
/**
* *(-1)
@return übergebende rationale Zahl *(-1)
*/
public Fractional negation(){
Rational rückgabe=new Rational(this.numerator*-1,this.denominator);
return rückgabe;
}
/**
getter Nenner
@return Nenner
*/
public long getD(){
return this.denominator;
}
/**
getter Zähler
@return Zähler
*/
public long getN(){
return this.numerator;
}
/**größter gemeinsamer Teiler
@param n Zahl 1
@param d Zahl 2
*/
public static long ggT(long n, long d){
if(d == 0)
return n;
return ggT(d, n%d);
}
/**
Umwandlung eines @see Rational Objekts in einen Strin
Rückgabe
@return Zähler / Nenner
*/
public String toString(){
return "" +getN() + "/"+getD();
}
/**
Kopiert übergebendes Objekt
Rückgabe
@return neues Objekt mit gleichen Parametern
*/
public Rational clone(){
Rational rückgabe=new Rational(this.getN(), this.getD());
return rückgabe;
}
/**
überprüft ob 2 Objekte gleich sind, also dieselben Parameter enthalten
Parameter
@param y @Rational Objekt mit dem verglichen wird
@return bool
*/
public boolean equals(Rational y){
if(this.getN()==y.getN()&&this.getD()==y.getD()){
return true;
}
else
return false;
}
/**
berechnet Hashcode eines Objekts, indem Zähler+Nenner quadriert und mod 10 gerechnet wird
@return HashCode
*/
public int hashCode(){
return (int)Math.pow(this.getN()+this.getD(),2)%10;
}
}
现在是具有 Testfeatures 的最终 compRational 类:由于 compRational 扩展了 Rational,因此它继承了 Rational 类中声明的compareTo 方法,该方法覆盖了 Comparable 中的compareTo。
package app.exercise.algebra;
import java.lang.Comparable;
public class compRational extends Rational implements Comparable<Rational>{
public static void main(String args[]){
Rational test=new Rational(3,8);
Rational test2=new Rational(10,3);
Rational test3=new Rational(3,8);
// Compare
System.out.println("Compare Test: "+test.compareTo(test2));
if((test.compareTo(test3)==0) == test.equals(test3)){
System.out.println("compare test 2: "+true);
}
}
}
好的。在不了解有关您的任务的确切预期的更多详细信息的情况下,您将需要:
Comparable<Rational>
public CompRational(int num, int den) {
super(num,den);
}
@Override
public int compareTo(Rational y) {
// your existing code here
}
CompRational test = new CompRational(3, 8);
CompRational test2 = new CompRational(10, 3);
CompRational test3 = new CompRational(3, 8);
注意:课程应以大写字母开头