A Second Look at Classes and Objects - 2

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
9-1 Chapter.8 Classes & Objects: A deeper look –Static Class Members –Passing & Returning Objects from Methods –The toString method –Writing an equals.
Chapter 8: A Second Look at Classes and Objects
© 2010 Pearson Addison-Wesley. All rights reserved. 9-1 Chapter Topics Chapter 9 discusses the following main topics: –Static Class Members –Passing Objects.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Java Software Solutions
1 Fall 2009ACS-1903 Methods – Ch 5 A design technique referred to as stepwise refinement (or divide and conquer, or functional decomposition) is used to.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Chapter 6: A First Look at Classes
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 8.
Class and Object. Class vs Object Let us consider the following scenario… Class defines a set of attributes/fields and a set of methods/services. Object.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
9-2  Static Class Members  Passing Objects as Arguments to Methods  Returning Objects from Methods  The toString method  Writing an equals Method.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes: Cooper & Ji & Varol Fall 09.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Chapter 9 – Part Tony Gaddis Modified by Elizabeth Adams.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
A Second Look At Classes And Objects Chapter 9. Static Class Members When a value is stored in a static field, it is not in an object of the class. In.
Topics Instance variables, set and get methods Encapsulation
Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #1 Review.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Introduction to Constructors Lecture # 4. Copyright © 2011 Pearson Education, Inc. 3-2 Arguments Passed By Value In Java, all arguments to a method are.
Chapter 9 A Second Look at Classes and Objects - 4.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Chapter 9 A Second Look at Classes and Objects - 2.
Chapter 8 Arrays and the ArrayList Class Introduction to Arrays.
Programming Logic and Design Seventh Edition
Chapter 7: User-Defined Functions II
Java Primer 1: Types, Classes and Operators
Arrays 2/4 By Pius Nyaanga.
Chapter 10: Void Functions
Topics Static Class Members Overloaded Methods Overloaded Constructors
Chapter 3: Using Methods, Classes, and Objects
by Tony Gaddis and Godfrey Muganda
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter Topics Chapter 5 discusses the following main topics:
Chapter 9: A Second Look at Classes and Objects
Chapter 9: A Second Look at Classes and Objects
Introduction to Classes
Starting Out with Java: From Control Structures through Objects
Building Java Programs
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Today’s topics UML Diagramming review of terms
Defining Classes and Methods
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Lecture 5- Classes, Objects and Methods
Dr. Sampath Jayarathna Cal Poly Pomona
Suggested self-checks: Section 7.11 #1-11
Defining Classes and Methods
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
A+ Computer Science PARAMETERS
C Parameter Passing.
Building Java Programs
Presentation transcript:

A Second Look at Classes and Objects - 2 Chapter 9 A Second Look at Classes and Objects - 2

Contents Passing Objects As Arguments to Methods Returning Objects from Methods The toString Method Writing an equals Method Methods That Copy Objects

I. Passing Objects As Arguments to Methods When a variable is passed as an argument to a method, it is said to be passed by value. This means that a copy of the variable's value is passed into the method's parameter. When the method changes the contents of the parameter variable, it does not affect the contents of the original variable that was passed as an argument.

I. Passing Objects As Arguments to Methods To pass an object as a method argument, we pass an object reference. Reference variable holds the memory address of an object. The method has access to the object that the variable reference. When writing a method that receives the value of a reference as an argument, we must take care not to accidentally modify the contents of the object that is referenced by default.

I. Passing Objects As Arguments to Methods Passing a reference as an argument A Rectangle object changeRectangle(box); public static void changeRectangle(Rectangle r) { r.setLength(0.0); r.setWidth(0.0); } length: 12.0 width: 5 address

I. Passing Objects As Arguments to Methods Both box and r reference the same object A Rectangle object length: 12.0 width: 5 The box variable holds the address of a Rectangle object. address The r variable holds the address of a Rectangle object. address

II. Returning Objects from Methods A method can return a reference to an object. The address of the object is then returned from the method.

II. Returning Objects from Methods account = getAccount(); A BankAccount object balance: 3200.0 address public static BankAccount getAccount() { (Statements appear here) return new BankAccount(balance); } The getAccount method returns a reference to a BankAccount object.

III. The toString Method Most classes can benefit from having a method named toString: Is implicitly called under certain circumstances Typically, the method toString returns a string that represents the state of an object. An object's state is simply the data that is stored in the object's fields at any given moment. For example, the value of the balance field represents the BankAccount object's state at that moment.

III. The toString Method An example of a class, which holds data about a company's stock: symbol: holds the trading symbol for the company's stock sharePrice: holds the current price per share of the stock. UML diagram for Stock class Stock - symbol : String - sharePrice : double + Stock(sym : String, price : double) + getSymbol() : String + getSharePrice() : double + toString : String

III. The toString Method

III. The toString Method

III. The toString Method

III. The toString Method Evey class automatically has a toString method that returns a string containing: the object's class name, and the @ symbol, and an integer that is usually based on the object's memory address.

IV. Writing an equals Method How to determine whether two objects contain the same data? We cannot compare them with == operator. Instead, the class must have a method such as equals for comparing the contents of objects. Stock com1 = new Stock(“XYZ”, 9.62); Stock com2 = new Stock(“XYZ”, 9.62); if(com1 == com2) System.out.println(“Both objects are the same.”); else System.out.println(“The objects are different.”); This is a mistake.

IV. Writing an equals Method symbol: “XYZ” sharePrice: 9.62 A Stock object The com1 variable holds the address of a Stock address. address The if statement compares these two address. symbol: “XYZ” sharePrice: 9.62 A Stock object The com2 variable holds the address of a Stock address. address

IV. Writing an equals Method The equals method accepts a Stock object as its argument. public boolean equals(Stock object2) { boolean status; //Determine whether this object's symbol and //sharePrice fields are equal to object2's //symbol and sharePrice fields. if(symbol.equals(object2.symbol) && sharePrice == object2.sharePrice) status = true; else status = false; return status; }

IV. Writing an equals Method This code uses the Stock class's equals method to compare two Stock object. Stock com1 = new Stock("XYZ", 9.62); Stock com2 = new Stock("XYZ", 9.62); if(com1.equals(com2)) System.out.println("Both objects are the same."); else System.out.println("The objects are different.");

V. Methods That Copy Objects How to make a copy of an object? Stock com1 = new Stock(“XYZ”, 9.62); Stock com2 = com1; This is a mistake. - This does not make a copy of the object referenced by com1. - It makes a copy of address that is stored in com1 and stores that address in com2.

V. Methods That Copy Objects Stock com1 = new Stock(“XYZ”, 9.62); Stock com2 = com1; symbol: “XYZ” sharePrice: 9.62 A Stock object The com1 variable holds the address of a Stock address. address The com2 variable holds the address of a Stock address. address Both variables reference the same object. This type of assignment operation is called a reference copy because only the address is copied, not the actual object itself.

V. Methods That Copy Objects We should write a method in the class Stock named copy, which returns a copy of a Stock object: public Stock copy() { //Create a new Stock object and initialize it //with the same data held by the calling object. Stock copyObject = new Stock(symbol, sharePrice); //Return a reference to the new object return copyObject; }

V. Methods That Copy Objects Copy Constructors Another way to create a copy of an object is to use a copy constructor. A copy constructor is simply a constructor that accepts an object of the same class as an argument. It makes the object that is being created a copy of the object that was passed as an argument.

V. Methods That Copy Objects The Copy Constructor of the Stock class: public Stock(Stock object2) { symbol = object2.symbol; sharePrice = object2.sharePrice; } How to use the copy constructor? Stock com1 = new Stock(“XYZ”, 9.62); //Create another Stock object that is //a copy of the com1 object Stock com2 = new Stock(com1);