CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.

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

Interfaces A Java interface is a collection
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Java Software Solutions
Chapter 4: Writing Classes
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
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.
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
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
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.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
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.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
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)
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
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
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
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.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
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.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
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 November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
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.
Chapter 5: Enhancing Classes
Chapter 4: Writing Classes
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
More Object Oriented Programming
Object Oriented Programming
Object Based Programming
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
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.
Classes, Encapsulation, Methods and Constructors (Continued)
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
Java Programming Language
Lecture 11 Parameters CSE /26/2018.
Object-Oriented Design Part 2
Parameters, Overloading Methods, and Random Garbage
Chapter 4: Writing Classes
Presentation transcript:

CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick

CSE 1302 Topics Identifying Classes & Objects Static Members Class Relationships “this” Parameters Revisited Overloading – Methods – Operators Testing

CSE 1302 Identifying Classes & Objects Identify potential classes within the specification Nouns

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

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

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

CSE 1302 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

CSE 1302 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

CSE 1302 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);

CSE 1302 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

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

CSE 1302 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

CSE 1302 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?

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

CSE 1302 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

CSE 1302 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 12 Student - firstName : String - lastName : String - homeAddress : Address - schoolAddress : Address + ToString() : String - streetAddress : String - city : String - state : String - zipCode : long Address

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

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

CSE 1302 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

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

CSE 1302 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

CSE 1302 Parameters There 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)

CSE 1302 Example 1 and the output is ? static void Main() { int a = 42; Console.WriteLine (a); B (a); Console.WriteLine (a); } static void B (int x) { x += 9; Console.WriteLine (x); }

CSE 1302 Example 2 and the output is ? static 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); }

CSE 1302 Example 3 and the output is ? static void Main() { int a; B (out a); Console.WriteLine (a); } static void B (out int x) { x = 9; Console.WriteLine (x); }

CSE 1302 Example 4 and the output is ? class 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); }

CSE 1302 Example 5 and the output is ? class 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); }

CSE 1302 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.

CSE 1302 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

CSE 1302 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

CSE 1302 Overloading Operators In C#, not only can methods be overloaded, operators can be overloaded as well.

CSE 1302

C# enables you to overload most operators to make them sensitive to the context in which they are used. Use operator overloading when it makes an application clearer than accomplishing the same operations with explicit method calls. Class ComplexNumber overloads the plus (+), minus (-) and multiplication (*) operators to enable programs to add, subtract and multiply instances of class ComplexNumber using common mathematical notation Operator overloading

CSE 1302 Operator Overloading Keyword operator, followed by an operator symbol, indicates that a method overloads the specified operator. Methods that overload binary operators must take two arguments—the first argument is the left operand, and the second argument is the right operand. Overloaded operator methods must be public and static.

CSE 1302

Overload the plus operator (+) to perform addition of ComplexNumber s