ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.

Slides:



Advertisements
Similar presentations
The Random and String Classes The import statement.
Advertisements

Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”
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.
Chapter Day 6. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 DUE Problem set 2 posted 
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
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.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
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.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
Class Library, Formatting, Wrapper Classes, and JUnit Testing
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Chapter 3 Using Classes and Objects. Chapter Scope Creating objects Services of the String class The Java API class library The Random and Math classes.
Objects and Classes. Problem Solving The key to designing a solution is breaking it down into manageable pieces When writing software, we design separate.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer.
CSC 1051 M.A. Papalaskari, Villanova University CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
CSE 1201 Object Oriented Programming Using Classes and Objects.
SEEM Java – Basic Introduction, Classes and Objects.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
1 Wrapper Classes  Sometimes we want to use a primitive type as an object, so there are wrapper classes that will help us.  In particular, we need to.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
3-1 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
1 CMPT 126 Java Basics Using Classes and Objects.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Variables in Java A variable holds either
Formatting Output & Enumerated Types & Wrapper Classes
Using Classes and Objects
Creating Objects & String Class
Classes, Libraries & Packages
Chapter 3: Using Classes and Objects
Using Classes and Objects
Objects and Primitive Data
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.
Packages & Random and Math Classes
Outline Creating Objects The String Class The Random and Math Classes
String Objects & its Methods
Presentation transcript:

ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects

ECE122 L4: Creating Objects February 8, 2007 Outline °Problem: How do I create multiple objects from a class Java provides a number of ‘built-in’ classes for us °Understanding referencing Requires understanding of how computer memory works °Garbage collection Java automatically “cleans up unused items” °Generating random numbers in Java programs Why is this important?

ECE122 L4: Creating Objects February 8, 2007 Objects in the computer °Abstractions for the parts of a problem that matter °Objects contain data items Data description: data value My car has a model year, an integer, with value 1999 Data values are stored in variables Variables storing the data values for a particular object are the object’s instance variables The values of the object’s instance variables form the object’s state

ECE122 L4: Creating Objects February 8, 2007 Classes °Class: a template for creating objects Bank account class String class Student class °Every Java object is an instance of some class °The class of an object determines its data description and functionality

ECE122 L4: Creating Objects February 8, 2007 English to Java dictionary °Piece of data: value °Kind of data: type °Place to store a value: variable °Structured group of variables: object °Kind of object: class °Object of a particular class: class instance

ECE122 L4: Creating Objects February 8, 2007 Creating Objects °A variable holds either a primitive type or a reference to an object °A class name can be used as a type to declare an object reference variable String title; °No object is created with this declaration °An object reference variable holds the address of an object °The object itself must be created separately

ECE122 L4: Creating Objects February 8, 2007 Creating Objects °Generally, we use the new operator to create an object title = new String ("Java Software Solutions"); °Creating an object is called instantiation °An object is an instance of a particular class

ECE122 L4: Creating Objects February 8, 2007 Invoking Methods °We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods count = title.length() °A method may return a value, which can be used in an assignment or expression °A method invocation can be thought of as asking an object to perform a service

ECE122 L4: Creating Objects February 8, 2007 Reference Variables H e l l o. °What happens when we create a string? String testStr = new String ( " Hello."); 1.Locations are allocated in memory for the string 2.8 bit characters fill up the location in the memory 3.Address of first character saved in another location called ‘testStr” So, we can say that “testStr” is a pointer to a series of characters in memory

ECE122 L4: Creating Objects February 8, 2007 Reference Variables H e l l o testStr

ECE122 L4: Creating Objects February 8, 2007 References °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 °Rather than dealing with arbitrary addresses, we often depict a reference graphically "Steve Jobs" name1 num1 38

ECE122 L4: Creating Objects February 8, 2007 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:

ECE122 L4: Creating Objects February 8, 2007 Reference Assignment °For object references, assignment copies the address: name2 = name1; name1 name2 Before: "Steve Jobs" "Steve Wozniak" name1 name2 After: "Steve Jobs"

ECE122 L4: Creating Objects February 8, 2007 Aliases °Two or more references that refer to the same object are called aliases of each other °One object can be accessed using multiple reference variables °Aliases can be useful, but should be managed carefully °Changing an object through one reference changes it for all of its aliases There is really only one object

ECE122 L4: Creating Objects February 8, 2007 Classes °A class can contain data declarations and method declarations int acctNumber; float acctBalance; Data declarations Method declarations

ECE122 L4: Creating Objects February 8, 2007 Bank Account Example acct acctNumber balance acct acctNumber balance

ECE122 L4: Creating Objects February 8, 2007 Garbage Collection °If object no longer has any valid references to it It can no longer be accessed by the program °The object is useless, and therefore is called garbage °Java performs automatic garbage collection periodically Returns an object's memory to the system for future use °In other languages, the programmer is responsible for performing garbage collection

ECE122 L4: Creating Objects February 8, 2007 The String Class °Because strings are so common, we don't have to use the new operator to create a String object title = "Java Software Solutions"; °This is special syntax that works only for strings °Each string literal (enclosed in double quotes) represents a String object title = new String(“Java Software Solutions”); alternative

ECE122 L4: Creating Objects February 8, 2007 String Methods °Once a String object has been created, neither its value nor its length can be changed °Thus we say that an object of the String class is immutable °Several methods of the String class return new String objects They are modified versions of the original

ECE122 L4: Creating Objects February 8, 2007 String Class

ECE122 L4: Creating Objects February 8, 2007 String Indexes °It is occasionally helpful to refer to a particular character within a string °This can be done by specifying the character's numeric index °The indexes begin at zero in each string °In the string "Hello", the character 'H' is at index 0 and the 'o' is at index 4

ECE122 L4: Creating Objects February 8, 2007 Class Libraries °A class library is a collection of classes that we can use when developing programs °The Java standard class library is part of any Java development environment °Its classes are not part of the Java language, but we rely on them heavily °Various classes we've already used ( System, Scanner, String ) are part of the Java standard class library °Other class libraries can be obtained through third party vendors, or you can create them yourself

ECE122 L4: Creating Objects February 8, 2007 Packages °The classes of the Java standard class library are organized into packages °Some of the packages in the standard class library are: Package java.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing

ECE122 L4: Creating Objects February 8, 2007 The import Declaration °All classes of the java.lang package are imported automatically into all programs °It's as if all programs contain the following line: import java.lang.*; °That's why we didn't have to import the System or String classes explicitly in earlier programs

ECE122 L4: Creating Objects February 8, 2007 Random Numbers Streams of numbers that have been produced by a process that is believed to be random Truly random numbers. One example might be the spinning of a roulette wheel. Most truly random activities involve some sort of physical process which is difficult to replicate Unfortunately, computers are not very effective at generating truly random numbers Use pseudorandom number generator

ECE122 L4: Creating Objects February 8, 2007 The Random Class °The Random class is part of the java.util package °It provides methods that generate pseudorandom numbers °A Random object performs complicated calculations Based on a seed value to produce a stream of seemingly random values

ECE122 L4: Creating Objects February 8, 2007 java.util.Random °The java.util.Random class — a more controlled way to generate random numbers. °If we set the same “seed,” we get the same “random” sequence. Random generator = new Random(); Random generator2 = new Random(seed); long seed;

ECE122 L4: Creating Objects February 8, 2007 java.util.Random °Methods: int k = generator.nextInt (); int k = generator.nextInt (n); double x = generator.nextDouble (); All 2 32 possible int values are produced with (approximately) equal probability 0  k < n 0  x < 1

ECE122 L4: Creating Objects February 8, 2007 Summary °Very important to understand the difference between objects and classes °The “new” keyword creates (instantiates) objects in memory. A reference (pointer) is used to indicate the location of the object Note that there may be multiple references to the same object °Strings are “immutable”. They cannot be lengthened once they are “instantiated” °Random number are useful. Java provides a class which generates pseudorandom numbers