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.

104 chapter 4✞/ / KomplexTest : test KomplexInterfacepublic class KomplexTest {☎2✝}public static void main ( String [] argv ) {Komplex a , e ;e = new Komplex ( ) ;a = new Komplex ( 1 . , 1 . , 1) ;Komplex b = new Komplex ( 1 . , 2 . , 1) ;System . out . println ("Cartesian : Re a = " + a. re + ", Im a = " + a.im + "" );System . out . println ("Cartesian : Re b = " + b. re + ", Im b = " + b.im + "" );b . add ( a , 1) ;e = b;System . out . println ("Cartesian : e=b + a=" + e. re + " " + e.im + "" );/ / Polar Version , uses get and set methodsa = new Komplex (Math . sqrt (2.) , Math . PI /4. , 0) ; // Polar via 0b = new Komplex (Math . sqrt (5.) , Math . atan2 (2. , 1.) , 0) ;System . out . println ("Polar : Re a = " + a . getRe () + ", Im a = " + a . getIm () + "" );System . out . println ("Polar : Re b = " + b. getRe () + ", Im b = " + b . getIm () + "" );b . add ( a , 0) ;e = b;System . out . println ("Polar e=b + a = " + e . getRe () + " " + e . getIm () + "" );}46810121416182022Listing 4.12 KomplexTest.java tests Komplex and KomplexInterface. All three classes mustbe compiled with the same javac command.You should observe how KomplexInterface requires us to have methods forgetting and setting the real and imaginary parts of Komplex objects, as well asadding, subtracting, multiplying, dividing, and conjugating complex objects. (Inthe comments we see the suggestion that there should also be methods for gettingand setting the modulus and phase.)The class Komplex contains the constructors for Komplex objects. This differsfrom our previous implementation Complex by having the additional integer variabletype. Iftype = 0, then the complex numbers are in polar representation, elsethey are in Cartesian representation. So, for example, the method for arithmetic,such as the add method on line 22, is actually two different methods dependingupon the value of type. In contrast, the get and set methods for real and imaginaryparts are needed only for the polar representation, and so the value of type is notneeded.4.9.12 Polymorphism,Variable MultitypingPolymorphism allows a variable name declared as one type to contain other typesas needed while the program runs. The idea may be applied to both the class andthe interface. Class polymorphism allows a variable that is declared as one type tocontain types it inherits. To illustrate, if we declare myBaton of type Baton,Baton myBaton;then it will be valid to assign an object of type Baton to that variable, which iswhat we have been doing all along. However, it is also permissible to assign a−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 104

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

Saved successfully!

Ooh no, something went wrong!