1 / 32

Example: complex number

Example: complex number. public class ComplexNumber { private double real; private double img; // override default constructor public ComplexNumber () { real = 0.0; img = 0.0; } // a more meaningful constructor

Download Presentation

Example: complex number

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Example: complex number public class ComplexNumber { private double real; private double img; // override default constructor public ComplexNumber () { real = 0.0; img = 0.0; } // a more meaningful constructor public ComplexNumber (double x, double y) { real = x; img = y; }

  2. Example: complex number // compute magnitude public double Magnitude () { return (Math.sqrt(real*real + img*img)); } // compute argument public double Argument () { return (Math.atan (img/real)); } // add a complex number to it public void Add (ComplexNumber c) { real += c.GetReal(); img += c.GetImg(); }

  3. Example: complex number // add the Get/Set methods public double GetReal () { return real; } public double GetImg () { return img; } public void SetReal (double x) { real = x; } public void SetImg (double x) { img = x; } } // end class

  4. Example: complex number class ComplexNumberTester { public static void main (String a[]) { int n = 10, i; // construct 1+i ComplexNumber c1 = new ComplexNumber (1, 1); // construct 1-i ComplexNumber c2 = new ComplexNumber (1, -1); // add these two c1.Add(c2); // array of complex numbers ComplexNumber rootsOfUnity[] = new ComplexNumber[n];

  5. Example: complex number for (i=0; i<n; i++) { rootsOfUnity[i] = new ComplexNumber (Math.cos (2*i*Math.PI/n), Math.sin (2*i*Math.PI/n)); } } }

  6. Example: room public class Room { private int numDoors; private int numWindows; private int numLamps; private int numFans; private String wallColor; // Override default constructor public Room () { numDoors = 1; numWindows = 2; numLamps = 1; numFans = 1; wallColor = “White”; }

  7. Example: room // a richer constructor public Room (int nD, int nW, int nL, int nF, String wC) { numDoors = nD; numWindows = nW; numLamps = nL; numFans = nF; wallColor = new String (wC); // what is the problem with wallColor = wC ? }

  8. Example: room // might want to add a fan public void AddFan (int howmany) { numFan += howmany; } // might want to re-paint wall public void PaintWall (String whichcolor) { wallColor = new String (whichcolor); } }

  9. Example: room public class House { // array of rooms private Room rooms[]; // might have some people private int numHuman; // might have some animals private int numPets; // might have a garden private boolean isThereAGarden; // might have a swimming pool private boolean isThereASwimmingPool;

  10. Example: room // override default constructor public House () { rooms = new Room[1]; rooms[0] = new Room ();// default room numHuman = 1; numPets = 1; isThereAGarden = true; isThereASwimmingPool = false; }

  11. Example: room // More useful constructor public House (int nD[], int nW[], int nL[], int nF[], String wC[], int nH, int nP, boolean g, boolean sp) { int numRooms = nD.length; int i; rooms = new Room[numRooms]; for (i=0; i<numRooms; i++) { rooms[i] = new Room (nD[i], nW[i], nL[i], nF[i], wC[i]); } numHuman = nH;

  12. Example: room numPets = nP; isThereAGarden = g; isThereASwimmingPool = sp; } // might want to add a fan in some room public void AddFan (int roomID) { rooms[roomID].AddFan (1); } // paint wall of some room public void PaintWall (int roomID, String color) { rooms[roomID].PaintWall (color); }

  13. Example: room // add a pet public void BringPet () { numPets++; } // get reference to a room public Room GetRoom (int roomID) { return rooms[roomID]; } }

  14. Example: room class HouseBuilder { public static void main (String a[]) { House myHouse = new House (); // repaint wall myHouse.GetRoom(0).PaintWall (“Golden”); // could paint wall of room[0] as below also // myHouse.PaintWall (0, “Golden”); // bring a pet myHouse.BringPet(); } }

  15. Example: polynomial A polynomial is an aggregate of algebraic terms Each term has a coefficient and an array of indices, one for each variable Need three classes: an AlgebraicTerm class, a polynomial class, and a top level class implementing main

  16. Example:Algebraic Term public class AlgebraicTerm { private double coefficient; private int[] index; // General constructor public AlgebraicTerm (double coeff, int[] ind) { int numVar = ind.length; int i; index = new int[numVar]; for (i=0; i<numVar; i++) { index[i] = ind[i]; } coefficient = coeff; } // continued in next slide

  17. // Special constructor for univariate // Note: method overloading public AlgebraicTerm (double coefficient, int index) { this.coefficient = coefficient; this.index = new int[1]; this.index[0] = index; } // continued in next slide

  18. public double Evaluate (double[] v) { int i; double value = coefficient; for (i=0; i<index.length; i++) { // Could say this.index[i] // But not needed value *= Math.pow(v[i], index[i]); } return value; } // continued in next slide

  19. public AlgebraicTerm Add (AlgebraicTerm aterm) { AlgebraicTerm rterm = null; if (checkConsistency (aterm)) { rterm = new AlgebraicTerm (coefficient+aterm.GetCoefficient(), index); } return rterm; } // continued in next slide

  20. public double GetCoefficient () { return coefficient; } public int[] GetIndex () { return index; } // continued in next slide

  21. private boolean checkConsistency (AlgebraicTerm aterm) { return (index.length == aterm.GetIndex().length); } public static void main(String arg[ ]){ int [ ] a = {3,2,1}; double c=2.5; double [ ] b ={2.0, 3.0, 1.0}; AlgebraicTerm A = new AlgebraicTerm(c,a); System.out.println(A.Evaluate(b)); Example(); }

  22. public static void Example(){ int[] a= {2,3,1}; double c= 2.5; AlgebraicTerm A = new AlgebraicTerm(c,a); a[0]=1; a[1]=2; a[2]=3; c=3.2; AlgebraicTerm B = new AlgebraicTerm(c,a); System.out.println(A.coefficient); for (int i=0;i<A.index.length;i++) System.out.println(A.index[i]); System.out.println(B.coefficient); for (int i=0;i<A.index.length;i++) System.out.println(B.index[i]); double [] b={3.0,2.0,1.0}; System.out.println(A.Evaluate(b)); System.out.println(B.Evaluate(b)); } }

  23. public class ATExample{ public static void main(String arg[]){ AlgebraicTerm C= new AlgebraicTerm(2.0,4); C.Example(); int[] a= {2,3,1}; double c= 2.5; AlgebraicTerm A = new AlgebraicTerm(c , a); System.out.println(A.GetCoefficient()); for (int i=0;i<A.GetIndex().length;i++) System.out.println(A.GetIndex()[i]); double [] b={3.0,2.0,1.0}; System.out.println(A.Evaluate(b)); } }

  24. Example: polynomial public class Polynomial { AlgebraicTerm [] terms; public Polynomial (double [] coefficient, int [] index[], int numVar) { int nTerms = coefficient.length; int i; terms = new AlgebraicTerm[nTerms]; for (i=0; i<nTerms; i++) { terms[i] = new AlgebraicTerm (coefficient[i], index[i]); } } // continued in next slide

  25. Example: polynomial public Polynomial (AlgebraicTerm [] t) { int nTerms = t.length; int i; terms = new AlgebraicTerm[nTerms]; for (i=0; i<nTerms; i++) { terms[i] = t[i]; } } // continued in next slide

  26. Example: polynomial public void SetTerms (AlgebraicTerm[] terms) { this.terms = terms; } public AlgebraicTerm[] GetTerms () { return terms; } // continued in next slide

  27. Example: polynomial public double Evaluate (double[] v) { int nTerms = terms.length; int i; double value=0; for (i=0; i<nTerms; i++) { value += terms[i].Evaluate(v); } return value; } // continued in next slide

  28. Example: polynomial public Polynomial Add (Polynomial p) { Polynomial q = null; AlgebraicTerm[] aterms; int i; if (checkConsistency(p)) { // Assume ordered terms aterms = new AlgebraicTerm [terms.length]; for (i=0; i<terms.length; i++) { aterms[i] = terms[i].Add (p.GetTerms()[i]); } q = new Polynomial (aterms); } return q; } // continued in next slide

  29. Example: polynomial private boolean checkConsistency (Polynomial p) { return (p.GetTerms().length == terms.length); } } // end of polynomial class; continued in next slide

  30. Example: polynomial class PolynomialExample { public static void main (String arg[]) { double coeff[] = {1, 2, 1}; int index[][] = {{2}, {1}, {0}}; double values[] = {-2}; Polynomial perfectQ = new Polynomial (coeff, index, 1); coeff[1] = -2; Polynomial perfectQ2 = new Polynomial (coeff, index, 1); Polynomial oneMoreQ = perfectQ.Add (perfectQ2); // continued in next slide

  31. Example: polynomial swap (perfectQ, perfectQ2); System.out.println (perfectQ.Evaluate(values) + “, ” + perfectQ2.Evaluate(values)); } // end main // following method doesn’t work /* private static void swap (Polynomial P1, Polynomial P2) { AlgebraicTerm terms[] = P1.terms; P1.terms = P2.terms; P2.terms = terms; }*/ // continued in next slide

  32. Example: polynomial // Note that swap must be static because // it is called by a static method private static void swap (Polynomial P1, Polynomial P2) { AlgebraicTerm terms[] = P1.GetTerms(); P1.SetTerms (P2.GetTerms()); P2.SetTerms (terms); } } // end class

More Related