当前位置:首页 » 编程语言 » java复数类

java复数类

发布时间: 2022-07-28 09:29:45

❶ 用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算

public class ComplexDemo {
// main方法
public static void main(String[] a) {
Complex b = new Complex(2, 5);
Complex c = new Complex(3, -4);
System.out.println(b + "+" + c + "=" + b.add(c));
System.out.println(b + "-" + c + "=" + b.minus(c));
System.out.println(b + "*" + c + "=" + b.multiply(c));
System.out.println(b + "/" + c + "=" + b.divide(c));
}
}

// Complex类
class Complex {
private double m;// 实部
private double n;// 虚部

public Complex(double m, double n) {
this.m = m;
this.n = n;
}

// add
public Complex add(Complex c) {
return new Complex(m + c.m, n + c.n);
}

// minus
public Complex minus(Complex c) {
return new Complex(m - c.m, n - c.n);
}

// multiply
public Complex multiply(Complex c) {
return new Complex(m * c.m - n * c.n, m * c.n + n * c.m);
}

// divide
public Complex divide(Complex c) {
double d = Math.sqrt(c.m * c.m) + Math.sqrt(c.n * c.n);
return new Complex((m * c.m + n * c.n) / d, Math.round((m * c.n - n * c.m) / d));
}

public String toString() {
String rtr_str = "";
if (n > 0)
rtr_str = "(" + m + "+" + n + "i" + ")";
if (n == 0)
rtr_str = "(" + m + ")";
if (n < 0)
rtr_str = "(" + m + n + "i" + ")";
return rtr_str;
}
}

❷ JAVA:定义一个表示复数类的类

package com.test;

public class ComplexNum {
// Z = a + bi
private int Rez; // 实部
private int Imz; // 虚部

public int getRez() {
return Rez;
}

public void setRez(int rez) {
Rez = rez;
}

public int getImz() {
return Imz;
}

public void setImz(int imz) {
Imz = imz;
}

public ComplexNum(){}

// 构造函数
public ComplexNum(int rez, int imz) {
super();
Rez = rez;
Imz = imz;
}

// 加
public static void plus(ComplexNum a,ComplexNum b){
ComplexNum temp = new ComplexNum();
temp.setRez(a.getRez()+b.getRez());
temp.setImz(a.getImz()+b.getImz());
display(temp);
}

// 减
public static void minus(ComplexNum a,ComplexNum b){
ComplexNum temp = new ComplexNum();
temp.setRez(a.getRez()-b.getRez());
temp.setImz(a.getImz()-b.getImz());
display(temp);
}

// 显示
public static void display(ComplexNum a){
StringBuffer sb = new StringBuffer();
sb.append(a.getRez());
if(a.getImz()>0){
sb.append("+"+a.getImz()+"i");
}else if(a.getImz()<0){
sb.append(a.getImz()+"i");
}
System.out.println(sb.toString());
}

public static void main(String[] args) {
ComplexNum a = new ComplexNum(4, 3); //构造方法1
ComplexNum b = new ComplexNum(); // 构造方法2
b.setRez(5);
b.setImz(3);

plus(a, b); //加
minus(a, b); //减

display(a);//显示
}
}

❸ 使用JAVA编程实现复数类ComplexNumber

import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ComplexTest extends Applet implements ActionListener{

Label firstReal, firstImg;
TextField firstRealNum, firstImgNum;

Label secondReal, secondImg;
TextField secondRealNum, secondImgNum;

Button add = new Button("Add");
Button subtract = new Button("Subtract");

TextField addResult, subtractResult;

public void init(){

firstReal = new Label("First Complex Real Number: ");
firstRealNum = new TextField(7);
super.add(firstReal);
super.add(firstRealNum);

firstImg = new Label("First Complex Imaginary Number: ");
firstImgNum = new TextField(7);
super.add(firstImg);
super.add(firstImgNum);

secondReal = new Label("Second Complex Real Number: ");
secondRealNum = new TextField(7);
super.add(secondReal);
super.add(secondRealNum);

secondImg = new Label("Second Complex Imaginary Number: ");
secondImgNum = new TextField(7);
super.add(secondImg);
super.add(secondImgNum);

super.add(add);
addResult = new TextField(7);
super.add(addResult);

super.add(subtract);
subtractResult = new TextField(7);
super.add(subtractResult);

add.addActionListener(this);
subtract.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {

double firstComplxReal = Double.parseDouble(firstRealNum.getText());
double firstComplxImg = Double.parseDouble(firstImgNum.getText());

double secondComplxReal = Double.parseDouble(secondRealNum.getText());
double secondComplxImg = Double.parseDouble(secondImgNum.getText());

ComplexNumber complxNum1 = new ComplexNumber(firstComplxReal, firstComplxImg);
ComplexNumber complxNum2 = new ComplexNumber(secondComplxReal, secondComplxImg);

addResult.setText(complxNum1.add(complxNum2).toString());
subtractResult.setText(complxNum1.subtract(complxNum2).toString());

}
}

class ComplexNumber{

private double real;
private double imaginary;

public ComplexNumber(double realNum, double imaginaryNum){
this.real = realNum;
this.imaginary = imaginaryNum;
}

// (a+bi) + (c+di) = (a+b) + (c+d)i
public ComplexNumber add(ComplexNumber complexNum2){

double newRealPart = this.real + complexNum2.getReal();
double newImgPart = this.imaginary + complexNum2.getImaginary();

return new ComplexNumber(newRealPart, newImgPart);
}

//(a+bi) - (c+di) = (a-b) - (c-d)i
public ComplexNumber subtract(ComplexNumber complexNum2){
double newRealPart = this.real - complexNum2.getReal();
double newImgPart = this.imaginary - complexNum2.getImaginary();

return new ComplexNumber(newRealPart, newImgPart);
}

public double getImaginary() {
return imaginary;
}

public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}

public double getReal() {
return real;
}

public void setReal(double real) {
this.real = real;
}

public String toString(){
return real + "+" + imaginary + "i";
}

}

❹ java:实现复数类Complex 成员属性: real, imag 分别表示实部,虚部

‍public
class
Complex{
private
Double
real;//实部,类型为Double类
private
Double
imag;//虚部,类型为Double类
/*构造函数一(一般构造函数都是初始化一些成员,如
这里的real,imag)
*/
public
Complex(Double
a,Double
b){
real=a;
imag=b;}
/*构造函数二:根据已有复数创建对象,就是复制复数p
的两个成员值了
*/
public
Complex(Complex
p){
real=p.real;
imag=p.imag;}
/*构造函数三,空参数构造函数,调用它将会产生实例
为0的对象
*/
public
Complex(){
real=0.0;
imag=0.0;}
/*成员方法,实现加法。复数的相加或相减,其实是各实部与虚
部的相加减,
*/
public
Complex
add(Complex
oth){
//创一个新的Complex用来保存相加后得到的复数
Complex
plextem=new
Complex();
//实部相加
plextem.real=real+oth.real;
//虚部相加
plextem.imag=imag+oth.imag;
//返回已保存在plextem的相加后的复数
return
plextem;
}
/*成员方法
实现减法。同加法一样。
*/
public
Complex
cut(Complex
oth){
Complex
plextem=new
Complex();
plextem.real=real-oth.real;
plextem.imag=imag-oth.imag;
return
plextem;
}
/*用来显示或打印复数,也就是人们眼中的复数形式,为:
5.4+10.2i,4.2+(-1.2)i等
*/
public
void
print(){
System.out.println(real+"+"+imag+"i");
}
}

❺ java 设计一个复数类

不知道是不是 ~
//复数类。

public class Complex
{
private double real,im; //实部,虚部

public Complex(double real, double im) //构造方法
{
this.real = real;
this.im = im;
}

public Complex(double real) //构造方法重载
{
this(real,0);
}

public Complex()
{
this(0,0);
}

public Complex(Complex c) //拷贝构造方法
{
this(c.real,c.im);
}

public boolean equals(Complex c) //比较两个对象是否相等
{
return this.real==c.real && this.im==c.im;
}

public String toString()
{
return "("+this.real+"+"+this.im+"i)";
}

public void add(Complex c) //两个对象相加
{ //改变当前对象,没有返回新对象
this.real += c.real;
this.im += c.im;
}

public Complex plus(Complex c) //两个对象相加,与add()方法参数一样不能重载
{ //返回新创建对象,没有改变当前对象
return new Complex(this.real+c.real, this.im+c.im);
}

public void subtract(Complex c) //两个对象相减
{ //改变当前对象,没有返回新对象
this.real -= c.real;
this.im -= c.im;
}

public Complex minus(Complex c) //两个对象相减,与subtract()方法参数一样不能重载
{ //返回新创建的对象,没有改变当前对象
return new Complex(this.real-c.real, this.im-c.im);
}
}

class Complex__ex
{
public static void main(String args[])
{
Complex a = new Complex(1,2);
Complex b = new Complex(3,5);
Complex c = a.plus(b); //返回新创建对象
System.out.println(a+" + "+b+" = "+c);
}
}

/*
程序运行结果如下:
(1.0+2.0i) + (3.0+5.0i) = (40.0+7.0i)

*/

❻ 用java 编写一个复数类

public class Complex {

protected int a;

protected int b;

public Complex(int a, int b) {
this.a = a;
this.b = b;
}

public String toString() {
return this.a + " + " + this.b + "i";
}

public static Complex addition(Complex complex1, Complex complex2) {
int a = complex1.a + complex2.a;
int b = complex1.b + complex2.b;
return new Complex(a, b);
}

public static Complex subtract(Complex complex1, Complex complex2) {
int a = complex1.a - complex2.a;
int b = complex1.b - complex2.b;
return new Complex(a, b);
}

public static Complex multiplication(Complex complex1, Complex complex2) {
int a = complex1.a * complex2.a - complex1.b * complex2.b;
int b = complex1.b * complex2.a + complex1.a * complex2.b;
return new Complex(a, b);
}

public static Complex division(Complex complex1, Complex complex2) throws Exception {
if (complex2.a == 0) {
throw new Exception("complex2.a is 0");
}
if (complex2.b == 0) {
throw new Exception("complex2.b is 0");
}
int a = (complex1.a * complex2.a + complex1.b * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
int b = (complex1.b * complex2.a - complex1.a * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
return new Complex(a, b);
}
}

//测试用的类
public class ComplexTest {

public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Complex complex1 = new Complex(1, 2);
Complex complex2 = new Complex(3, 4);
System.out.println(complex1 + " + " + complex2 + " = " + Complex.addition(complex1, complex2));
System.out.println(complex1 + " - " + complex2 + " = " + Complex.subtract(complex1, complex2));
System.out.println(complex1 + " x " + complex2 + " = " + Complex.multiplication(complex1, complex2));
System.out.println(complex1 + " / " + complex2 + " = " + Complex.division(complex1, complex2));
}
}

❼ 用Java语言定义复数类Complex,必须满足如下要求:

public class Complex{
public int RealPart;
public int ImaginPart;
public Complex(){
this.RealPart = 0;
this.ImaginPart = 0;
}

public Complex(int r, int i){
this.RealPart = r;
this.ImaginPart = i
}

public Complex complexAdd(Complex a){
this.RealPart += a.RealPart;
this.ImaginPart += a.ImaginPart;
//这里返回什么?复数值是指哪个?还是复数对象?没有说清楚。我这里返回复数对象。
return this;
}

public String ToString(){
return ""+this.RealPart + this.ImaginPart;//return "" + 1 + 2 = "12";不知道这里要求是不是这样。有些话写的我没法理解。你的a + bi是个啥?如果是返回实数和虚数的和就给 1 + 2的部分加上括号。
}
}

❽ java 定义一个复数类

//余下的自己完成

import java.util.Scanner;

public class ComplexOperation {

static Scanner s = new Scanner(System.in);

public Complex option(Complex c1, Complex c2, String opch) {
Complex r = new Complex();
if("+".equals(opch)) {
r.setReaPart(c1.getReaPart() + c2.getReaPart());
r.setVirPart(c1.getVirPart() + c2.getVirPart());

} else if("-".equals(opch)) {
r.setReaPart(c1.getReaPart() - c2.getReaPart());
r.setVirPart(c1.getVirPart() - c2.getVirPart());
}
return r;
}

public Complex read(String info) {
System.out.println(info);
Complex c = new Complex();
System.out.print("实部: ");
c.setReaPart(s.nextInt());
System.out.print("虚部: ");
c.setVirPart(s.nextInt());
return c;
}

public static void main(String[] args) {

// ComplexOperation co = new ComplexOperation();
// Complex c1 = co.read("输入复数一");
// Complex c2 = co.read("输入复数二");
// System.out.print("输入运算符: ");
// String opch = s.next();
// System.out.print("结果是: " + co.option(c1, c2, opch));
// double d = 2.36;
// int len = 1;
// String format = "%" + len + ".2f";
// System.out.printf(format, d);

}

}
class Complex{
private int reaPart;
private int virPart;

public Complex() {
}
public Complex(int r, int v) {
this.reaPart = r;
this.virPart = v;

}

public String toString() {
int tag = this.getVirPart();
if(tag == 0) {
return getReaPart() + "";
} else if(tag > 0) {
return getReaPart() + "+" + getVirPart() + "i";
} else {
return getReaPart() + "-" + -getVirPart() + "i";
}

}
public int getReaPart() {
return reaPart;
}
public void setReaPart(int reaPart) {
this.reaPart = reaPart;
}
public int getVirPart() {
return virPart;
}
public void setVirPart(int virPart) {
this.virPart = virPart;
}

}

❾ 用JAVA定义个复数类

public class Complex {

private double x;//实部
private double y;//虚部

public Complex(){}

/**构造函数
* @param x 实数部分
* @param y 虚数部分
*/
public Complex(double x,double y){
super();
this.x = x;
this.y = y;
}

/**求模
* @return 该复数的模
*/
public double mod(){
return x * x + y * y;
}

/**复数间加法
* @param complex 加数
* @return 计算结果
*/
public Complex add(Complex complex){
double x = this.x + complex.x;
double y = this.y + complex.y;
return new Complex(x,y);
}

/**复数与实数的加法
* @param a 加数
* @return 计算结果
*/
public Complex add(double a){
return this.add(new Complex(a,0));
}

/**复数间减法
* @param complex 减数
* @return 计算结果
*/
public Complex subtract(Complex complex){
double x = this.x - complex.x;
double y = this.y - complex.y;
return new Complex(x,y);
}

/**复数与实数的减法
* @param a 减数
* @return 计算结果
*/
public Complex subtract(double a){
return subtract(new Complex(a,0));
}

/**复数间乘法
* @param complex 乘数
* @return 计算结果
*/
public Complex multiply(Complex complex){
double x = this.x * complex.x - this.y * complex.y;
double y = this.y * complex.x + this.x * complex.y;
return new Complex(x,y);
}

/**复数间除法
* @param complex 除数
* @return 计算结果
*/
public Complex divide(Complex complex){
double x = (this.x * complex.x + this.y * complex.y) / (complex.mod());
double y = (this.y * complex.x - this.x * complex.y) / (complex.mod());
return new Complex(x,y);
}

public String toString(){
StringBuffer sb = new StringBuffer();
if(x != 0){
sb.append(x);
if(y > 0){
sb.append("+" + y + "i");
}else if(y < 0){
sb.append(y + "i");
}
}else{
if(y != 0){
sb.append(y + "i");
}
}
if(x == 0 && y == 0){
return "0";
}
return sb.toString();
}

public double getX() {
return x;
}

public void setX(double x) {
this.x = x;
}

public double getY() {
return y;
}

public void setY(double y) {
this.y = y;
}

public static void main(String[] args) {
Complex a = new Complex(2,0.5);
Complex b = new Complex(0.5,2);
System.out.println("(" + a + ")+(" + b + ")=" + a.add(b));
System.out.println("(" + a + ")+" + 2 + "=" + a.add(2));
System.out.println("(" + a + ")-(" + b + ")=" + a.subtract(b));
System.out.println("(" + a + ")-" + 2 + "=" + a.subtract(2));
System.out.println("(" + a + ")*(" + b + ")=" + a.multiply(b));
System.out.println("(" + a + ")/(" + b + ")=" + a.divide(b));
}

}

❿ java:创建复数类并实现复数的基本运算

package main;

public class Complex {
private int a;
private int b;

public Complex() {
this.a = 0;
this.b = 0;
}

public Complex(int a, int b) {
this.a = a;
this.b = b;
}

public int getA() {
return a;
}

public void setA(int a) {
this.a = a;
}

public int getB() {
return b;
}

public void setB(int b) {
this.b = b;
}

@Override
public String toString() {
return "(" + a + "+" + b + "i)";
}
}

package main;

public class ComplexTest {
public static Complex add(Complex c1, Complex c2) {
return new Complex(c1.getA() + c2.getA(), c1.getB() + c2.getB());
}

public static Complex subtract(Complex c1, Complex c2) {
return new Complex(c1.getA() - c2.getA(), c1.getB() - c2.getB());
}

public static Complex multiply(Complex c1, Complex c2) {
return new Complex(c1.getA() * c2.getA() - c1.getB() * c2.getB(),
c1.getB() * c1.getA() + c1.getA() * c2.getB());
}

public static Complex division(Complex c1, Complex c2) {
int a = c1.getA();
int b = c1.getB();
int c = c2.getA();
int d = c2.getB();
return new Complex((a *c + b * d) / (c * c + d * d), (b * c - a * d) / (c * c + d * d));
}

public static void main(String []args) {
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex();
System.out.println(c1);
System.out.println(c2);

System.out.println(add(c1, c2));
}
}

热点内容
javascript设计模式源码 发布:2025-01-24 14:49:07 浏览:908
linqtosql查询 发布:2025-01-24 14:48:57 浏览:120
华为手机更换开机密码如何操作 发布:2025-01-24 14:43:15 浏览:699
快手等待上传 发布:2025-01-24 14:41:37 浏览:380
apache和php7 发布:2025-01-24 14:32:26 浏览:892
linuxio文件 发布:2025-01-24 13:40:21 浏览:438
在excel设密码如何取消 发布:2025-01-24 13:38:54 浏览:483
电脑装存储时不能开机 发布:2025-01-24 13:38:52 浏览:285
2000人同时在线的小程序需要什么服务器 发布:2025-01-24 13:37:17 浏览:853
怎么搭建linux服务器配置 发布:2025-01-24 13:37:16 浏览:113