Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #1 Review.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
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
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
© 2010 Pearson Addison-Wesley. All rights reserved. 9-1 Chapter Topics Chapter 9 discusses the following main topics: –Static Class Members –Passing Objects.
Chapter 2 Application Layer. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 2-2.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Chapter 6 Human Capital. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 16 Resources and the Environment at the Global Level.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
Chapter 6: A First Look at Classes
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
Starting Out With Java 5 (Control Structures to Objects) Chapter 6 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
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.
1 CSC241: Object Oriented Programming Lecture No 22.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
Chapter 9: A Second Look at Classes and Objects
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.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
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.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7. 12: Structures Starting Out with C++ Early Objects Eighth.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
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.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Chapter 9 A Second Look at Classes and Objects - 2.
Chapter 10: Void Functions
Topics Static Class Members Overloaded Methods Overloaded Constructors
Chapter 8 Classes and Objects
A Second Look at Classes and Objects - 2
if-else-if Statements
Chapter 9: A Second Look at Classes and Objects
Chapter 9: A Second Look at Classes and Objects
Today’s topics UML Diagramming review of terms
Assessment – Java Basics: Part 1
Object Oriented Programming Review
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 4 Constructors Section 4.4
Classes and Objects Object Creation
Presentation transcript:

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #1 Review of Instance Fields and Methods Each instance of a class has its own copy of instance variables. Example: The Rectangle class defines a length and a width field. Each instance of the Rectangle class can have a different value stored in its length field. Instance methods require that an instance of a class be created in order to be used. Instance methods typically interact with instance fields or calculate values based on those fields.

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #2 The this Reference – Book 9.8 The this reference is simply a name that an object can use to refer to itself. The this reference can be used to overcome shadowing and allow a parameter to have the same name as an instance field. public void setFeet(int feet) { this.feet = feet; //sets the this instance’s feet field //equal to the parameter feet. } Local parameter variable feet Shadowed instance variable

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #3 The this Reference The this reference can be used to call a constructor from another constructor. public FeetInches(int feet) { this(feet, 0); } This constructor would allow an instance of FeetInches to be created using only feet as a parameter. It calls the constructor that takes feet and inches using feet as the feet argument and 0 as the inches argument. Elaborate constructor chaining can be created using this technique. If this is used in a constructor, it must be the first statement in the constructor.

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #4 Methods That Copy Objects – Chapter 9.6 There are two ways to copy an object. You cannot use the assignment operator to copy reference types Reference only copy This is simply copying the address of an object into another reference variable. Deep copy (correct) This involves creating a new instance of the class and copying the values from one object into the new object. Example: ObjectCopy.javaObjectCopy.java

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #5 Copy Constructors – 9.6 A copy constructor accepts an existing object of the same class and clones it public Stock(Stock object 2) { symbol = object2.symbol; sharePrice = object2.sharePrice; } Stock company1 = new Stock(“XYZ”, 9.62) // Create a Stock object //Create company2, a copy of company1 Stock company2 = new Stock(company1);

Overloading Methods – Book 6.4 Overloading is the term for the creation of two different methods with the same name. Must have different numbers of or types of parameters. Where have we seen these before? Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #6

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #7 Passing Objects as Arguments – 9.2 Objects can be passed to methods as arguments. Java passes all arguments by value. When an object is passed as an argument, the value of the reference variable is passed. The value of the reference variable is an address or reference to the object in memory. A copy of the object is not passed, just a pointer to the object. When a method receives a reference variable as an argument, it is possible for the method to modify the contents of the object referenced by the variable.

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #8 Passing Objects as Arguments Example: PassObject.java PassObject2.java PassObject.java PassObject2.java displayRectangle(box); Address public static void displayRectangle(Rectangle r) { // Display the length and width. System.out.println(“Length: “ + r.getLength() + “ Width: “ + r.getWidth()); } A Rectangle object length: width:

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #9 Returning Objects From Methods Chapter 9.3 Methods are not limited to returning the primitive data types. Methods can return references to objects as well. Just as with passing arguments, a copy of the object is not returned, only its address. Example: ReturnObject.javaReturnObject.java Method return type: public static BankAccount getAccount() { … return new BankAccount(balance); }

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #10 Returning Objects from Methods account = getAccount(); public static BankAccount getAccount() { … return new BankAccount(balance); } balance: address A BankAccount Object

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #11 The equals Method – Chapter 9.5 The FeetInches class has an equals method. If we try the following: FeetInches distance1 = new FeetInches(6, 5); FeetInches distance2 = new FeetInches(6, 5); if (distance1 == distance2) // This is a mistake. System.out.println("The objects are the same."); else System.out.println("The objects are not the same."); only the addresses of the objects are compared.

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #12 The equals Method Instead of using the == operator to compare two FeetInches objects, we should use the equals method. public boolean equals(FeetInches object2) { boolean status; if (feet == object2.getFeet() && inches == object2.getInches()) status = true; else status = false; return status; } Now, objects can be compared by their contents rather than by their memory addresses. Example: StockCompare.javaStockCompare.java