码迷,mamicode.com
首页 > 编程语言 > 详细

java之 ------ 类的封装、继承和多态(三)

时间:2015-05-02 13:57:27      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

问题:声明复数类,成员变量包括实部和虚部,成员方法包括实现由字符串构造复数、复数加法、减法、字符串描述、比较相等等操作。

声明复数类如下

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

    public Complex(double real, double imag)        //构造方法
    public Complex(double real)                   //构造方法重载
    public Complex()                            //构造方法
    public Complex(String s)               //构造方法,由字符串构造复制对象
    public Complex(Complex c)            //拷贝构造方法,复制对象
    public String toString()                 //对象的字符串描述,
    public double getModel()               //返回复数对象的模
    public void add(Complex c)             //两个对象相加,改变当前对象
    public static Complex add(Complex c1, Complex c2)    //返回两个对象相加后的对象,重载
    public void subtract(Complex c)                     //两个对象相减,改变当前对象
    public static Complex subtract(Complex c1, Complex c2)  //返回两个对象相减后的对象,重载
    public boolean equals(Object obj)                    //比较两个对象是否相等
}

代码实现:

import java.util.*;
class ComplexNumber{
	int really,virtual;
	public ComplexNumber(int a,int b){
		this.set(a,b);
	}
	public ComplexNumber(){
	}
	public ComplexNumber(String str){
		this.set(str);
	}
	public void set(String str){
		if(str.length()==0){
			return ;
		}
		boolean isVirtualNegat=false;
		boolean isReallyNegat=false;
		int[] a=new int[2];
		int k=0;
		for(int i=0;i<str.length();i++){
			if(str.charAt(i)=='i'){
				break;
			}
			if(str.charAt(i)=='+'||str.charAt(i)=='-'){
				if(i!=0){
					k++;
				}
				if(str.charAt(i)=='-'){
					if(i==0){
						isReallyNegat=true;
					}else{
						isVirtualNegat=true;
					}
				}
				continue;
			}
			a[k]=a[k]*10+str.charAt(i)-'0';
		}
		if(isVirtualNegat){
			a[1]*=-1;
		}
		if(isReallyNegat){
			a[0]*=-1;
		}
		this.set(a[0],a[1]);
	}
	public void set(int a,int b){
		this.really=a;
		this.virtual=b;
	}
	public boolean equals(ComplexNumber x){
		if(x.really==this.really&&x.virtual==this.virtual){
			return true ;
		}
		return false;
	}
	public ComplexNumber add(ComplexNumber x){
		ComplexNumber sum=new ComplexNumber();
		sum.really=this.really+x.really;
		sum.virtual=this.virtual+x.virtual;
		return sum;
	}
	public ComplexNumber minus(ComplexNumber x){
		ComplexNumber sum=new ComplexNumber();
		sum.really=this.really-x.really;
		sum.virtual=this.virtual-x.virtual;
		return sum;
	}
	public int  compleTo(ComplexNumber x){
		if(this.really>x.really){
			return 1;
		}else if(this.really==x.really){
			if(this.virtual>x.virtual){
				return 1;
			}else if(this.virtual==x.virtual){
				return 0;
			}else{
				return -1;
			}
		}else{
			return -1;
		}
	}
	public String toString(){
		if(this.virtual<0){
			if(this.really==0){
				return this.virtual+"i";
			}
			return this.really+"-"+-1*this.virtual+"i";
		}else if(this.virtual==0){
			return this.really+"";
		}else{
			if(this.really==0){
				return this.virtual+"i";
			}
			return this.really+"+"+this.virtual+"i";
		}
	}
}
class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		System.out.println("Please input two number`s really part and virtual part:");
		int reall1=sc.nextInt(),virtual1=sc.nextInt();
		int reall2=sc.nextInt(),virtual2=sc.nextInt();
		ComplexNumber a=new ComplexNumber(reall1,virtual1);
		ComplexNumber b=new ComplexNumber(reall2,virtual2);
		//System.out.println(a+" "+b);
		System.out.println("("+a+")"+" + ("+b+") = ("+a.add(b)+")");
		System.out.println("("+a+")"+" - ("+b+") = ("+a.minus(b)+")");
		if(a.compleTo(b)==0){
			System.out.println(a+" = "+b);
		}else if(a.compleTo(b)==1){
			System.out.println(a+" > "+b);
		}else{
			System.out.println(a+" < "+b);
		}
		System.out.println("-------------------");
		
		System.out.println("Please input two complex-number : ");
		String x=sc.next();
		String y=sc.next();
		ComplexNumber c=new ComplexNumber(x);
		ComplexNumber d=new ComplexNumber(y);
		System.out.println("("+c+")"+" + ("+d+") = ("+c.add(d)+")");
		System.out.println("("+c+")"+" - ("+d+") = ("+c.minus(d)+")");
		if(c.compleTo(d)==0){
			System.out.println(c+" = "+d);
		}else if(c.compleTo(d)==1){
			System.out.println(c+" > "+d);
		}else{
			System.out.println(c+" < "+d);
		}
	}
}


java之 ------ 类的封装、继承和多态(三)

标签:

原文地址:http://blog.csdn.net/u011479875/article/details/45438771

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!