找不到构造函数Rational(String)

时间:2018-07-26 18:10:15

标签: java

我的主要功能的第5行出现错误...该行显示:

System.out.println( "The fractional number is " + myRational.Rational(s) );

发生的错误告诉我

The method Rational(String) is undefined for the type Assignment13.Rational.

任何帮助或建议,将不胜感激。

import java.math.BigInteger;
import java.util.Scanner;

public class Assignment13 {

    public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
            System.out.println("Enter a decimal number: ");
            String s = input.next();
           Rational myRational = new Rational();
            System.out.println( "The fractional number is " + myRational.Rational(s) );

    }

 public class Rational extends Number implements Comparable<Rational> {


    // Data fields for numerator and denominator
    private BigInteger numerator = BigInteger.ZERO;
    private BigInteger denominator = BigInteger.ONE;

    /** Construct a rational with default properties */
    public Rational() {
      this(BigInteger.ZERO,BigInteger.ONE);
    }

    /** Construct a rational with specified numerator and denominator */
    public Rational(BigInteger numerator, BigInteger denominator) {
      BigInteger gcd = gcd(numerator, denominator);
      this.numerator= (denominator.compareTo(BigInteger.ZERO) > 0 ? BigInteger.ONE : 
         new BigInteger("-1")).multiply(numerator.divide(gcd));
      this.denominator = denominator.divide(gcd);
    }

    public Rational(String decimal) {

        int index = (decimal.contains(".")) ? decimal.indexOf('.') : decimal.indexOf('/');
        BigInteger d;
        BigInteger n;
        // if string is in decimal form
        if (decimal.contains(".")) {
            int power = decimal.substring(index + 1, decimal.length()).length();
            d = new BigInteger ("Math.pow(10,power)");
            n = new BigInteger(new StringBuilder(decimal).deleteCharAt(index).toString());
        } else {
            // if string contains '/'
            n = new BigInteger(decimal.substring(0, index));
            d = new BigInteger(decimal.substring (index + 1, decimal.length()));
        }

        BigInteger gcd = gcd(n, d);
        this.numerator = ((d.compareTo(BigInteger.ZERO) > 0) ? BigInteger.ONE : new BigInteger("-1")).multiply(n).divide(gcd);
        this.denominator = d.abs().divide(gcd);
        }

   /** Find GCD of two numbers */
   private  BigInteger gcd(BigInteger n, BigInteger d) {
       BigInteger n1 = n.abs();
       BigInteger n2 = d.abs();
       BigInteger gcd = BigInteger.valueOf(1);

     for (BigInteger k = BigInteger.valueOf(1); k.compareTo(n1) <= 0 && k.compareTo(n2) <= 0; k.add(BigInteger.ONE)) {
       if (n1.mod(k) == BigInteger.ZERO && n2.mod(k) == BigInteger.ZERO)
         gcd = k;
     }

     return gcd;
   }

   /** Return numerator */
   public BigInteger getNumerator() {
     return numerator;
   }

   /** Return denominator */
   public BigInteger getDenominator() {
     return denominator;
   }

   /** Add a rational number to this rational */

public Rational add(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator()).add(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
     return new Rational(n, d);
   }

   /** Subtract a rational number from this rational */

public Rational subtract(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator()).subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
     return new Rational(n, d);
   }

   /** Multiply a rational number by this rational */


public Rational multiply(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
     return new Rational(n, d);
   }

   /** Divide a rational number by this rational */


public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.numerator);
     return new Rational(n, d);
   }

   @Override
   public String toString() {
     if (denominator.compareTo(BigInteger.ONE) == 0)
       return numerator + "";
     else
       return numerator + "/" + denominator;
   }

   @Override // Override the equals method in the Object class
   public boolean equals(Object other) {
    if (((this.subtract((Rational)(other))).getNumerator()).compareTo(BigInteger.ZERO) == 0)
       return true;
     else
       return false;
   }

   @Override // Implement the abstract intValue method in Number
   public int intValue() {
     return (int)doubleValue();
   }

   @Override // Implement the abstract floatValue method in Number
   public float floatValue() {
     return (float)doubleValue();
   }

   public BigInteger bigIntegerValue() {
       return numerator.multiply(new BigInteger("1").divide(denominator));
   }

   @Override // Implement the doubleValue method in Number
   public double doubleValue() {
       return numerator.divide(denominator).doubleValue();
  }

  @Override // Implement the abstract longValue method in Number
  public long longValue() {
    return (long)doubleValue();
  }

  @Override // Implement the compareTo method in Comparable
  public int compareTo(Rational o) {
    if (this.subtract(o).getNumerator().compareTo(BigInteger.ZERO) > 0)
      return 1;
    else if (this.subtract(o).getNumerator().compareTo(BigInteger.ZERO) < 0)
      return -1;
    else
      return 0;
  }
}
    }

1 个答案:

答案 0 :(得分:0)

您将 $message = $html_mail; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; $headers .= 'To: '. $to . "\r\n"; $headers .= 'From: My website <'.$admin_mail.'>' . "\r\n"; $response = mail($to, $subject, $message, $headers); 传递给类String s的构造函数,并从有效的实例Assignment13.Rational中调用它。没有理由创建一个新对象,也没有理由再使用另一个对象。

自变量声明以来,

myRational已经是myRational。如果要从Rational打印新创建的对象,请执行以下操作:

String s

或者最好将类设为System.out.println("The fractional number is " + new Assignment13().new Rational(s)); ,然后可以通过以下方式进行访问:

  • static在同一班上
  • new Rational(s)),因为它们是new Assignment13.Rational(s)

在两种情况下,您都可以省略以下行:

public

我建议您覆盖方法Object::toString,该方法默认情况下在打印到控制台时。

相关问题