name: inverse layout: true class: taylor msoe --- class: center, middle .title[ # Object Oriented Development ] ??? Toggle speaker view by pressing P Pressing C clones the slideshow view, which is the view to put on the projector if you're using speaker view. Press ? to toggle keyboard commands help. --- # Object Oriented Development * Idea: identify objects that need to be represented in your program + Group all data associated with the object together + Define functionality that can operate on that data -- * Complexity can be hidden within the class -- * Only implementer of the class needs to worry about that complexity -- * Results in shorter, simplified programs --- # Example: Reading Data ``` public static void main(String[] args) { Scanner in = new Scanner(System.in); // Get first number System.out.println("Enter a complex number in the form: 3.0 + 4.3i"); // Read line of text entered by user String line = in.nextLine(); // Create a scanner object of what the user entered, but don't include // the "i" at the end Scanner parser = new Scanner(line.substring(0, line.length()-1)); // Convert the real part to a double double realOne = parser.nextDouble(); // Read in and ignore the plus symbol parser.next(); // Convert the imaginary part to a double double imagOne = parser.nextDouble(); // Use Complex class to read second number: System.out.println("Enter a complex number in the form: 3.0 + 4.3i"); Complex c2 = new Complex(in.nextLine()); ``` --- # Example: Calculations ``` // Calculate result of adding two complex numbers double realAnswer = realOne + realTwo; double imagAnswer = imagOne + imagTwo; System.out.println("(" + realOne + " + " + imagOne + "i) * (" + realTwo + " + " + imagTwo + "i) = (" + realAnswer + " + " + imagAnswer + "i)"); // Use Complex class to simplify above to one line: System.out.println(c1 + " * " + c2 + " = " + c1.plus(c2)); } ``` --- # Defining a Class ``` public class Complex { private double real; private double imag; public Complex(String complex) { ... } public Complex(double real, double imag) { ... } public static Complex sum(Complex first, Complex second) { ... } public Complex plus(Complex that) { ... } } ``` -- * A class definition consists of attributes/fields and methods/behaviors -- * Attribute or method can be associated with an object or the class --- # UML Class Diagram * Unified Modeling Language (UML) Class Diagrams describe a class "graphically" --
--- # Visibility Modifiers * Apply to attribute or method declared/defined immediately after the modifier -- * `private double real;` — `real` only visible in methods within the class -- * `public Complex(String value) {` — visible everywhere -- * Attributes typically declared `private` -- * Methods typically declared `public` --- # Methods * A method declaration consists of: + A visibility modifier: determines who can access the method + A return type: the type of value returned (or `void` if nothing is returned) + A name: used to call the method + Zero or more parameters: local variables whose values are assigned by the arguments passed to the method ``` public static Complex sum(Complex one, Complex two) { ``` -- * The body of a method contains a sequence of instructions ``` double sumReal = one.real + two.real; double sumImag = one.imag + two.imag; return new Complex(sumReal, sumImag); } ``` --- # Instance Variables / Instance Methods * Associated with a specific instance from the class -- * Need reference to an object to access instance variable/method, e.g., `in.next();` -- * Each object has its own copy of the instance variables -- * All instance variables/methods are available inside instance methods -- * A local variable with the same name as an instance variable hides the instance variable -- * The keyword `this` is available in each instance method -- * `this` is a reference that points to the object that called the method -- * `this` can be used to disambiguate between instance and local variables --- # Memory Diagram ``` // In main: // In the Complex class: Complex num1 = new Complex(2.0, 4.0); public Complex plus(Complex that) { Complex num2 = new Complex(3.0, 5.0); double r = real + that.real; Complex num3 = num1.plus(num2); double imag = this.imag + that.imag; Complex result = new Complex(r, imag); return result; } ``` --
--- # Class Variables / Class Methods * A class variable or method is declared as `static` -- * Associated with a class (as opposed to an instance) -- * Accessed with the class name. E.g., `Math.PI`, `Math.sin(3)` -- * Class methods have access to class variables/methods but **not** instance variables/methods --- # Encapsulation Combine data and behavior associated with that data in a class -- * Provides a public interface — rules for interacting with the data -- * Simplifies client code, e.g., `in.nextInt()` -- * Allows focus on business logic -- * Facilitates distribution of development effort by providing a clear boundary of functionality -- * Fosters code reuse --- # Information Hiding * We declare attributes as `private` to hide this information from client code -- * Protects data from unauthorized use -- * Simplifies implementation of class since access/modification of attributes is controlled -- * Allows for significant class implementation changes without modifying client code --- # Class Constants * A class constant is a class variable whose value cannot change -- * Should have names that are all uppercase with underscores between words, e.g., `SPEED_OF_LIGHT` -- * Must be defined when declared -- * Class constants may be declared `public` ``` public static final double SPEED_OF_LIGHT = 299_792_458; // m/s ``` --- # Constructors * Must have same as the class -- * Do not provide a return type in the declaration -- * The constructor implicitly returns a reference to the object that was created -- * Called with the `new` keyword, e.g., `new Complex(2.0, 4.0)` -- * Used to initialize instance variables ``` public Complex(double real, double imag) { this.real = real; this.imag = imag; } ``` --- # Method Overloading * We say that a method is **overloaded** when there are at least two methods in a class with the same name -- * The methods must have a different parameter list -- * Compiler determines which method to call based on the arguments passed to the method ``` Complex num1 = new Complex(2, 4); Complex num2 = num1.plus(num1); Complex num3 = num1.plus(3.2); Complex num4 = num1.plus(8.6, 1.2); ``` -- * The above calls three different `plus()` methods: ``` public Complex plus(Complex that) { ... } public Complex plus(double real) { ... } public Complex plus(double real, double imag) { ... } ``` --- # Constructor Overloading ``` public Complex() { real = 0.0; imag = 0.0; } public Complex(double real) { this.real = real; imag = 0.0; } public Complex(double real, double imag) { this.real = real; this.imag = imag; } ``` -- * Multiple constructors may have similar/identical code --- # Simplified Constructor Implementation * We can reduce duplicate code by having one constructor call another constructor ``` public Complex() { this(0.0, 0.0); } public Complex(double real) { this(real, 0.0); } public Complex(double real, double imag) { this.real = real; this.imag = imag; } ``` -- * The call to another constructor must be the first call in the constructor --- # Checking for Equality with `.equals()` * Comparing two references with `==` tells you if both references refer to exactly the same object -- * If we want to know if the two objects are considered equal, we can call `.equals()` ``` Complex num1 = new Complex(2, 4); Complex num2 = new Complex(2, 4); System.out.println(num1.equals(num2)); ``` --- # Implementing `.equals()` * We can define the `.equals()` method to return `true` when we consider the objects equivalent ``` public boolean equals(Complex that) { final double equivalenceThreshold = 0.0001; boolean isEqual = Math.abs(real-that.real)