12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

✞/ / Komplex : Cartesian / polar complex via interface// ’type = 0 ’ −> polar representation , else rectangularpublic class Komplex implements KomplexInterface {public double mod, theta , re , im;public Komplex ( ) { // Default constructormod = 0; theta = 0; re = 0; im = 0; }public Komplex ( double x, double y, int type ) { // Constructorif ( type == 0) {mod = x ; theta = y; } else {re = x; im = y; } }public double getRe () { return mod∗Math . cos ( theta ) ; }public double getIm () { return mod∗Math . sin ( theta ) ; }public double setRe () { re = mod∗Math . cos ( theta ) ; return re ; }public double setIm () { im = mod∗Math . sin ( theta ) ; return im ; }public void add ( Komplex other , int type ) {double tempMod = 0 . ;if ( type == 0) {tempMod = Math . sqrt ( this . mod∗ this .mod + other .mod∗other .mod+ 2∗ this . mod∗other .mod∗Math . cos ( this . theta−other . theta ) ) ;this . theta = Math . atan2 ( this . mod∗Math . sin ( this . theta )+ other .mod∗Math . sin ( other . theta ) , this . mod∗Math . cos ( this . theta )+ other .mod∗Math . cos ( other . theta ) ) ;this . mod = tempMod ;} else { this .re = this . re + other . re ; this .im = this .im + other .im; }}public void sub ( Komplex other , int type ) {if ( type == 0) {this .mod = Math . sqrt ( this . mod∗ this .mod + other .mod∗other .mod −2∗ this . mod∗other .mod∗(Math . cos ( this . theta )∗Math . cos ( other . theta )+ Math . sin ( this . theta )∗Math . sin ( other . theta ) ) ) ;this . theta = Math . atan (( this . mod∗Math . sin ( this . theta )−other .mod∗Math . sin ( other . theta ) )/( this . mod∗Math . cos ( this . theta )−other .mod∗Math . cos ( other . theta ) ) ) ;} else { this .re = this .re−other . re ; this .im = this .im−other .im; }}public void div ( Komplex other , int type ) {if ( type == 0) { this . mod = this .mod/other .mod;this . theta = this . theta−other . theta ;} else { this .re = (this .re∗other . re + this .im∗other .im)/( other . re∗other . re + other .im∗other .im) ;this .im = ( this .im∗other . re−this .re∗other .im)/( other . re∗other . re + other .im∗other .im ) ;}}☎246810121416182022242628303234363840424446485052✝}public void mult (Komplex other , int type ) {if ( type == 0) {this . mod = this . mod∗other .mod;this . theta = this . theta + other . theta ;} else {Komplex ans = new Komplex ( ) ;ans . re = this .re∗other . re−this .im∗other .im;ans .im = this .re∗other .im + this .im∗other . re ;this .re = ans.re;this .im = ans.im;}}public void conj ( int type ) {if ( type == 0) { this . mod = this . mod ; this . theta = −this . theta ; }else { this .re = this .re; this .im = −this .im; } }545658606264666870Listing 4.11 Komplex.java manipulates complex numbers using the interfaceKomplexInterface in Listing 4.10.−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 103

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!