© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Chapter 4: Writing Classes
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes, Encapsulation, Methods and Constructors
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
ELC 312 Day 6. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Agenda Questions? Problem set two Posted in WebCT  Due Sept 30  On Page 153&154.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved2/48 Writing Classes We've been using predefined classes. Now we will learn.
Writing Classes (Chapter 4)
Objects and Classes. Problem Solving The key to designing a solution is breaking it down into manageable pieces When writing software, we design separate.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
SEEM Java – Basic Introduction, Classes and Objects.
Wednesday –POD –I have updated grades in powerschool. If you have a zero for a lab grade, it probably means you didn’t DropItToMe. Please do so. –Slides.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Outline Anatomy of a Class Encapsulation Anatomy of a Method Copyright © 2014 Pearson Education, Inc.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Variables in Java A variable holds either
Chapter 4: Writing Classes
Writing Classes Chapter 4.
Writing Classes We've been using predefined classes from the Java API. Now we will learn to write our own classes. Chapter 4 focuses on: class definitions.
Creating Objects & String Class
Chapter 4: Writing Classes
Chapter 4 Writing Classes.
Chapter 4 Writing Classes.
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Chapter 4 Writing Classes.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 4 Writing Classes
Anatomy of a Method.
Writing Classes.
Chapter 4 Writing Classes.
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Packages & Random and Math Classes
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
4 Writing Classes Software Solutions Lewis & Loftus java 5TH EDITION
Presentation transcript:

© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just reference (holds the address)  No object is created with this declaration Creation/Instantiation: title = new String (“Cin Ali");  title (object) is an instance of String (class)  BTW, only for strings, title=“Cin Ali” was enough Call Method : Dot operator count = title.length();

© 2004 Pearson Addison-Wesley. All rights reserved3-2 References A primitive variable contains the value itself, but an object variable contains the address of the object An object reference can be thought of as a pointer to the location of the object "Steve Jobs" name1 num1 38

© 2004 Pearson Addison-Wesley. All rights reserved3-3 Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After:

© 2004 Pearson Addison-Wesley. All rights reserved3-4 Reference Assignment For object references, assignment copies the address: Changing an object through one reference changes it for all name2 = name1; name1 name2 Before: "Steve Jobs" "Steve Wozniak" name1 name2 After: "Steve Jobs"

© 2004 Pearson Addison-Wesley. All rights reserved3-5 Garbage Collection When an object no longer has any valid references to it, it can no longer be accessed Java performs automatic garbage collection periodically, returning an object's memory to the system for future use In other languages, the programmer is responsible for performing garbage collection

© 2004 Pearson Addison-Wesley. All rights reserved3-6 Class Libraries Predefined classes - libraries Java standard class library is part of any Java development environment  Various classes we've already used ( System, Scanner, String ) are part of the Java standard class library It has packages Package java.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support (automatically imported) Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing

© 2004 Pearson Addison-Wesley. All rights reserved3-7 The import Declaration To use a class from a package, you could use its fully qualified name java.util.Scanner Or import the class, and then use just the class name import java.util.Scanner; To import all classes in a particular package, use the * wildcard character import java.util.*;

© 2004 Pearson Addison-Wesley. All rights reserved3-8 The import Declaration java.lang package (e.g., System, String classes ) are imported automatically into all programs.  No need to say: import java.lang.*; The Scanner class is in java.util, and therefore must be imported Random class in java.util Math class in java.lang

© 2004 Pearson Addison-Wesley. All rights reserved4-9 Classes and Objects Die class - t he blueprint for a die object  We can then instantiate many die objects State: which face is showing  private int faceValue A behavior: it can be rolled  roll method that assigns a random value to faceValue Die constructor sets faceValue of each new die object to 1 Other methods (behaviors) that might be useful See RollingDice.java (page 157)RollingDice.java See Die.java (page 158)Die.java

© 2004 Pearson Addison-Wesley. All rights reserved4-10 Data Scope The scope of data is the area in a program in which that data can be referenced (used) Data declared at the class level can be referenced by all methods in that class Data declared within a method (local data) can be used only in that method faceValue is instance data  each instance (object) has its own version

© 2004 Pearson Addison-Wesley. All rights reserved4-11 Instance Data A class declares the type of the data, but it does not reserve any memory space for it Every time a Die object is created, a new faceValue variable is created as well The objects of a class share the method definitions, but each object has its own data space That's the only way two objects can have different states

© 2004 Pearson Addison-Wesley. All rights reserved4-12 Instance Data We can depict the two Die objects from the RollingDice program as follows: die1 5 faceValue die2 2 faceValue Each object maintains its own faceValue variable, and thus its own state

© 2004 Pearson Addison-Wesley. All rights reserved4-13 Encapsulation We can take one of two views of an object:  internal - the details of the variables and methods of the class that defines it  external - the services that an object provides and how the object interacts with the rest of the system From the external view, an object is an encapsulated entity, providing a set of specific services These services define the interface to the object

© 2004 Pearson Addison-Wesley. All rights reserved4-14 Encapsulation One object (called the client) may use another object for the services it provides The client of an object may request its services (call its methods), but it should not have to be aware of how those services are accomplished Any changes to the object's state (its variables) should be made by that object's methods We should make it difficult, if not impossible, for a client to access an object’s variables directly That is, an object should be self-governing

© 2004 Pearson Addison-Wesley. All rights reserved4-15 Encapsulation An encapsulated object can be thought of as a black box -- its inner workings are hidden from the client The client invokes the interface methods of the object, which manages the instance data Methods Data Client

© 2004 Pearson Addison-Wesley. All rights reserved4-16 Visibility Modifiers for Encapsulation public protected private  can be referenced only within that class public variables violate encapsulation  clients modify the values directly!!  Instance variables should not be declared public Service methods are public (for clients) Support methods are not public (for service methods)

© 2004 Pearson Addison-Wesley. All rights reserved4-17 Accessors and Mutators Because instance data is private, a class usually provides services to access and modify data values Accessor method returns the current value of a variable ( getX, where X is the name of the value) Mutator method changes the value of a variable ( setX)

© 2004 Pearson Addison-Wesley. All rights reserved4-18 Method Body The method header is followed by the method body char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } The return expression must be consistent with the return type sum and result are local data They are created each time the method is called, and are destroyed when it finishes executing

© 2004 Pearson Addison-Wesley. All rights reserved4-19 Parameters When a method is called, the actual parameters in the invocation are copied into the formal parameters in the method header char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } ch = obj.calc (3, count, "Hello");

© 2004 Pearson Addison-Wesley. All rights reserved4-20 Local Data Local variables inside a method are destroyed when it finishes Instance variables, declared at the class level, exists as long as the object exists

© 2004 Pearson Addison-Wesley. All rights reserved4-21 Bank Account Example Let’s look at another example that demonstrates the implementation details of classes and methods We’ll represent a bank account by a class named Account It’s state can include the account number, the current balance, and the name of the owner An account’s behaviors (or services) include deposits and withdrawals, and adding interest

© 2004 Pearson Addison-Wesley. All rights reserved4-22 Driver Programs A driver program drives the use of other, more interesting parts of a program Driver programs are often used to test other parts of the software The Transactions class contains a main method that drives the use of the Account class, exercising its services See Transactions.java (page 172)Transactions.java See Account.java (page 173)Account.java

© 2004 Pearson Addison-Wesley. All rights reserved4-23 Bank Account Example acct acctNumber balance name “Ted Murphy” acct acctNumber balance name “Jane Smith”

© 2004 Pearson Addison-Wesley. All rights reserved4-24 Constructors Revisited Note that a constructor has no return type specified in the method header, not even void A common error is to put a return type on a constructor, which makes it a “regular” method that happens to have the same name as the class The programmer does not have to define a constructor for a class Each class has a default constructor that accepts no parameters