cse 1301 lecture 11 object oriented programming figures from lewis, “c# software solutions”,...

56
CSE 1301 Lecture 11 Object Oriented Programming es from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

Upload: cameron-french

Post on 11-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Lecture 11

Object Oriented Programming

Figures from Lewis, “C# Software Solutions”, Addison Wesley

Richard Gesick

Page 2: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Topics

• Software Development• Identifying Classes & Objects• Static Members• Class Relationships• “this”• Parameters Revisited• Overloading

– Methods– Operators

• Testing

Page 3: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

The Software Life Cycle• The overall life cycle of a program includes use and

maintenance:

• A version of the software that is made available to user is called a release

UseDevelopment

Maintenance

Page 4: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Development vs. Maintenance

Use andMaintenance

Development

Page 5: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

The Waterfall Model

Establish requirements

Create design

Implement code

Test system

Page 6: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

An Iterative Development Process

Establishrequirements

Createdesign

Implementcode

Testsystem

Page 7: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Identifying Classes & Objects

• Identify potential classes within the specification

• Nouns

Page 8: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Page 9: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

In Gaming…

• How do you identify the classes within a game?

• What have you seen so far in labs and assignments?

Page 10: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

What Goes in the Class

• How do you decide what should be in the class– Data– Methods

• Not easy, but must be done

Page 11: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Static Members

• Static methods can be invoked via the class name

• Static variables are stored at the class level (one copy for all instances)

Page 12: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

The static Modifier• Remember that static methods (also called class methods) that

can be invoked through the class name rather than through a particular object

• For example, the methods of the Math class are static:

Math.sqrt (25)

• To write a static method, we apply the static modifier to the method definition

• The static modifier can be applied to variables as well

• It associates a variable or method with the class rather than with an object

Page 13: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Static Variables• Static variables are also called class variables • Normally, each object has its own data space, but if a variable

is declared as static, only one copy of the variable exists

private static int count;

• Memory space for a static variable is created when the class in which it is declared is loaded

• All objects created from the class share static variables• Changing the value of a static variable in one object changes it

for all others• Local variables cannot be static

Page 14: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Static Methods

public static int triple (int num){ int result; result = num * 3; return result;}

class Helper

Because it is static, the method can be invoked as:

value = Helper.triple (5);

Page 15: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Static Methods

• Static methods cannot reference instance variables, because instance variables don't exist until an object exists

• However, a static method can reference static variables or local variables

Page 16: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Class Relationships

Classes can have various relationships to one another. Most common are:

• Dependency (“uses”)• Aggregation (“has a”)• Inheritance (“is a”)

Page 17: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Object Relationships• Objects can have various types of relationships to each other

• A general association, as we've seen in UML diagrams, is sometimes referred to as a use relationship

• A general association indicates that one object (or class) uses or refers to another object (or class) in some way

• We could even annotate an association line in a UML diagram to indicate the nature of the relationship

Author Bookwrites

Page 18: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Dependency

• One class dependent (uses) another class– Game uses ball, paddle– Ship uses bullet

• Sometimes, a class depends on another instance of itself– Is one date equal to another date?– Is one picture equal to another picture?

Page 19: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Rational Class Design

• Let’s design a class that represents a rational number (fraction)

• What data needs to be stored?• What methods or operations are needed?

• Look at the following client code:

Page 20: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Rational r1 = new Rational(6, 8); Rational r2 = new Rational(1, 3); Rational r3, r4, r5, r6, r7; Console.Out.WriteLine("First rational number: " + r1); Console.Out.WriteLine("Second rational number: " + r2); if (r1.Equals(r2)) Console.Out.WriteLine("r1 and r2 are equal."); else Console.Out.WriteLine("r1 and r2 are NOT equal.");

r3 = r1.Reciprocal(); Console.Out.WriteLine("The reciprocal of r1 is: " + r3); r4 = r1.Add(r2); r5 = r1.Subtract(r2); r6 = r1.Multiply(r2); r7 = r1.Divide(r2);

Console.Out.WriteLine("r1 + r2: " + r4); Console.Out.WriteLine("r1 - r2: " + r5); Console.Out.WriteLine("r1 * r2: " + r6); Console.Out.WriteLine("r1 / r2: " + r7);

Console.In.ReadLine(); // Wait for enter key

Page 21: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Page 22: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

UML Design

Rational

Page 23: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Constructor

public Rational (int numer, int denom){

}

Page 24: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Reciprocal

public Rational Reciprocal ( ){

}

Page 25: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

ToString

public override string ToString ( ){

}

Page 26: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Reduce

private void Reduce ( ){

if (numerator != 0) { int common = Gcd(Math.Abs(numerator), denominator);

numerator = numerator / common; denominator = denominator / common; }}

Page 27: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Gcd

private int Gcd (int num1, int num2){

while (num1 != num2) if (num1 > num2) num1 = num1 - num2; else num2 = num2 - num1;

return num1;}

Page 28: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Add

public Rational Add (Rational op2){

}

Page 29: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Subtract

public Rational Subtract (Rational op2){

}

Page 30: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Multiply

public Rational Multiply (Rational op2){

}

Page 31: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Divide

public Rational Divide (Rational op2){

}

Page 32: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Equals

public bool Equals (Rational op2){

}

Page 33: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Aggregation

• One class is “made up” of other classes• “has a” relationship

– Gameboard has a marble– Deck has a card

Page 34: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Aggregation

• An aggregate object is an object that contains references to other objects

• For example, an Account object contains a reference to a String object (the owner's name)

• An aggregate object represents a has-a relationship

• A bank account has a name

• Likewise, a student may have one or more addresses

Page 35: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Aggregation in UML• An aggregation association is shown in a UML class

diagram using an open diamond at the aggregate end

StudentBody

+ Main (args : String[]) : void

+ ToString() : String

1 2Student

- firstName : String- lastName : String- homeAddress : Address- schoolAddress : Address

+ ToString() : String

- streetAddress : String

- city : String- state : String

- zipCode : long

Address

Page 36: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

The this Reference• The this reference allows an object to refer to itself

• That is, the this reference, used inside a method, refers to the object through which the method is being executed

• Suppose the this reference is used in a method called tryMe

• If tryMe is invoked as follows, the this reference refers to obj1:

obj1.tryMe();

• But in this case, the this reference refers to obj2:

obj2.tryMe();

Page 37: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

The this reference• The this reference can also be used to

distinguish the parameters of a constructor from the corresponding instance variables with the same names

public Account (String name, long acctNumber, double balance){ this.name = name; this.acctNumber = acctNumber; this.balance = balance;}

Page 38: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Assignment Revisited• The act of assignment takes a copy of a value and stores it in

a variable

• For primitive types:

num2 = num1;

Before

num1

5

num2

12

After

num1

5

num2

5

Page 39: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Reference Assignment

• For object references, assignment copies the memory location:

bishop2 = bishop1;

Before

bishop1 bishop2

After

bishop1 bishop2

Page 40: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Aliases• Two or more references that refer to the same

object are called aliases of each other

• One object (and its data) can be accessed using different reference variables

• Aliases can be useful, but should be managed carefully

• Changing the object’s state (its variables) through one reference changes it for all of its aliases

Page 41: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

ParametersThere are three main types of parameters:1. Value - passes a copy (value) of the variable to the

method. This is the default. 2. Reference - passes a reference to the actual variable.

Marked with "ref", use this when you want to pass a value in and have any change to that value be persistent when the method is complete

3. Out - passes a reference to the actual variable. Marked with "out", use this when you want the method to generate a value and place it for later use in the actual variable (persists when the method is complete)

Page 42: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

using System; namespace ParameterTester {

public class Num {

private int value;

public Num (int update) { value = update; }

public void SetValue (int update) { value = update; }

public override string ToString ( ) { return value + ""; }

} }

Page 43: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

using System;namespace ParameterTester { class ParameterPassing {

static void Main(string[] args) { ParameterModifier modifier = new

ParameterModifier(); int a1 = 111; Num a2 = new Num(222); Num a3 = new Num(333);

Console.Out.WriteLine("Before calling changeValues:"); Console.Out.WriteLine("a1\ta2\ta3"); Console.Out.WriteLine(a1 + "\t" + a2 + "\t" + a3 + "\n");

modifier.ChangeValues(a1, a2, a3);

Console.Out.WriteLine("After calling changeValues:"); Console.Out.WriteLine("a1\ta2\ta3"); Console.Out.WriteLine(a1 + "\t" + a2 + "\t" + a3 + "\n"); Console.In.ReadLine(); // Wait for enter key } }}

Page 44: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

using System;

namespace ParameterTester { public class ParameterModifier {

public void ChangeValues(int f1, Num f2, Num f3) { Console.Out.WriteLine("Before changing the values:"); Console.Out.WriteLine("f1\tf2\tf3"); Console.Out.WriteLine(f1 + "\t" + f2 + "\t" + f3 + "\n");

f1 = 999; f2.SetValue(888); f3 = new Num(777);

Console.Out.WriteLine("After changing the values:"); Console.Out.WriteLine("f1\tf2\tf3"); Console.Out.WriteLine(f1 + "\t" + f2 + "\t" + f3 + "\n"); Console.In.ReadLine(); } }}

Page 45: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Page 46: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Page 47: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Example 1static void Main() {

int a = 42; Console.WriteLine (a); B (a); Console.WriteLine (a);

} static void B (int x) {

x += 9; Console.WriteLine (x);

}

Page 48: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Example 2static void Main() {

int a = 42; Console.WriteLine (a); B (ref a); Console.WriteLine (a);

} static void B (ref int x) {

x += 9; Console.WriteLine (x);

}

Page 49: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Example 3static void Main() {

int a; B (out a); Console.WriteLine (a);

} static void B (out int x) {

x = 9; Console.WriteLine (x);

}

Page 50: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Example 4class Z { public int y; } static void Main() {

Z myZ = new Z(); myZ.y = 42; Console.WriteLine (myZ.y); B (myZ); Console.WriteLine (myZ.y);

} static void B (Z x) {

x.y += 9; Console.WriteLine (x.y);

}

Page 51: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Example 5class Z { public int y; } static void Main() {

Z myZ = new Z(); myZ.y = 42; Console.WriteLine (myZ.y); B (ref myZ); Console.WriteLine (myZ.y);

} static void B (ref Z x) {

x = new Z(); x.y = 1; Console.WriteLine (x.y);

}

Page 52: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

• Be careful to note the difference between a pass-by-reference parameter and a parameter of a reference type.

• Use the activation stack to track local variables and how parameters of the above types affect the variables from one stack frame to the next.

Page 53: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Overloading Methods• Method overloading is the process of using the same method

name for multiple methods

• The signature of each overloaded method must be unique

• The signature includes the number, type, and order of the parameters

• The compiler determines which version of the method is being invoked by analyzing the parameters

• The return type of the method is not part of the signature

Page 54: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Overloading Methods

float tryMe (int x){ return x + .375;}

Version 1

float tryMe (int x, float y){ return x*y;}

Version 2

result = tryMe (25, 4.32f)

Invocation

Page 55: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Overloading Operators

• In C#, not only can methods be overloaded, operators can be overloaded as well.

Page 56: CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301