分数 class 分数 Driver 疑难解答
Fraction class troubleshooting for Fraction Driver
我在第一学期使用 Java 编程,我们刚刚开始接触 OOP,我 运行 遇到了我的代码的一个小问题(不足为奇)。需要大师们的帮助才能弄清楚缺少什么,我知道我已经很接近答案了。基本上我得到了一个 driver 分数 class ,我需要自己写 class 。 driver 中的所有内容都必须保持原样。
这是 driver:
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
这是我得到的分数 class。
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
你这样声明你的构造函数:
public void Fraction()
public void Fraction(int n, int d)
构造函数没有 return 类型。声明它们:
public Fraction()
public Fraction(int n, int d)
我在第一学期使用 Java 编程,我们刚刚开始接触 OOP,我 运行 遇到了我的代码的一个小问题(不足为奇)。需要大师们的帮助才能弄清楚缺少什么,我知道我已经很接近答案了。基本上我得到了一个 driver 分数 class ,我需要自己写 class 。 driver 中的所有内容都必须保持原样。
这是 driver:
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
这是我得到的分数 class。
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
你这样声明你的构造函数:
public void Fraction()
public void Fraction(int n, int d)
构造函数没有 return 类型。声明它们:
public Fraction()
public Fraction(int n, int d)