java定义复数类代码 编写复数类
Java创建一个复数类
package table;
创新互联-专业网站定制、快速模板网站建设、高性价比左云网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式左云网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖左云地区。费用合理售后完善,十多年实体公司更值得信赖。
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,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 定义一个复数类
//余下的自己完成
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编写一个复数类,包含实部和虚部属性,还有复数相加、相减以及复数的求模、输出复数字符串”a+bi”
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==0x.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(image0)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();
}
}
用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 编写一个复数类
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定义复数类代码 编写复数类
URL网址:http://scyanting.com/article/doogdgo.html