220 likes | 329 Views
Chapter 5 Constructors. 1 Constructors. 2 Overloading. 3 . 4 Problem. 5 Solution. 6 Scope. 7 Object variables. Complex conjugate. 8 Example. public complexNumber conjugate() { complexNumber conj = new complexNumber(new cartesian(this.x,-this.y); return conj; }. add.
E N D
Chapter 5Constructors Constructors
1 Constructors Constructors
2 Overloading Constructors
3 ... Constructors
4 Problem Constructors
5 Solution Constructors
6 Scope Constructors
7 Object variables Constructors
Complex conjugate 8 Example public complexNumber conjugate() { complexNumber conj = new complexNumber(new cartesian(this.x,-this.y); return conj; } add public complexNumber add (complexNumber operand) { double RealSum = this.x + operand.x; double ImSum = this.y + operand.y; return new complexNumber(new cartesian(RealSum, ImSum)); } Do I need to expose the internal state Constructors
multiply 9 Private public complexNumber mult (complexNumber operand) { double RealProd = this.Real()*operand.Real() - this.Imaginary()+operand.Imaginary(); double ImProd = this.Real()*operand.Imaginary() + this.Imaginary()*operand.Real(); return new complexNumber(new cartesian(RealProd, ImProd)); } Here I am protecting one part of a class from changes in another part. multiply works if the storage of state change takes longer to execute add needs changing if the state changes executes fast Constructors
10 Storing state How to store state? public class complexNumber { private polar point; … } public class complexNumber { private cartesian point; … } How to decide? Constructors
11 Printing out an object public String toString() { String Cn = "(" + x + ", " + y + "i)"; return Cn; } The output from System.out.println(objectName); Is whatever is returned by toString; override toString in the Object class Constructors
12 Recap Constructors
13 Inheritance Constructors
14 ... Constructors
15 Extends Constructors
16 Core API Constructors
17 Pillage then burn Constructors
18 Evolution Constructors
19 Access Constructors
20 Object class Constructors