我将重复我在java中必须做的事情,以我认为我需要思考的方式来完成这项任务。(对不起,我是编程新手)。
第一类;为复数定义类。我发现这相当简单,我的答案如下。
public class Complex {
private double real;
private double imaginary;
public Complex()
{
this( 0.0, 0.0 );
}
public Complex( double r, double i )
{
real = r;
imaginary = i;
}
}第二类;使用公共静态方法进行加法和减法,从第一类中调用实数和虚数。这一部分我发现更具挑战性,因为我没有100%掌握这一点。
Public class ComplexArith
public static ComplexAdd(Complex one, Complex two)
return Complex(one.getReal() + two.getReal(), one.getImaginary() + two.getImaginary());
public static ComplexSub(Complex one, Complex two)
return Complex(one.getReal() - two.getReal(), one.getImaginary - two.getImaginary());第三部分是要求用户输入,并对复数集进行加减。我对此并不熟悉,因为我从来没有要求用户输入(0.0,0.0)格式。
对整个代码有什么见解吗?我是在正确的轨道上吗?
编辑:
我第一次上完课的时候。感谢你们。
第二节课,我遇到了编译问题,因为我不能完全理解一些东西。
public class ComplexArith{
public static Complex add(Complex one, Complex two)
{
return Complex(one.getReal() + two.getReal(), one.getImaginary() + two.getImaginary());
}
public static Complex sub(Complex one, Complex two)
{
return Complex(one.getReal() - two.getReal(), one.getImaginary - two.getImaginary());
}
}我知道需要定义一和二,但我不知道如何定义它们。我该如何定义它们呢?我以为它是从双r,双i的Complex类中调用的。
我还认为在第一个类中也定义了.getImaginary。这是第一个类。
public class Complex
{
private double real;
private double imaginary;
public Complex()
{
this( 0.0, 0.0 );
}
public Complex( double r, double i )
{
real = r;
imaginary = i;
}
public double getReal() {
return this.real;
}
public double getImaginary() {
return this.imaginary;
}
}发布于 2012-04-11 10:16:57
这是正确的,但是您需要在Complex对象上使用getters。
例如:
public class Complex
{
private double real;
private double imaginary;
public Complex()
{
this( 0.0, 0.0 );
}
public Complex( double r, double i )
{
real = r;
imaginary = i;
}
public double getReal() {
return this.real;
}
public double getImaginary() {
return this.imaginary;
}
}您的方法还需要返回类型:
public class ComplexArith
{
public static Complex complexAdd(Complex one, Complex two) {
return Complex(one.getReal() + two.getReal(),
one.getImaginary() + two.getImaginary());
}
public static Complex complexSub(Complex one, Complex two) {
return Complex(one.getReal() - two.getReal(),
one.getImaginary - two.getImaginary());
}
}此外,这与您的程序的功能无关,但习惯上是让您的方法使用camelCase。因此,您的方法应该如下所示:
public static Complex complexAdd(Complex one, Complex two) {
return Complex(one.getReal() + two.getReal(),
one.getImaginary() + two.getImaginary());
}发布于 2012-04-11 10:18:18
就我个人而言,我会把这些算术运算放在复杂的类中。这些都是真正的复数运算,所以我不会把它们封装在复杂类之外。
我会考虑让复数变得不可变。这样就是线程安全的。
我喜欢静态的add,sub,mul,div方法。确保它们返回一个Complex (现在不返回)。其他方法,如余弦、正弦等,可能属于复杂包中的Math类。有关实数的示例,请参阅java.lang.Math。
您需要返回"new Complex“。你写的代码不能编译。
发布于 2013-09-27 07:02:54
这是我的实现,我在一个类上做了所有的事情:
package name.puzio.math;
public final class ComplexNumber {
private final double imaginary;
private final double real;
@Override
public final boolean equals(Object object) {
if (!(object instanceof ComplexNumber))
return false;
ComplexNumber a = (ComplexNumber) object;
return (real == a.real) && (imaginary == a.imaginary);
}
public ComplexNumber(double real, double imaginary) {
this.imaginary = imaginary;
this.real = real;
}
public static final ComplexNumber createPolar(double amount, double angel) {
return new ComplexNumber(amount * Math.cos(angel), amount * Math.sin(angel));
}
public final double getImaginary() {
return imaginary;
}
public final double getReal() {
return real;
}
public final double getAmount() {
return Math.sqrt((real * real) + (imaginary * imaginary));
}
public final double getAngle() {
return Math.atan2(imaginary, real);
}
public final ComplexNumber add(ComplexNumber b) {
return add(this, b);
}
public final ComplexNumber sub(ComplexNumber b) {
return sub(this, b);
}
public final ComplexNumber div(ComplexNumber b) {
return div(this, b);
}
public final ComplexNumber mul(ComplexNumber b) {
return mul(this, b);
}
public final ComplexNumber conjugation() {
return conjugation(this);
}
/**
* Addition:
* @param a
* @param b
* @return
*/
private final static ComplexNumber add(ComplexNumber a, ComplexNumber b) {
return new ComplexNumber(a.real + b.real, a.imaginary + b.imaginary);
}
/**
* Subtraktion:
* @param a
* @param b
* @return
*/
private final static ComplexNumber sub(ComplexNumber a, ComplexNumber b) {
return new ComplexNumber(a.real - b.real, a.imaginary - b.imaginary);
}
/**
* Multiplikation:
* @param a
* @param b
* @return
**/
private final static ComplexNumber mul(ComplexNumber a, ComplexNumber b) {
return new ComplexNumber((a.real * b.real) - (a.imaginary * b.imaginary), (a.imaginary * b.real) + (a.real * b.imaginary));
}
/**
* Division:
* @param a
* @param b
* @return
**/
private final static ComplexNumber div(ComplexNumber a, ComplexNumber b) {
double d = (b.real * b.real) + (b.imaginary * b.imaginary);
if (d == 0)
return new ComplexNumber(Double.NaN, Double.NaN);
return new ComplexNumber(((a.real * b.real) + (a.imaginary * b.imaginary)) / d, ((a.imaginary * b.real) - (a.real * b.imaginary)) / d);
}
/**
* Konjugation:
* @param a
* @return
**/
private final static ComplexNumber conjugation(ComplexNumber a) {
return new ComplexNumber(a.real, -a.imaginary);
}
}https://stackoverflow.com/questions/10098958
复制相似问题