Encapsulation, inheritance, and polymorphism of Java's------class (III)

Source: Internet
Author: User

issue: declare plural classes, member variables include real and imaginary parts, and member methods include implementing operations that are constructed of complex numbers by strings, complex addition, subtraction, string descriptions, comparison equality, and so on.

Declare the plural class as follows

public class Complex {public    double real,imag;                       Real, virtual part public    Complex (double real, double imag)        //Construction method public    Complex (double real)                   //constructor method overload    Public Complex ()                            //Constructor public    Complex (string s)               //Construction method, copy object from string construction public    Complex (Complex c)            Copy construction method, copy object public string    toString ()                 //Object description, Public    double Getmodel ()               //return modulus    of complex object public void Add (Complex c)             //Two objects Add, change current object public    static Complex Add (Complex C1, Complex C2)    // Returns the object after the addition of two objects, overloads public    void Subtract (Complex c)                     //Two object subtraction, changing the current object public    static Complex subtract ( Complex C1, Complex C2)  //Returns the object after subtracting two objects, overloads public    Boolean equals (Object obj)                    //Compares two objects for equality}

Code implementation:

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 the 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 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);}}


Encapsulation, inheritance, and polymorphism of Java's------class (III)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.