Chapter 3, cont Sept 20, 2004.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

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.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
Static Class Members Wrapper Classes Autoboxing Unboxing.
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.
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.
MATH AND RANDOM CLASSES.  The need for random numbers occurs frequently when writing software.  The Random class, which is part of the java.util class,
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.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
Using Java Class Library
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.
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.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
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.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
Wrapper Classes  Java offers a convenient way to incorporate, or wrap, a primitive data type into an object, for example, wrapping int into the class.
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.
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
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.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Formatting Output & Enumerated Types & Wrapper Classes
Static Members and Methods
Using Classes and Objects
Functions, Part 2 of 2 Topics Functions That Return a Value
CSC 1051 – Data Structures and Algorithms I
Data Conversion & Scanner Class
Using Classes and Objects (Chapter 3)
Classes, Libraries & Packages
Chapter 3: Using Classes and Objects
CMSC 202 Static Methods.
Unit-2 Objects and Classes
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Using Classes and Objects
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
The Random Class The Random class is part of the java.util package
Packages & Random and Math Classes
Classes and Objects Static Methods
Outline Creating Objects The String Class The Random and Math Classes
Chapter 2: Java Fundamentals cont’d
Chapter 6 Dynamic Programming.
Chapter 3 Using Classes and Objects
Chapter 2 Reference Types.
Chapter 4 Greedy Algorithms.
Presentation transcript:

Chapter 3, cont Sept 20, 2004

Reminder – Hardcopy due today! In class – Stapled. Anything turned in after lecture will be considered 10 points late. Late submissions are turned in timely if you get them in by the next class day after your successful run. Any late penalty points on the submission still apply. © 2004 Pearson Addison-Wesley. All rights reserved

Today Review exercise on terminology. Work alone for 10 minutes. You may use your book. Work with a partner when announced. 10 minutes. Work with another group for the remaining 5 minutes. © 2004 Pearson Addison-Wesley. All rights reserved

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 See RandomNumbers.java (page 126) – book © 2004 Pearson Addison-Wesley. All rights reserved

The Math Class The Math class is part of the java.lang package The Math class contains methods that perform various mathematical functions These include: absolute value square root exponentiation trigonometric functions © 2004 Pearson Addison-Wesley. All rights reserved

value = Math.cos(90) + Math.sqrt(delta); The Math Class The methods of the Math class are static methods (also called class methods) Static methods can be invoked through the class name – no object of the Math class is needed value = Math.cos(90) + Math.sqrt(delta); This means that we are dealing with only one thing; we will not have different “Maths” in our program. Math class provides us with services that we can use with other programs and does not define a reference type. See Quadratic.java (page 129) - examples We discuss static methods further in Chapter 6 © 2004 Pearson Addison-Wesley. All rights reserved

Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean void Void © 2004 Pearson Addison-Wesley. All rights reserved

Integer age = new Integer(40); Wrapper Classes The following declaration creates an Integer object which represents the integer 40 as an object Integer age = new Integer(40); An object of a wrapper class can be used in any situation where a primitive value will not suffice For example, some objects serve as containers of other objects Primitive values could not be stored in such containers, but wrapper objects could be © 2004 Pearson Addison-Wesley. All rights reserved

num = Integer.parseInt(str); Wrapper Classes Wrapper classes also contain static methods that help manage the associated type For example, the Integer class contains a method to convert an integer stored in a String to an int value: num = Integer.parseInt(str); The wrapper classes often contain useful constants as well For example, the Integer class contains MIN_VALUE and MAX_VALUE which hold the smallest and largest int values © 2004 Pearson Addison-Wesley. All rights reserved

Some of you may have seen Keyboard class If you input a bad value the system will give you a message about the Min_Value being used. That value is found in the corresponding wrapper class for the return type of the method. © 2004 Pearson Addison-Wesley. All rights reserved

In Chapter 3 We are not covering Enumerated Types or the Graphical chapters. © 2004 Pearson Addison-Wesley. All rights reserved