CS256 Computer Science I Kevin Sahr, PhD Lecture 11: Objects 1 Programs as Models remember: we write programs to solve realworld problems programs act as models of the real-world problem to be solved one way to model problems is to use a mathematical approach model the problem using variables and formulas/ functions so far, all of our programs have used this approach 2 Procedural Paradigm in Computer Science this is known as the Procedural Programming Paradigm under this paradigm, programs consist of two basic building blocks: 1.data 2.methods 3 Class Methods Java implements the procedural programming paradigm using class methods method header contains the keyword static invoked using ClassName.methodName(arguments) so far all of our methods have been class methods 4
Class Methods (cont) class methods work fine for: mathematical functions EX: Math.sin methods associated with a single object EX: Input class, where all input comes from the keyboard (not from multiple sources) 5 EX: Cylinder using this approach, we can model a cylinder using: two variables representing the cylinder s height and radius class methods that calculate the surface area, volume, etc. for a given height and radius (passed as arguments) 6 Multiple Cylinders works fine for one cylinder what if we have two cylinders? need variables for each: height1 and radius1 height2 and radius2 but what if we have two thousand cylinders? what if each also has a weight, a name, a color, a location, etc.? 7 Objects need a better way to model the real world when we look around us, we don t see data and methods we see objects that have: 1. attributes: associated data values 2. behavior: what the object can do 8
EX: Student for example, each student has attributes: name, ID#, age, grades, etc. behavior: talk, walk, take quiz, etc. 9 Many Students each student has different attribute values Bob has ID#555 Mary has ID#101 each student has independent behavior Bob talks Mary takes a quiz 10 OOP this approach to modeling led to the Object Oriented Programming (OOP) Paradigm basic building blocks of programs are objects, each consisting of attributes and behavior Java is an OOP language 11 Class want each student to be represented by a student object in our program in Java we do this by expanding our definition of what a class is class: a blueprint or template for creating objects 12
- ClassName Classes & Objects each class defines the attributes and behavior associated with an object of that class every object is an instance of some class the attributes associated with an object are called instance variables the behaviors associated with an object are defined by methods called instance methods classes are like data types for objects 13 Using Objects how do we use objects in Java? for our examples, let s assume there exists a class named Square that specifies the attributes and behavior of a square object 14 Creating an Object Variable creating a variable that represents an object requires two steps: 1. declaration - creates a variable that will refer to the object 2. instantiation - creates the actual object 15 Variable Declaration syntax: variablename; EX: Square sqr; assigns the label sqr to a memory location tells Java that this location is going to refer to an instance of the Square class 16
ClassName - variablename Object Instantiation syntax: = new ClassName(); EX: sqr = new Square(); creates an instance of the Square class makes the variable sqr refer to the new Square object 17 One Step declaration and instantiation can be done in a single statement syntax: variablename = new ClassName(); EX: create two Square object variables Square s1 = new Square(); Square s2 = new Square(); 18 Class API a class API can contain three different types of methods: 1.constructors 2.class methods - have keyword static in method header 3.instance methods - method header exactly like class methods except no static 19 Class API a class API can contain three different types of methods: 1.constructors 2.class methods - have keyword static in method header 3.instance methods - method header exactly like class methods except no static 20
Constructors purpose is to initialize the object give initial values to the object attributes invoked using the new operator when an object is instantiated method name is the same as the class name have no return type (not even void) 21 Default Constructor the default constructor is a constructor that takes no arguments initializes object attributes to default values 22 EX: Square Class Square Class API Constructors public Square() Default constructor to create a Square that is red, 30 pixels in size, is located at (60, 50), and is not initially visible. 23 Invoking Constructors invoked with the new operator EX: Square s1 = new Square(); invokes default Square constructor 24
Multiple Constructors a class may have more than one constructor multiple constructors must take different arguments Java invokes the correct constructor based on the arguments you give it 25 EX: Person Class Person Class API Constructors public Person() Constructor for a Person named John Doe who is 0 years old. Initially this Person is alive. public Person(String name, int age) Constructor for a Person with the specified name and age. Initially this Person is alive. 26 Invoking Constructors invokes default Person constructor EX: Person p1 = new Person(); Person p2 = new Person( Mary, 20); invokes second Person constructor 27 Class API a class API can contain three different types of methods: 1.constructors 2.class methods - have keyword static in method header 3.instance methods - method header exactly like class methods except no static 28
Instance Methods once we have an object, we can invoke instance methods on it invocation syntax is similar to class methods except invoked on a particular instance syntax: objectname.methodname(arguments); the available instance methods are given in the class API instance method headers look exactly like class method headers, except they do not contain static 29 Square Class the Square class API is attached note it contains no class methods Note the changecolor() instance method with method header: public void changecolor(string newcolor) EX: Create two Square objects. Change the color of one to green, and the other to blue. Square sqra = new Square(); Square sqrb = new Square(); sqra.changecolor( green ); sqrb.changecolor( blue ); 30 Shape Example three shape classes (Square, Circle, and Triangle) are provided ShapeDemo.java is a program that demonstrates how to use these classes the compiled shape class files must be placed in the same folder with ShapeDemo.java spend some time understanding this Example 31 Lecture 11 Vocabulary procedural programming paradigm class method object attribute object oriented programming (OOP) paradigm class instance instance variable constructor default constructor instance method instantiation 32