290 likes | 491 Views
CMPT 125. Object Oriented Programming. Objectives. Understand the RGB colour model and use Java Color objects Create and use classes , constructors , getters , setters , and mutators Use static and non-static variables and methods
E N D
CMPT 125 Object Oriented Programming
Objectives • Understand the RGB colour model and use Java Color objects • Create and use classes, constructors, getters, setters, and mutators • Use static and non-static variables and methods • Understand public and private methods and variables. John Edgar
Representing Colour • Let's say we want to represent colours (which may be drawn on a monitor) • There are many different colour models • One of the simplest to understand is the RGB (Red Green Blue) model • RGB colours • A colour is represented by three numbers, which represent the amount of red, green and blue in the colour • The numbers could be doubles (between 0.0 and 1.0) or • Integers, between 0 and some other number, in most cases 255 • How many colours can be represented? John Edgar
Colours and RGB Values r0, g0, b0 r78, g99, b45 r255, g124, b128 r128, g128, b128 r169, g87, b161 r255, g255, b0 r255, g255, b255 r255, g0, b0 r0, g255, b0 r0, g0, b255 John Edgar
Storing Colour Data • Three variables are required to represent one colour • It would be convenient to be able to refer to colours in the same way that we refer to primitive types • Object Oriented Programming (OOP) allows classes and objects to be used to organize code to collect variables and methods that represent one entity • An object is a collection of variables and methods • A class is a factory (or blueprint) for creating objects of a particular type John Edgar
Creating Object – Constructors • To create a new object it is necessary to use a special method called a constructor and the new operator • Color c1 = new Color(125, 67, 212); • Color c2 = new Color(0, 255, 0); //green • The constructor has the same name as the object’s class and may require information to be passed to it • Many classes (such as Color) have a number of different constructors • There are a couple of notable exceptions where it is not necessary to explicitly use the new operator and a constructor • New arrays can be created using an initialization list • Strings can be created by specifying the string literal to be created. John Edgar
Using Methods • Once an object has been created the dot operator can be used to access its methods • Note that an object is often referred to as an instance of a class • Therefore creating a new object is termed instantiating an object • The dot operator immediately follows the object reference, and is followed by the method name and brackets • Arguments required by the method are placed in the brackets • Using a method is termed calling, or invoking it John Edgar
Using Color Methods • To use a Color method create a Color object and then call the desired method • e.g. To find the green component of some Color object c • int g = c.getGreen(); • Methods are not limited to accessing data about an object • Some methods will change objects or create new objects • The Colorbrighter method returns a new, brighter version of the calling Color (without changing the calling Color object) • Color brightC = c.brighter(); John Edgar
What is an Object? • An object is some entity to be represented in a program • Can be concrete (e.g. a representation of a colour, a person, or a ball, or a car) or • Abstract (e.g. a representation of a theory or an error) • An object is described by • A set of attributes that define its state and • A set of behaviours that describe what it does John Edgar
Classes and Objects • An object is an instance of a class • An object is defined by its class • The class is the model, pattern or blueprint for creating objects of that type • Another analogy is that the class is a factory for creating objects • A class is itself not an object • In the same way that a blueprint for a car is not a car • Creating an object reserves space for the object in memory • Creating a new object is often referred to as instantiation • i.e. creating an instance • Each object has its own space in memory • Therefore each object has its own state John Edgar
Encapsulation • An object combines both variables and methods • Variables give the structure of an object • Methods dictate its behaviour • An object should encapsulate its attributes and behaviour • To encapsulate means to encase or enclose • Each object should protect and manage its own information, hiding the inner details • Objects should interact with the rest of the system only through a specific set of methods (its public interface) • In Java, object encapsulation is achieved using modifiers • A modifier is a reserved word that specifies a characteristic of a variable or method John Edgar
Creating Classes • There are many different classes available in the standard Java library • Collection classes such as ArrayList, HashSet, TreeMap • Color, for representing colours • Random, for generating random numbers • There are also many third party created classes, such as those in the csjava package • EasyInput, for streamlining input • TheMatrix, for displaying and manipulating 2D graphics • We can also create our own classes • To represent whatever is needed to solve a problem John Edgar
Three Scenarios • A program that calculates, and then graphically displays the height to which a dropped ball bounces • A program to compare and evaluate different models of toasters • A CRPG (Computer Role Playing Game) where the player takes on the role of a hero in a world of hideous monsters • Each program will require the creation of classes to represent program components John Edgar
Ball John Edgar
Toaster John Edgar
Creepy Spider Demon John Edgar
Designing a Class • There are many ways to design a class • Is its major purpose to store data? or • To perform some operation? or • A combination of the two? • Your initial focus may either be on the class variables or its methods • There are some general principles of good design John Edgar
Design: Make Variables Private • Variables should generally by made directly inaccessible from outside the class • This is achieved by making them private • The values of variables can be accessed using "getter" methods • A getter method returns the value of a variable • New values can be assigned to variables using "setter" methods • A setter method assigns the value passed to its parameter to a variable John Edgar
Design: Write Constructors • Constructors should initialize all of the variables in an object • It is often necessary to write more than one constructor • Default constructor, with no parameters that assigns default values to variables • Constructor with parameters that assigns the values passed to the parameters to the variables • Copy constructor that takes an object of the same class and creates a copy of it John Edgar
Java and the Default Constructor • If no constructor exists for a class Java creates a default constructor • This constructor assigns default values to each variable • Primitive typed variables are assigned the default values (e.g. 0 for numeric types) • Reference variables are assigned null • Creating any constructor (including constructors with parameters) prevents this default from being created John Edgar
Design: Make Helper Methods Private • Helper methods are methods that assist class methods in performing their tasks • Helper methods are often created to implement part of a complex task or to • Perform sub-tasks required by a number of methods • They are therefore only useful to the class and should not be visible outside the class • Helper methods are relevant only to the implementation of a class, therefore they should be hidden, and not made part of the interface John Edgar
Design: Setters Only When Needed • Remember that variables are made private to hide implementation details from other classes • To prevent classes from depending on each others' implementations and • To protect variables from being assigned inappropriate values • Providing a setter method to a variable may allow some other class to change the variable inappropriately • Therefore, consider whether or not each variable requires a setter method John Edgar
static variables and methods • static variables and methods “belong” to the class, not individual objects • They are shared by all objects of the same class • Static variables are often used to store class constants • Or keep track of data that relates to the entire class • There is only one copy of a static variable for the entire class • They may be changed by static or non-static methods • Static methods cannot access the variables of individual object • As it would not be possible to determine which object's variables to change John Edgar
non static variables and methods • If a variable or method is not given the static modifier then it is non-static and “belongs” to individual objects • Non-static variables are used to store data about individual objects • e.g. the characters of one string or the coordinates of one Point • They cannot be changed or accessed by non-static methods • Non-static methods can access either static or non-static variables (or methods) • When accessing non-static variables they refer to the variables that belong to the calling object John Edgar
Mutable and Immutable Objects • The data of an immutable object cannot be changed e.g. • Strings - it is not possible to change the characters in a string • The data of mutable can be changed e.g. • Points • Collection objects • Be careful when changing the data of a mutable object if • It is referred to by more than one variable or • If it is contained in a collection, particularly a set or a map John Edgar
Objects and Software Design • Using objects can aid design • People are used to the idea of everyday objects having states and behaviours • The same idea can be applied to components of a program • Using objects can also promote modular design where modules are • Highly cohesive (they perform one well defined task) and • Loosely coupled (modules are as independent as possible from each other) John Edgar
Coupling • Loosely coupled modules or classes are easier to understand • Can understand one class without reading many others • Loosely coupled modules or classes are easier to maintain • Changes to the implementation of one class can be made without affecting other classes John Edgar
Cohesion • Each module or class should have one logical task • Cohesion can be applied to both methods and classes • A method should be responsible for only one, well defined, task • A class should represent one, well defined, entity • A class with high cohesion is easier to understand, and therefore easier to modify and maintain John Edgar
Information Hiding • Modules should be loosely coupled and only allowed to communicate through their interfaces, thereby hiding the details of their implementations • Loose coupling decreases the likelihood that changing the implementation of one module will require major changes in other modules • Provides protection of the details from other modules, e.g. prevents other modules from assigning invalid values to a module's attributes • Information hiding can be easily achieved in object oriented programming John Edgar