我在第一个学期使用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
这是我的Fraction类。
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
答案 0 :(得分:0)
你声明你的构造函数是这样的:
public void Fraction()
public void Fraction(int n, int d)
构造函数没有返回类型。声明他们:
public Fraction()
public Fraction(int n, int d)