【问题标题】:Main Method to be used by Test Class测试类使用的主要方法
【发布时间】:2017-06-17 16:59:42
【问题描述】:

嗨,我有我的主课,但我无法弄清楚如何从我的测试课输出我的代码。即使尝试了很多方法来输出两个分数的简单加法和减法,我也不明白,因为它应该在我的主要方法中执行,但似乎无法将它放入我的测试类。

这是我的所有功能类的代码:

package rational;

 public class Rational {

private int numer, denom;

 //constructors
    public Rational(){
        int num = 1;
        int den = 2;
        reduce();
    }
    public Rational(int num, int den){
    numer = num;
    denom = den;
    reduce();
    }
    public Rational(Rational x){
    numer = x.numer;
    denom = x.denom;
    reduce();
    }

   //setters
    public void setNumer(int num){
    numer = num;
    reduce();
    }
    public void setDenom(int den){
    denom = den;
    reduce();
    }
    public void setRational(int num, int den){
    numer = num;
    denom = den;
    reduce();
    }

     //getters
    public int getNumer(){
    return numer;
    }
    public int getDenom(){
    return denom;
    }

    //Copy method
    public void copyFrom(Rational x){
    numer = x.numer;
    denom = x.denom;
    reduce();
    }

    //Equals method        
    public boolean equals(Rational x){
    if (numer / denom == x.numer / x.denom){
    return(true);
            }
    else {
    return(false);
        }
    }

    //Compare to method
    public int compareTo(Rational x){
    if (numer / denom == x.numer / x.denom){
    return (0);
    }
    else if (numer / denom < x.numer / x.denom){
    return (-1);
    }
    else{
    return (1);
        }    
    }

    //Find greatest common divisor
    static int gcd(int x, int y){
    int r;
    while (y != 0) {
    r = x % y;
    x = y;
    y = r;
        }
    return x;
    }

    //Rational Addition            
    public void plus(Rational x){
    int greatdenom = x.denom * denom;       
    int multx = greatdenom / x.denom;
    int mult = greatdenom / denom;
    denom = x.denom * denom;
    numer = (x.numer * multx) + (numer * mult);
    reduce();
    }

    //Rational Subtraction
    public void minus(Rational x){
    int greatdenom = x.denom * denom;       
    int multx = greatdenom / x.denom;
    int mult = greatdenom / denom;
    denom = x.denom * denom;
    if (x.numer > numer){
    numer = (x.numer * multx) - (numer * mult);
        }
    else {
    numer = (numer * mult) - (x.numer * multx);
        }
    reduce();
    }

     //Multiplication       
    public void times(Rational x){
    numer = numer * x.numer;
    denom = denom * x.denom;
    reduce();
    }

    //Division        
    public void divBy(Rational x){
    numer = numer / x.numer;
    denom = denom / x.denom;
    reduce();
    }

     //Fraction simplifier        
    private void reduce(){
    int divisor;
    divisor = Rational.gcd(numer, denom);
    numer = numer / divisor;
    denom = denom / divisor;
    }

@Override
    public String toString(){
    if (denom == 1){
    return numer + "";
    }
    else{
    return numer + " / " + denom;
    }       
}
   }

【问题讨论】:

  • 那么主要方法在哪里?我看不到。
  • 你的测试课在哪里?
  • @justAJAVAGUY 你在哪里test 类和main 方法。
  • @LKTN.25 那是我想打电话的另一堂课
  • @DavidWallace 这是我想使用 main 调用的另一个类

标签: java object testing methods main


【解决方案1】:

您需要在您的 plus 方法中返回一个 Rational(而不是 void)。

public Rational plus(Rational x){
   //do addition stuff.
    return new Rational(//what numerator be//,//what denom should be//)
    }

作为一个建议,我会将所有这些操作符方法设为静态并接受 2 个参数。然后你会为两个有理数的分子和分母中的每一个使用“getter”。

像这样:

public static Rational plus(Rational r1, Rational r2) {
    int r1Num=r1.getNum();
    int r1Denom=r1.getDenom();
    int r2Num=r2.getNum();
    int r2Denom=r2.getDenom();
    //do all your plus stuff
    return new Rational(//new num, //new denom);
}

【讨论】:

  • 我将如何接受两个参数,我将采用哪种方法?
  • 为 plus 方法做。它看起来像上面那样。您必须在 Rational 类中创建那些 geNum() 和 getDenom 方法。将所有数学函数放在一个单独的类中会使您的代码更加简洁。
  • 谢谢你帮了我:)
【解决方案2】:

尝试以下代码作为测试类。

示例代码测试类

public class TestRational
{
  public static void main(String[] args)
  {
    Rational rational= new Rational(2,3);
    rational.plus(rational);
    System.out.print(rational.toString());
  }
}

输出将是

4 / 3

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 2010-09-19
    • 2018-04-20
    • 1970-01-01
    • 2015-02-18
    • 2016-07-20
    • 2012-10-21
    • 2021-06-26
    • 1970-01-01
    相关资源
    最近更新 更多