「定义复数类Java」定义复数类complex
今天给各位分享定义复数类Java的知识,其中也会对定义复数类complex进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java 定义一个复数类
- 2、用JAVA定义个复数类
- 3、Java创建一个复数类
- 4、定义和实现复数的java类
- 5、用Java语言定义复数类Complex,必须满足如下要求:
- 6、JAVA:定义一个表示复数类的类
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 table;
public class Complex
{
double real;
double imaginary;
public static final Complex ZERO = new Complex (0, 0);
public static final Complex ONE = new Complex (1, 0);
public static final Complex I = new Complex (0, 1);
public Complex ( double real, double imaginary )
{
this.real = real;
this.imaginary = imaginary;
}
public double magnitude ()
{
return Math.sqrt (this.real * this.real + this.imaginary * this.imaginary);
}
public Complex negative ()
{
return new Complex (-real, -imaginary);
}
public double valueOf ()
{
return this.real;
}
public Complex add ( Complex a, Complex b )
{
return new Complex (a.real + b.real, a.imaginary + b.imaginary);
}
public Complex subtract ( Complex a, Complex b )
{
return new Complex (a.real - b.real, a.imaginary - b.imaginary);
}
public Complex multiply ( Complex a, Complex b )
{
return new Complex (a.real * b.real - a.imaginary * b.imaginary, a.real * b.imaginary + a.imaginary * b.real);
}
@Override
public String toString ()
{
StringBuilder builder = new StringBuilder ();
builder.append ("Complex [real=").append (real).append (", imaginary=").append (imaginary).append ("]");
return builder.toString ();
}
}
定义和实现复数的java类
public class Complex {
private double real, imag;// 定义实部和虚部
public Complex() {
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImag() {
return imag;
}
public void setImag(double imag) {
this.imag = imag;
}
public String toString() {
if (real == 0 imag == 0) {
return "0";
}
StringBuffer buf = new StringBuffer();
if (real != 0) {
buf.append(this.real);
if (imag 0) {
buf.append("+").append(imag).append("i");
} else if (imag 0) {
buf.append(imag).append("i");
}
} else {
buf.append(imag).append("i");
}
return buf.toString();
}
public Complex add(Complex a) {
return new Complex(real + a.real, imag + a.imag);
}
public static void main(String[] args) {
Complex c1 = new Complex(3.2, -4.7);
Complex c2 = new Complex(-3.9, 94.3);
Complex c3 = c1.add(c2);
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
}
用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:定义一个表示复数类的类
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的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于定义复数类complex、定义复数类Java的信息别忘了在本站进行查找喔。