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.

Slides:



Advertisements
Similar presentations
1001ICT Programming 1 Semester 1, 2011 Lecture 6 Using Java Classes (Textbook, Chapter 3, Sections 3.2 to 3.7 ONLY)
Advertisements

The Random and String Classes The import statement.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
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. © 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.
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.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Outline Last Time: Creating Objects The GregorianCalendar Class.
Using Classes and Objects Chapters 3 Section 3.3 Packages Section 3.4 Random Class Section 3.5 Math Class Section 3.7 Enumerated Types Instructor: Scott.
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.
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
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.
Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara.
1 Objects and Primitive Data  Now we can explore some more fundamental programming concepts  Chapter 2 focuses on: predefined objects primitive data.
© 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.
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.
© 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.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
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.
CSCI 1100/1202 January 23, Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.
Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
ELC 312 Day 4. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Agenda Questions? Problem set one due Problem set two Posted in WebCT  Due Sept.
1 CMPT 126 Java Basics Using Classes and Objects.
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,
Chapter 2: Objects and Primitive Data Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
© 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
Outline Creating Objects The String Class Packages Formatting Output
Using Classes and Objects
Chapter 2: Objects and Primitive Data
Classes, Libraries & Packages
Chapter 3: Using Classes and Objects
Chapter 2: Objects and Primitive Data
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.
Chapter 2: Objects and Primitive Data
Packages & Random and Math Classes
Outline Creating Objects The String Class The Random and Math Classes
Chapter 2: Objects and Primitive Data
Chapter 2: Objects and Primitive Data
Presentation transcript:

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 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

2 Creating Objects  Generally, we use the new operator to create an object title = new String ("Java Software Solutions"); This calls the String constructor, which is a special method that sets up the object  Creating an object is called instantiation  An object is an instance of a particular class

3 Creating Objects  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  Once an object has been instantiated, we can use the dot operator to invoke its methods title.length()

4 String Methods  The String class has several methods that are useful for manipulating strings  Many of the methods return a value, such as an integer or a new String object

5 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 per se, but we rely on them heavily  The System class and the String class are part of the Java standard class library  Other class libraries can be obtained through third party vendors, or you can create them yourself

6 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 and components Network communication Utilities XML document processing

7 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.*;

8 The import Declaration  All classes of the java.lang package are imported automatically into all programs  That's why we didn't have to import the System or String classes explicitly in earlier programs  The Random class is part of the java.util package  It provides methods that generate pseudorandom numbers  See RanNums.java on wikiRanNums.java

9 Class Methods  Some methods can be invoked through the class name, instead of through an object of the class  These methods are called class methods or static methods  The Math class contains many static methods, providing various mathematical functions, such as absolute value, trigonometry functions, square root, etc. temp = Math.cos(90) + Math.sqrt(delta);

10 Formatting Output  The NumberFormat class has static methods that return a formatter object getCurrencyInstance() getPercentInstance()  Each formatter object has a method called format that returns a string with the specified information in the appropriate format  See Price.javaPrice.java

11 Formatting Output  The DecimalFormat class can be used to format a floating point value in generic ways  For example, you can specify that the number should be printed to three decimal places  The constructor of the DecimalFormat class takes a string that represents a pattern for the formatted number  See CircleStats.java (page 107)CircleStats.java