Example complex number public class Complex Number private
Example: complex number public class Complex. Number { private double real; private double img; // override default constructor public Complex. Number () { real = 0. 0; img = 0. 0; } // a more meaningful constructor public Complex. Number (double x, double y) { real = x; img = y; }
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 (Complex. Number c) { real += c. Get. Real(); img += c. Get. Img(); }
Example: complex number // add the Get/Set methods public double Get. Real () { return real; } public double Get. Img () { return img; } public void Set. Real (double x) { real = x; } public void Set. Img (double x) { img = x; } } // end class
Example: complex number class Complex. Number. Tester { public static void main (String a[]) { int n = 10, i; // construct 1+i Complex. Number c 1 = new Complex. Number (1, 1); // construct 1 -i Complex. Number c 2 = new Complex. Number (1, -1); // add these two c 1. Add(c 2); // array of complex numbers Complex. Number roots. Of. Unity[] = new Complex. Number[n];
Example: complex number for (i=0; i<n; i++) { roots. Of. Unity[i] = new Complex. Number (Math. cos (2*i*Math. PI/n), Math. sin (2*i*Math. PI/n)); } } }
Example: room public class Room { private int num. Doors; private int num. Windows; private int num. Lamps; private int num. Fans; private String wall. Color; // Override default constructor public Room () { num. Doors = 1; num. Windows = 2; num. Lamps = 1; num. Fans = 1; wall. Color = “White”; }
Example: room // a richer constructor public Room (int n. D, int n. W, int n. L, int n. F, String w. C) { num. Doors = n. D; num. Windows = n. W; num. Lamps = n. L; num. Fans = n. F; wall. Color = new String (w. C); // what is the problem with wall. Color = w. C ? }
Example: room // might want to add a fan public void Add. Fan (int howmany) { num. Fan += howmany; } // might want to re-paint wall public void Paint. Wall (String whichcolor) { wall. Color = new String (whichcolor); } }
Example: room public class House { // array of rooms private Room rooms[]; // might have some people private int num. Human; // might have some animals private int num. Pets; // might have a garden private boolean is. There. AGarden; // might have a swimming pool private boolean is. There. ASwimming. Pool;
Example: room // override default constructor public House () { rooms = new Room[1]; rooms[0] = new Room (); // default room num. Human = 1; num. Pets = 1; is. There. AGarden = true; is. There. ASwimming. Pool = false; }
Example: room // More useful constructor public House (int n. D[], int n. W[], int n. L[], int n. F[], String w. C[], int n. H, int n. P, boolean g, boolean sp) { int num. Rooms = n. D. length; int i; rooms = new Room[num. Rooms]; for (i=0; i<num. Rooms; i++) { rooms[i] = new Room (n. D[i], n. W[i], n. L[i], n. F[i], w. C[i]); } num. Human = n. H;
Example: room num. Pets = n. P; is. There. AGarden = g; is. There. ASwimming. Pool = sp; } // might want to add a fan in some room public void Add. Fan (int room. ID) { rooms[room. ID]. Add. Fan (1); } // paint wall of some room public void Paint. Wall (int room. ID, String color) { rooms[room. ID]. Paint. Wall (color); }
Example: room // add a pet public void Bring. Pet () { num. Pets++; } // get reference to a room public Room Get. Room (int room. ID) { return rooms[room. ID]; } }
Example: room class House. Builder { public static void main (String a[]) { House my. House = new House (); // repaint wall my. House. Get. Room(0). Paint. Wall (“Golden”); // could paint wall of room[0] as below also // my. House. Paint. Wall (0, “Golden”); // bring a pet my. House. Bring. Pet(); } }
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 Algebraic. Term class, a polynomial class, and a top level class implementing main
Example: Algebraic Term public class Algebraic. Term { private double coefficient; private int[] index; // General constructor public Algebraic. Term (double coeff, int[] ind) { int num. Var = ind. length; int i; index = new int[num. Var]; for (i=0; i<num. Var; i++) { index[i] = ind[i]; } coefficient = coeff; } // continued in next slide
// Special constructor for univariate // Note: method overloading public Algebraic. Term (double coefficient, int index) { this. coefficient = coefficient; this. index = new int[1]; this. index[0] = index; } // continued in next slide
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
public Algebraic. Term Add (Algebraic. Term aterm) { Algebraic. Term rterm = null; if (check. Consistency (aterm)) { rterm = new Algebraic. Term (coefficient+aterm. Get. Coefficient(), index); } return rterm; } // continued in next slide
public double Get. Coefficient () { return coefficient; } public int[] Get. Index () { return index; } // continued in next slide
private boolean check. Consistency (Algebraic. Term aterm) { return (index. length == aterm. Get. Index(). length); } public static void main(String arg[ ]){ int [ ] a = {3, 2, 1}; double c=2. 5; double [ ] b ={2. 0, 3. 0, 1. 0}; Algebraic. Term A = new Algebraic. Term(c, a); System. out. println(A. Evaluate(b)); Example(); }
public static void Example(){ int[] a= {2, 3, 1}; double c= 2. 5; Algebraic. Term A = new Algebraic. Term(c, a); a[0]=1; a[1]=2; a[2]=3; c=3. 2; Algebraic. Term B = new Algebraic. Term(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)); } }
public class ATExample{ public static void main(String arg[]){ Algebraic. Term C= new Algebraic. Term(2. 0, 4); C. Example(); int[] a= {2, 3, 1}; double c= 2. 5; Algebraic. Term A = new Algebraic. Term(c , a); System. out. println(A. Get. Coefficient()); for (int i=0; i<A. Get. Index(). length; i++) System. out. println(A. Get. Index()[i]); double [] b={3. 0, 2. 0, 1. 0}; System. out. println(A. Evaluate(b)); } }
Example: polynomial public class Polynomial { Algebraic. Term [] terms; public Polynomial (double [] coefficient, int [] index[], int num. Var) { int n. Terms = coefficient. length; int i; terms = new Algebraic. Term[n. Terms]; for (i=0; i<n. Terms; i++) { terms[i] = new Algebraic. Term (coefficient[i], index[i]); } } // continued in next slide
Example: polynomial public Polynomial (Algebraic. Term [] t) { int n. Terms = t. length; int i; terms = new Algebraic. Term[n. Terms]; for (i=0; i<n. Terms; i++) { terms[i] = t[i]; } } // continued in next slide
Example: polynomial public void Set. Terms (Algebraic. Term[] terms) { this. terms = terms; } public Algebraic. Term[] Get. Terms () { return terms; } // continued in next slide
Example: polynomial public double Evaluate (double[] v) { int n. Terms = terms. length; int i; double value=0; for (i=0; i<n. Terms; i++) { value += terms[i]. Evaluate(v); } return value; } // continued in next slide
Example: polynomial public Polynomial Add (Polynomial p) { Polynomial q = null; Algebraic. Term[] aterms; int i; if (check. Consistency(p)) { // Assume ordered terms aterms = new Algebraic. Term [terms. length]; for (i=0; i<terms. length; i++) { aterms[i] = terms[i]. Add (p. Get. Terms()[i]); } q = new Polynomial (aterms); } return q;
Example: polynomial private boolean check. Consistency (Polynomial p) { return (p. Get. Terms(). length == terms. length); } } // end of polynomial class; continued in next slide
Example: polynomial class Polynomial. Example { public static void main (String arg[]) { double coeff[] = {1, 2, 1}; int index[][] = {{2}, {1}, {0}}; double values[] = {-2}; Polynomial perfect. Q = new Polynomial (coeff, index, 1); coeff[1] = -2; Polynomial perfect. Q 2 = new Polynomial (coeff, index, 1); Polynomial one. More. Q = perfect. Q. Add (perfect. Q 2); // continued in next slide
Example: polynomial swap (perfect. Q, perfect. Q 2); System. out. println (perfect. Q. Evaluate(values) + “, ” + perfect. Q 2. Evaluate(values)); } // end main // following method doesn’t work /* private static void swap (Polynomial P 1, Polynomial P 2) { Algebraic. Term terms[] = P 1. terms; P 1. terms = P 2. terms; P 2. terms = terms; }*/ // continued in next slide
Example: polynomial // Note that swap must be static because // it is called by a static method private static void swap (Polynomial P 1, Polynomial P 2) { Algebraic. Term terms[] = P 1. Get. Terms(); P 1. Set. Terms (P 2. Get. Terms()); P 2. Set. Terms (terms); } } // end class
- Slides: 32