# 16 Object-Orientation

16.1 Basics of Object-Orientation

16.1.1 Methods and Members

16.1.1.1 Parameters as Members

16.1.1.2 Visibility

16.1.2 this Keyword

16.1.3 Encapsulation/Separating Interface from Implementation

16.1.4 Special Methods

16.1.4.1 Operator Methods

16.1.4.2 Unary Operators

16.1.4.3 Property Assignment Methods

16.1.4.4 The apply Method

16.1.4.5 The update Method

16.1.4.6 Overloading Methods

16.1.4.7 Vector Example

16.1.5 object Declarations

16.1.5.1 Applications

16.1.5.2 Introduction to Companion Objects

16.1.6 Object Decomposition

16.2 Revisiting the API

16.3 Meaning of case classes

16.4 import Options

Exercises

W.1. Write an application with a GUI that has a menu and a large TextArea. The menu should have options for Open, Save, and Exit. When the user selects File > Open, it should bring up a FileChooser for them to select a file. The file they select should be read in and the text in the TextArea should be set to that text. When the user selects File > Save, a FileChooser should come up letting them select the file to save to and the text in the TextArea should be written to that file.

W.2. Write a class that represents a polynomial. The data for this can be stored in a Seq[Double] where each value if the coefficient on a term. So the polynomial 2x^2+3x-4 could be stored as Array(-4,3,2). Not that the 0 index holds the coefficient for x^0 and in general the value at index i is the coefficent for x^i. Add methods into your polynomial that will add and subtract two polynomials. Also add methods to evaluate a polynomial at a particular value of x as well as a method for taking the derivative of that polynomial. You can write this class in both a mutable and immutable form. For practice, you should also include a companion object with a proper implementation of apply so that you can make polynomials without using new.

W.3. Write a class to represent a Matrix. Your matrix should have methods for doing addition, subtraction, and multiplication. For further practice you could add methods to find the determinant and the inverse.

Projects

W.1. If you did exercise W.2 you can build on top of this by putting a full GUI around it so that you can plot instances of your polynomial class. Write this as an application, not a script.