...包含实部和虚部属性,还有复数相加、相减以及复数的求模、输出复数...
发布网友
发布时间:2024-10-11 09:45
我来回答
共3个回答
热心网友
时间:2024-11-17 18:50
import java.util.*;
public class ComplexTest{
static class ComplexNumber{
private double real,image;
public ComplexNumber(){
this(0.0,0.0);}
public ComplexNumber(double a,double b){
real=a;image=b;
}
public ComplexNumber add(ComplexNumber x){
return new ComplexNumber(real+x.real,image+x.image);}
public ComplexNumber sub(ComplexNumber x){
return new ComplexNumber(real-x.real,image-x.image); }
public ComplexNumber mul(ComplexNumber x){
return new ComplexNumber(real*x.real-image*x.image,real*x.image+image*x.real);}
public ComplexNumber div(ComplexNumber x){
if(x.real==0&&x.image==0){
System.out.println("无法进行除法!");
return new ComplexNumber();}
else return new ComplexNumber((real*x.real+image*x.image)/(x.real*x.real+x.image*x.image)
,(image*x.real-real*x.image)/(x.real*x.real+x.image*x.image));}
public double getReal (){return real;}
public double getImage (){return image;}
public void show(){System.out.println(this.toString());}
public String toString(){
if(image<0)return ""+real+image+"i";
else return ""+real+"+"+image+"i";
}
}
static class Test{
public Test(){
Scanner sr=new Scanner(System.in);
ComplexNumber a,b,c;
try{System.out.println("请输入第一个实部和虚部:");
a=new ComplexNumber(sr.nextDouble(),sr.nextDouble());
System.out.println("请输入第二个实部和虚部:");
b=new ComplexNumber(sr.nextDouble(),sr.nextDouble());
System.out.print("第一个复数:");a.show();
System.out.print("第二个复数:");b.show();
c=a.add(b);
System.out.print("两个复数的和:");c.show();
c=a.sub(b);
System.out.print("两个复数的差:");c.show();
c=a.mul(b);
System.out.print("两个复数的积:");c.show();
c=a.div(b);
System.out.print("两个复数的商:");c.show();
}
catch(InputMismatchException e){
System.out.println("输入有误!");}
}
}
public static void main(String[] args){
new Test();
}
}
热心网友
时间:2024-11-17 18:49
public class Complex {
/** The real part of this complex number */
public final double real;
/** The imaginary part of this complex number */
public final double imag;
/**
* @param real
* @param imag
*/
public Complex(double real, double imag) {
super();
this.real = real;
this.imag = imag;
}
/**
* Returns a Complex whose value is (this + val).
*
* @param val
* - value to be added to this Complex.
* @return
*/
public Complex add(Complex val) {
return new Complex(this.real + val.real, this.imag + val.imag);
}
/**
* Returns a Complex whose value is (this - val).
*
* @param val
* - value to be subtracted from this Complex.
* @return
*/
public Complex subtract(Complex val) {
return new Complex(this.real - val.real, this.imag - val.imag);
}
/**
* Returns a Complex whose value is (this * val).
*
* @param val
* - value to be multiplied by this Complex.
* @return this * val
*/
public Complex multiply(Complex val) {
return new Complex(this.real * val.real - this.imag * val.imag,
this.real * val.imag + this.imag * val.real);
}
/**
* Return the conjugate complex(x - y<i><b>i</b></i>) number of a complex
* number (x + y<i><b>i</b></i>).
*
* @return (x - y<i><b>i</b></i>)
*/
public Complex conjugate() {
return new Complex(real, -imag);
}
/**
* Return the magnitude of this complex number.
*
* @return √(real²+imag²) that is (real²+imag²)^0.5
*/
public double magnitude() {
return Math.sqrt(real * real + imag * imag);
}
/**
* Return the string value of a complex. For example, the string value of a
* complex number whose real part is 4 and imaginary part is 7 will be "4+7i"(excluding quotation marks) .
*
* @see java.lang.Object#toString()
* @return x + y<i><b>i</b></i>
*/
@Override
public String toString() {
return real + "+" + imag + "i";
}
}
类实现了如下功能:
求和、差、积、共轭复数、复数的模(大小)、字符串表示。
热心网友
时间:2024-11-17 18:51
package com.lcw.baiduknow;
public class ComplexTest {
/**
* @param args
*/
public static void main(String[] args) {
ComplexTest complex1=new ComplexTest(3, 4);
ComplexTest complex2=new ComplexTest(5, 8);
System.out.println(complex1.mod());
complex1.addComp(complex2);
complex1.outputComp();
complex1.minComp(complex2);
complex1.outputComp();
}
private double realPart;//实部
private double virtualPart;//虚部
public ComplexTest(double realPart,double virtualPart ) {
this.realPart=realPart;
this.virtualPart=virtualPart;
}
//两复数相加
public ComplexTest addComp(ComplexTest c2)
{
this.realPart+=c2.realPart;
this.virtualPart+=c2.virtualPart;
return this;
}
//两复数相减
public ComplexTest minComp(ComplexTest c2)
{
this.realPart-=c2.realPart;
this.virtualPart-=c2.realPart;
return this;
}
//求复数的模
public double mod()
{
return Math.sqrt(this.realPart*this.realPart+this.virtualPart*this.virtualPart);
}
//输出
public void outputComp()
{
System.out.println(this.realPart+"+"+this.virtualPart+"i");
}
}