Object Oriented Programming

Slides:



Advertisements
Similar presentations
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Chapter 4: Writing Classes
Inheritance Inheritance Reserved word protected Reserved word super
Problem Solving #1 ICS Outline Review of Key Topics Review of Key Topics Example Program Example Program –Problem 7.1 Problem Solving Tips Problem.
Writing Classes in Java
Chapter 6 Object-Oriented Design. 6-2 Program Development The creation of software involves four basic activities:  establishing the requirements  creating.
Java Review Selim Aksoy Bilkent University Department of Computer Engineering
Enhancing classes Visibility modifiers and encapsulation revisited
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Classes, Encapsulation, Methods and Constructors
Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Static Class Members Wrapper Classes Autoboxing Unboxing.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Introduction to UML Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
Object Oriented Design and UML
Writing Classes (Chapter 4)
The this Reference The this reference, used inside a method, refers to the object through which the method is being executed Suppose the this reference.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Chapter 6 Object-Oriented Design Part 1. © 2004 Pearson Addison-Wesley. All rights reserved2/42 Object-Oriented Design Now we can extend our discussion.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Inheritance Reading for this Lecture: L&L 6.4 – 6.5.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Chapter 7 Object-Oriented Design Concepts
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Static class members.
CSE 1201 Object Oriented Programming Object Oriented Design.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Outline Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited.
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
University of Turkish Aeronautical Association Computer Engineering Department CENG 112 COMPUTER PROGRAMMING 2 Tansel Dökeroglu, Ph.D.
Chapter 5: Enhancing Classes
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Chapter 4: Writing Classes
Creating Objects & String Class
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
More Object Oriented Programming
Chapter 5 – Writing Classes
Object Based Programming
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Lecture 22 Inheritance Richard Gesick.
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.
Chapter 5: Enhancing Classes
Chapter 4 Writing Classes.
Static Class Members March 29, 2006 ComS 207: Programming I (in Java)
Overriding Methods & Class Hierarchies
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Object Oriented Programming Review
Packages & Random and Math Classes
Chapter 8 Inheritance Part 2.
Object-Oriented Design Part 2
Classes and Objects Object Creation
Chapter 4: Writing Classes
Presentation transcript:

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

Topics Software Development Identifying Classes & Objects Static Members Class Relationships “this” Parameters Revisited Overloading Methods Operators Testing

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 Use Development Maintenance

Development vs. Maintenance Use and Maintenance

The Waterfall Model Establish requirements Create design Implement code Test system

An Iterative Development Process Establish requirements Create design Implement code Test system

Identifying Classes & Objects Identify potential classes within the specification Nouns

In Gaming… How do you identify the classes within a game? What have you seen so far in labs and assignments?

What Goes in the Class How do you decide what should be in the class Data Methods Not easy, but must be done

Static Members Static methods can be invoked via the class name Static variables are stored at the class level (one copy for all instances)

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

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

Static Methods class Helper 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);

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

Class Relationships Classes can have various relationships to one another. Most common are: Dependency (“uses”) Aggregation (“has a”) Inheritance (“is a”)

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 Book writes

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?

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:

Rational r1 = new Rational(6, 8); 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

UML Design Rational

Constructor public Rational (int numer, int denom) { }

Reciprocal public Rational Reciprocal ( ) { }

ToString public override string ToString ( ) { }

Reduce private void Reduce ( ) { if (numerator != 0) int common = Gcd(Math.Abs(numerator), denominator); numerator = numerator / common; denominator = denominator / common; }

Gcd private int Gcd (int num1, int num2) { while (num1 != num2) if (num1 > num2) num1 = num1 - num2; else num2 = num2 - num1; return num1; }

Add public Rational Add (Rational op2) { }

Subtract public Rational Subtract (Rational op2) { }

Multiply public Rational Multiply (Rational op2) { }

Divide public Rational Divide (Rational op2) { }

Equals public bool Equals (Rational op2) { }

Aggregation One class is “made up” of other classes “has a” relationship Gameboard has a marble Deck has a card

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

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 2 Student - firstName : String - lastName : String - homeAddress : Address - schoolAddress : Address - streetAddress : String - city : String - state : String - zipCode : long Address

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();

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; }

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

Reference Assignment Before After For object references, assignment copies the memory location: bishop2 = bishop1; Before bishop1 bishop2 After bishop1 bishop2

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