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.

Slides:



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

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 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
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 
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.
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.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
Introduction to Java Programming, 4E Y. Daniel Liang.
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
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.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
CSC 1051 M.A. Papalaskari, Villanova University CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences.
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.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
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.
© 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.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS Lecture 4-1 Java's Work Horses.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
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.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Chapter 5: Enhancing Classes
Variables in Java A variable holds either
Formatting Output & Enumerated Types & Wrapper Classes
Using Classes and Objects
Creating Objects & String Class
Classes, Libraries & Packages
Using Classes and Objects
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
Chapter 6 Objects and Classes
Drawing Shapes (Graphics Class) Creating Objects Packages
Presentation transcript:

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 as a type to declare an object reference variable

3 Remember… Primitive Data Types X 5 Primitive Data Type int x = 5; A primitive data type variable contains the value itself, but an object variable contains the address of the object

4 References to Objects  An object reference variable holds the address of an object String s = “Hello”; //same as: String s = new String(“Hello”); s “Hello” Memory location An object variable contains the address of the object

5 Declaring the Object  The object itself must be created separately String s; s Memory location

6 Creating the Object itself  The object itself must be created separately String s; s = “Hello”; s “Hello” Memory location

7 Is this assignment? String s; String t; s = “hello”; t = s; s “Hello” Memory location t

8 Creating Objects, the formula  Generally, we use the new operator to create an object Object objectReference = new Object 0 or more Class name Class name parameters ( ); The identifier or variable name Specifications for the newly created object

9 Creating Objects  Generally, we use the new operator to create an object String name = new String (”Blanca J. Polo"); This keyword new calls the String constructor That is the one that creates/builds the object Creating an object is called instantiation An object is an instance of a particular class

10 Invoking Methods  We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods int nameLength = 0; nameLength = name.length()  A method may return a value  The returned value can be used in an assignment or expression

11 The main method is void A void method doesn’t return anything Return types can be of any Primitive Data Type Return types can be of any Object kind, either pre-made by JAVA or made by you

12 Review: The difference ”Blanca Polo" Srting name int num38

13 Primitive Data Type Assignment Review  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:

14 Object Assignment Review  For object references, assignment copies the address: name2 = name1; name1 name2 Before: ”Tom Hanks" ”Kurt Russell" name1 name2 After: ”Tom Hanks"

15 Aliases  Two or more references that refer to the same object are called aliases of each other  That creates an interesting situation: 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, because there is really only one object

16 Garbage Collection  When an 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, returning an object's memory to the system for future use  In other languages, the programmer is responsible for performing garbage collection

17 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  Other class libraries can be obtained through third party vendors, or you can create them yourself

18 What is in a Package  A package is a directory of files that contain utilities.  All classes of the java.lang package are imported automatically into all programs.  The String class, and the System.out class are part of this package.

19 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

20 The import Declaration  When you want to use a class from a package, you could use its fully qualified name java.util.Random  Or you can import the class, and then use just the class name import java.util.Random;  To import all classes in a particular package, you can use the * wildcard character import java.util.*;

21 The import Declaration  Import statements are always at the top of the program. import java.lang.*;  Many of the classes that we will use in this class will need to be imported.  Different JAVA programming environments have different libraries  We will use the standard libraries that can be found in any programming environment.

22 Counting

23 The Random Class  The Random class is part of the java.util package  It provides methods that generate pseudorandom numbers

24 Random Class Methods  Constructor: Random( ) Random r = new Random( ); float nextFloat( ) Returns a random number between 0.0 and 1.0 inclusive float f; f = r.nextFloat( );

25 More Random Class Methods int nextInt( ) Returns a random number that ranges over all possible int values positive and negative int i; i = r.nextInt( ); int nextInt( int num ) Returns a random number between 0 and num-1 (inclusive) int i; i = r.nextInt(5 ); // generates a number between 0 and 4 See Java/Numbers/GenOneRandom.java Java/Numbers/OneRandom.java

26 Questions

27 Questions?