fraction类故障排除

yyyllmsg  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(453)

我在用java编程的第一学期,我们刚开始接触oop,我的代码遇到了一个小问题(毫不奇怪)。需要大师们的帮助,你才能找出缺了什么,我知道我已经很接近答案了。基本上我得到了一个分数类的分数驱动程序,我需要自己编写这个类。司机的一切都要保持原样。
这是司机:

import java.util.Scanner;

public class FractionDemo
{

public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
Fraction c, d, x;       // Fraction objects

System.out.println("Enter numerator; then denominator.");
c = new Fraction(stdIn.nextInt(), stdIn.nextInt());
c.print();

System.out.println("Enter numerator; then denominator.");
d = new Fraction(stdIn.nextInt(), stdIn.nextInt());
d.print();

x = new Fraction();     // create a fraction for number 0

System.out.println("Sum:");
x.add(c).add(d);
x.print();

x.printAsDouble();

x = new Fraction(1, 1); // create a fraction for number 1

System.out.println("Product:");
x.multiply(c).multiply(d);
x.print();

x.printAsDouble();

System.out.println("Enter numerator; then denominator.");
x = new Fraction(stdIn.nextInt(), stdIn.nextInt());
x.printAsDouble();

}
} // end main

这是我的分数课。

public class Fraction
{
private int numerator;  // numerator of the fraction
private int denominator;   // denominator of the fraction

//**************************************************

public void Fraction()
{
numerator = 0;
denominator = 1;
}
// default constructor

public void Fraction(int n, int d)
{
numerator = n;
denominator = d;
}
//two parameter constructor

public void print()
{
System.out.println(numerator + "/" + denominator);
} //print fraction as n/d

public void printAsDouble()
{
System.out.printf("%f", numerator/denominator);
} //print fraction as double

public Fraction add(Fraction other)
{
//add
// calling object = calling obj + parameter
int n = this.numerator * other.denominator +
this.denominator * other.numerator;
int d = this.denominator * other.denominator;
this.numerator = n;
this.denominator = d;
return this;
}  
//multiply
public Fraction multiply(Fraction other)
{
int n = this.numerator * other.numerator;
int d = this.denominator * other.denominator;
this.numerator = n;
this.denominator = d;
return this;
}

} // end Fraction class

这是我收到的错误消息:

FractionDemo.java:19: error: constructor Fraction in class Fraction cannot be applied to given types;
    c = new Fraction(stdIn.nextInt(), stdIn.nextInt());
        ^
  required: no arguments
  found: int,int
  reason: actual and formal argument lists differ in length

FractionDemo.java:23: error: constructor Fraction in class Fraction cannot be applied to given types;
    d = new Fraction(stdIn.nextInt(), stdIn.nextInt());
        ^
  required: no arguments
  found: int,int
  reason: actual and formal argument lists differ in length
FractionDemo.java:35: error: constructor Fraction in class Fraction cannot be applied to given types;
    x = new Fraction(1, 1); // create a fraction for number 1
        ^
  required: no arguments
  found: int,int
  reason: actual and formal argument lists differ in length

FractionDemo.java:44: error: constructor Fraction in class Fraction cannot be applied to given types;
    x = new Fraction(stdIn.nextInt(), stdIn.nextInt());
        ^
  required: no arguments
  found: int,int
  reason: actual and formal argument lists differ in length
4 errors

Tool completed with exit code 1
mzillmmw

mzillmmw1#

您这样声明构造函数:

public void Fraction()
public void Fraction(int n, int d)

构造函数没有返回类型。声明他们:

public Fraction()
public Fraction(int n, int d)

相关问题