當前位置:首頁 » 編程語言 » 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));
}
}

熱點內容
cvr網路存儲 發布:2025-01-24 17:24:52 瀏覽:415
腿套壓縮襪 發布:2025-01-24 17:05:16 瀏覽:458
電腦如何將安卓軟體卸載干凈 發布:2025-01-24 17:03:06 瀏覽:489
hello密碼怎麼破解 發布:2025-01-24 17:03:06 瀏覽:73
pspfifa無緩存 發布:2025-01-24 16:45:13 瀏覽:165
androidhandler機制 發布:2025-01-24 16:41:10 瀏覽:936
安卓系統如何下載aov 發布:2025-01-24 16:29:53 瀏覽:573
iptables允許ip訪問 發布:2025-01-24 16:19:58 瀏覽:932
安卓80如何識別存儲卡許可權 發布:2025-01-24 16:19:54 瀏覽:232
存儲介質價格 發布:2025-01-24 16:19:18 瀏覽:151