英文:
Failing to call a method in test class
问题
这是你提供的代码的翻译:
我试图创建一个处理复数的复数类。在创建了表示两个复数乘积的mult方法之后,它无法运行。在下面的代码中,当我将s定义为:s=mult(u,v)时,Eclipse显示错误,我不明白为什么。有人可以帮助吗?
package gestion.complexe;
public class Complexe {
double reelle, imag;
public Complexe(double reelle, double imag) {
this.reelle = reelle;
this.imag = imag;
}
public Complexe() {
this(Math.random() * (2 - 2), Math.random() * (2 - 2));
}
public String toString() {
String s = reelle + " + " + imag + "i";
return s;
}
// 两个复数的和
static Complexe somme(Complexe c1, Complexe c2) {
return new Complexe(c1.reelle + c2.reelle, c1.imag + c2.imag);
}
// 两个复数的乘积
static Complexe mult(Complexe u, Complexe v) {
return new Complexe(u.reelle * v.reelle - u.imag * v.imag, u.reelle * v.imag + u.imag * v.reelle);
}
public boolean estReel(Complexe z) {
return z.imag == 0;
}
// 两个复数的模
public double module() {
return Math.sqrt(this.reelle * this.reelle + this.imag * this.imag);
}
}
这是测试类
package gestion.complexe;
// 测试类
public class Test {
public static void main(String[] args) {
Complexe u = new Complexe(1.0, 1.0);
Complexe v = new Complexe(3.0, 4.0);
Complexe r = new Complexe(1.0, 0.0);
// 问题出在下一行,Eclipse显示错误
Complexe s = mult(u, v);
System.out.println(s.toString());
}
}
感谢帮助。
英文:
I was trying to make a complex class for complex numbers. After creating the mult method giving the product of two complex numbers it fails to run. In this code below, when i define s as: s=mult(u,v), eclipse point an error and I don't understand why. Can anyone give a help?
package gestion.complexe;
public class Complexe {
double reelle,imag;
public Complexe(double reelle,double imag) {
this.reelle=reelle;
this.imag=imag;
}
public Complexe() {
this(Math.random( )*(2-2),Math.random( )*(2-2));
}
public String toString() {
String s=reelle+" + "+imag+"i";
return s;
}
// Sum of two complex
static Complexe somme(Complexe c1,Complexe c2) {
return new Complexe(c1.reelle+c2.reelle,c1.imag+c2.imag);
}
// Product of two complex numbers
static Complexe mult(Complexe u,Complexe v ) {
return new Complexe(u.reelle*v.reelle-
u.imag*v.imag,u.reelle*v.imag+u.imag*v.reelle);
}
public boolean estReel(Complexe z) {
return z.imag==0;
}
// magnitude of two complex numbers
public double module () {
return Math.sqrt(this.reelle*this.reelle + this.imag*this.imag);
}
}
here is the class test
package gestion.complexe;
// Test Class
public class Test {
public static void main(String[] args) {
Complexe u=new Complexe(1.0,1.0);
Complexe v=new Complexe(3.0,4.0);
Complexe r=new Complexe(1.0,0.0);
// The problem is the next line, eclipse point error
Complexe s=mult(u,v);
System.out.println(s.toString());
}
}
thanks for helping.
答案1
得分: 1
mult
是Complexe
类的静态函数。要使用它,您必须像这样使用该类作为前缀:
Complexe s = Complexe.mult(u, v);
英文:
mult
is a static function of the Complexe
class. To use it, you have to use the class as a prefix like this:
Complexe s = Complexe.mult(u, v);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论