英文:
Complex numbers with using HashSet
问题
以下是翻译好的部分:
import java.lang.Math;
public class Complex {
double dReal, dImaginary;
// 构造方法
public Complex() {}
public Complex(double dReal, double dImaginary) {
this.dReal = dReal;
this.dImaginary = dImaginary;
}
// 将复数转换为字符串
public String toString() {
if (dImaginary >= 0)
return dReal + "+" + dImaginary + "i";
else
return dReal + "-" + -dImaginary + "i";
}
// ================================================================
// 复数运算
// 计算两个复数的和 cA + cB
public Complex Add(Complex cB) {
Complex sum = new Complex();
sum.dReal = dReal + cB.dReal;
sum.dImaginary = dImaginary + cB.dImaginary;
return (sum);
}
// 计算两个复数的差 cA - cB
public Complex Sub(Complex cB) {
Complex diff = new Complex();
diff.dReal = dReal - cB.dReal;
diff.dImaginary = dImaginary - cB.dImaginary;
return (diff);
}
// 计算两个复数的乘积 cA * cB
public Complex Mult(Complex cB) {
Complex prod = new Complex();
prod.dReal = dReal * cB.dReal - dImaginary * cB.dImaginary;
prod.dImaginary = dImaginary * cB.dReal + dReal * cB.dImaginary;
return (prod);
}
// 计算两个复数的除法 cA / cB
public Complex Div(Complex cB) {
Complex div = new Complex();
double dR, dDen;
if (Math.abs(cB.dReal) >= Math.abs(cB.dImaginary)) {
dR = cB.dImaginary / cB.dReal;
dDen = cB.dReal + dR * cB.dImaginary;
div.dReal = (dReal + dR * dImaginary) / dDen;
div.dImaginary = (dImaginary - dR * dReal) / dDen;
} else {
dR = cB.dReal / cB.dImaginary;
dDen = cB.dImaginary + dR * cB.dReal;
div.dReal = (dR * dReal + dImaginary) / dDen;
div.dImaginary = (dR * dImaginary - dReal) / dDen;
}
return (div);
}
// ================================================================
// 在 Complex 类中执行练习方法
public static void main(String args[]) {
// 设置并打印两个复数
Complex cA = new Complex(1.0, 2.0);
Complex cB = new Complex(3.0, 4.0);
System.out.println("cA = " + cA.toString());
System.out.println("cB = " + cB.toString());
// 输出
Complex cC = cA.Add(cB);
System.out.println("复数 cA + cB = " + cC.toString());
Complex cD = cA.Sub(cB);
System.out.println("复数 cA - cB = " + cD.toString());
Complex cE = cA.Mult(cB);
System.out.println("复数 cA * cB = " + cE.toString());
Complex cF = cA.Div(cB);
System.out.println("复数 cA / cB = " + cF.toString());
}
}
请注意,此处提供的翻译可能会因为代码格式问题而导致部分内容错位,但整体翻译是准确的。如果您需要准确的代码,请将上述翻译内容复制到代码编辑器中,然后进行适当的格式修复。
英文:
The university has given the task: To implement methods of adding, subtracting, multiplying and dividing objects. Create a set (Hashset) of dimension n from complex coordinates. Transfer it to a method that completes/multiplies its elements.
I realize the methods of operations themselves, but I cannot understand why hashset is needed and how to implement it in code. I read the articles, but I still didn’t understand how to apply the class for realization.
Sorry for my broken english
import java.lang.Math;
public class Complex {
double dReal, dImaginary;
// Constructor methods
public Complex() {}
public Complex( double dReal, double dImaginary ) {
this.dReal = dReal;
this.dImaginary = dImaginary;
}
// Convert complex number to a string
public String toString() {
if (dImaginary >= 0)
return dReal + "+" + dImaginary + "i";
else
return dReal + "-" + -dImaginary + "i";
}
// ================================================================
// Complex number arithmetic
// Compute sum of two complex numbers cA + cB
public Complex Add(Complex cB ) {
Complex sum = new Complex();
sum.dReal = dReal + cB.dReal;
sum.dImaginary = dImaginary + cB.dImaginary;
return (sum);
}
// Compute difference of two complex numbers cA - cB
public Complex Sub( Complex cB ) {
Complex diff = new Complex();
diff.dReal = dReal - cB.dReal;
diff.dImaginary = dImaginary - cB.dImaginary;
return (diff);
}
// Compute product of two complex numbers cA * cB
public Complex Mult( Complex cB ) {
Complex prod = new Complex();
prod.dReal = dReal*cB.dReal - dImaginary*cB.dImaginary;
prod.dImaginary = dImaginary*cB.dReal + dReal*cB.dImaginary;
return (prod);
}
// Compute divisor of two complex numbers cA / cB
public Complex Div( Complex cB ) {
Complex div = new Complex();
double dR, dDen;
if(Math.abs( cB.dReal ) >= Math.abs( cB.dImaginary )) {
dR = cB.dImaginary/cB.dReal;
dDen = cB.dReal + dR*cB.dImaginary;
div.dReal = (dReal + dR*dImaginary)/dDen;
div.dImaginary = (dImaginary - dR*dReal)/dDen;
} else {
dR = cB.dReal/cB.dImaginary;
dDen = cB.dImaginary + dR*cB.dReal;
div.dReal = (dR*dReal + dImaginary)/dDen;
div.dImaginary = (dR*dImaginary - dReal)/dDen;
}
return (div);
}
// ================================================================
// Exercise methods in Complex class
public static void main (String args[]) {
// Setup and print two complex numbers
Complex cA = new Complex( 1.0, 2.0 );
Complex cB = new Complex( 3.0, 4.0 );
System.out.println("cA = " + cA.toString() );
System.out.println("cB = " + cB.toString() );
// Output
Complex cC = cA.Add( cB );
System.out.println("Complex cA + cB = " + cC.toString() );
Complex cD = cA.Sub( cB );
System.out.println("Complex cA - cB = " + cD.toString() );
Complex cE = cA.Mult( cB );
System.out.println("Complex cA * cB = " + cE.toString() );
Complex cF = cA.Div( cB );
System.out.println("Complex cA / cB = " + cF.toString() );
}
}
答案1
得分: 1
创建一个由复数坐标组成的集合(哈希集)的维数为 n。
它只是用来存储一组 `n` 个复数。
将其传递给一个方法,该方法将完成/相乘其元素。
使用您拥有的 `Mult` 方法来相乘所有 `n` 个复数。
Set<Complex> complexNumbers = new HashSet<>();
//向其中添加数字
Optional<Complex> result = complexNumbers.stream()
.reduce((c1, c2) -> c1.Mult(c2)); //或者 .reduce(Complex::Mult)
注:Java 命名约定是以小写字母开头命名方法(`mult` 或 `multiply` 将是合适的命名)
英文:
> Create a set (Hashset) of dimension n from complex coordinates.
It is just to store a set of n
complex numbers.
> Transfer it to a method that completes/multiplies its elements.
Multiply all the n
complex numbers using the Mult
method you have.
Set<Complex> complexNumbers = new HashSet<>();
//add numbers to it
Optional<Complex> result = complexNumbers.stream()
.reduce((c1, c2) -> c1.Mult(c2)); //or .reduce(Complex::Mult)
Note: Java naming convention is to name methods start with lower case (mult
or multiply
would be fair names)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论