Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.

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

Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
3. Using Classes And Objects Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
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.
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.
1 A Sorting Example (a start for Lab 4 problem number 3)
Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus.
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.
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.
Class Library, Formatting, Wrapper Classes, and JUnit Testing
COS 312 DAY 3 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 DUE right now Assignment 2 Posted – Due Feb 5 prior to class Using Classes and Objects.
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.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
© 2007 Pearson Addison-Wesley. All rights reserved3-1 Using Classes and Objects We can create more interesting programs using predefined classes and related.
Topics Chapter 2: –Data conversion Chapter 3 –Object creation and object references –The String class and its methods –The Java standard class library.
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:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data Animated Version.
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.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
Using Java Class Library
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.
© 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.
Using Classes and Objects (Chapter 3) Copyright © 2012 Pearson Education, Inc.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.4 Using Java Built-in Classes Produced by.
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.
COS 312 DAY 3 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 posted – Due Feb 2PM Assignment 2 Posted – Due Feb 2 PM Finish Data and Expressions.
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.
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.
Chapter 3 Using Classes and Objects. 2 We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: If statements.
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.
Formatting Output & Enumerated Types & Wrapper Classes
Outline Creating Objects The String Class Packages Formatting Output
Using Classes and Objects
Using Classes and Objects (Chapter 3)
Classes, Libraries & Packages
Chapter 3: Using Classes and Objects
Chapter 3, cont Sept 20, 2004.
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Objects and Primitive Data
Outline Creating Objects The String Class The Random and Math Classes
Chapter 3 Using Classes and Objects
Presentation transcript:

Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

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 a special syntax that works only for strings Each string literal (enclosed in double quotes) Represents a String object

String class Once a String object is created Its value can not be lengthened or shortened Nor can any of its characters change => String objects are immutable However, several methods in String Allow to create new String objects As a result of the modification of the original String

The use of String class and its methods Refer to StringMutation.java Some methods exercised in the program Refer to the index of a particular character The index of the first character in a String is zero, The index of the next one is 1, and so on Example: “Hello” is a String object where the index of ‘H’ is zero and the character at index four is ‘o’

Some methods of the String class char charAt (int index) Char c; String S = “hi there”; c = S.charAt(0); // c = h boolean equals (String str) boolean equalsIgnoreCase(String str) Refer to StringMutations.java

Random number Random numbers are often needed when writing software Flight simulator use random numbers to determine how often simulated flights has engine trouble are generated in JAVA through the Random class Part of the java.util package Picks a number at random out of a range of values

Random class Methods of Random class float nextFloat() returns a random number  between 0.0 (inclusive) and 1.0 (exclusive) int nextInt() Returns a random number over all possible int value int nextInt(int num) Returns a random number in the range 0 to num-1 Refer to RandomNumbers.java

Math class provides a large number of basic mathematical functions Helpful in making calculations is defined in java.lang package includes static methods => methods can be invoked through the name of the class They can be used without having to instantiate an object Return values used in expressions as needed value = Math.cos(90) + Math.sqrt(delta);

Using Math class Sample program Use Math class to compute the roots of a quadratic equation  ax 2 + bx +c Algorithm Read values (a, b, and c) Evaluate the roots of the equation Refer to Quadratic.java

Some methods of the Math class static int abs(int num) The absolute value of num static double cos(double angle) Returns the angle (in radians) cosine static double exp(double power) Returns the value of e raised to the specified power

Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

Formatting output It is often necessary to format values output looks appropriate when printed or displayed NumberFormat part of java.text package Provides generic formatting capabilities is not instantiated using the new operator instead by requesting an object From one of the static methods invoked thru the class name  NumberFormat fmt = NumberFormat.getCurrencyInstance();

Creating NumberFormat instance NumberFormat objects are created using getCurrencyInstance() invoked thru class name  returns a formatter for monetary values getPercentInstance() invoked thru class name  returns an object that formats a percentage are used to format numbers using method format() Refer to Purchase.java NumberFormat fmt = NumberFormat.getCurrencyInstance() double subtotal=19.35; System.out.println(fmt.format(subtotal) ); Output: $19.35

DecimalFormat class DecimalFormat part of java.text package allows to format values based on a pattern To determine how many digits should be printed  To the right of the decimal point (for instance) is instantiated in the traditional way using the new operator Its constructor DecimalFormat takes a String  That represents a pattern for the formatted number Refer to CircleStats.java

Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

Enumerated types Java allows you to define an enumerated type Which can then be used to declare variables as the type of a variable establishes all possible values for a variable of that type By listing, or enumerating the values Where the values are identifiers, and can be anything desired  enum Season {winter, spring, summer, fall} There is no limit to the number of listed values  Any number of values can be listed

Declaring and using an enumerated type Once a type is defined A variable of that type can be declared enum Grade {A, B, C, D, F}; Grade score; And it can be assigned a value Thru the name of the type score = Grade.A; Enumerated types are type-safe You cannot assign any value other than those listed

Ordinal values Internally, each value of an enumerated type is stored as an integer, called its ordinal value The first value has an ordinal value of zero The second one, and so on You cannot assign a numeric value to enumerated type, even if it corresponds to an ordinal value

Enumerated types: methods The declaration of an enumerated type is a special type of class And each variable of that type is an object methods associated with enumerated objects The ordinal() method returns the numeric value Of an enumerated type The name() returns the name of the value Refer to IceCream.java

Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

Wrapper classes A wrapper class is used to wrap a primitive value into an object Ex: create an object that serves as a container to hold an int represents a particular primitive type Ex: Integer class represents a simple integer value instantiated, stores a single primitive type value Ex: Integer object store a single int value its constructor accept the primitive value to store Ex: Integer ageObj = new Integer (40);

Wrapper classes in the JAVA class library For each primitive type in JAVA There exists a corresponding wrapper class (java.lang) Primitive typeWrapper class byteByte shortShort intInteger longLong floatFloat doubleDouble charCharacter booleanBoolean

Wrapper classes methods manages the associated primitive type Ex: Integer provides methods returning the int stored in it Some methods of the Integer class Integer (int value) Constructor: creates an new Integer object storing value float floatValue() returns the value of this integer as the float type static int parseInt (String str) Returns the int corresponding to the value in str

Autoboxing/Unboxing Autoboxing is the automatic conversion between Primitive value and corresponding wrapper object Integer obj1; int num1 = 69; Obj1 = num1; // automatically creates an Integer object Unboxing is the reverse condition Integer obj2 = new Integer (69); int num2; num2 = Obj2; // automatically extracts the int value Refer to wrapper_error.java