Download presentation
Presentation is loading. Please wait.
Published byWendy Floyd Modified over 9 years ago
1
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes
2
Chapter 3 - Java Programming With Supplied Classes2 Topics Packages and classes supplied w/ JDK The String class and its methods Declaring and accessing a String array The Vector, Calendar, and Date classes Data wrapper classes Writing and executing an applet Controlling font and color
3
Chapter 3 - Java Programming With Supplied Classes3 Using the Packages and Classes Supplied with Java Java Development Kit (JDK) –JDK 1.4 Consists of 135 packages, 2,991 pre-defined classes and interfaces ~ JDK 1.3: 76 and 1,842 –Package –Group of related classes (class library) –Keywords import –Gives the compiler access to the classes package –Assign classes to a particular package
4
Chapter 3 - Java Programming With Supplied Classes4
5
5 Using the String Class String Class –Member of java.lang package Automatically imported by Java compiler –Two ways to instantiate a String object Similar to primitives: –String a = Hello World”; Use the keyword new: –String s = new String(“Hello World”);
6
Chapter 3 - Java Programming With Supplied Classes6
7
7 Using the String Class Method types –Instance methods (nonstatic) Associated with a specific instance of the class Use reference variable to invoke e.g., s.length() –Class methods (static) Not associated with any instance Use class name to invoke Contain keyword static in their headers e.g., Student.getTotalNumberOfStudents()
8
Chapter 3 - Java Programming With Supplied Classes8
9
9 Using the String Class NullPointerException –Results from attempting to invoke an instance method using a reference variable that hasn’t been initialized Immutable –Refers to the fact that Java Strings cannot be changed Methods that “change” a string value actually return a new String instance
10
Chapter 3 - Java Programming With Supplied Classes10 Creating a String Array Array declaration –String stringArray[] = new String[4]; Creates array of 4 String objects Array elements are String objects Array element instantiation –stringArray[0] = new String(“Hello”); Must be performed for each element
11
Chapter 3 - Java Programming With Supplied Classes11
12
Chapter 3 - Java Programming With Supplied Classes12 Creating a String Array Comparing String objects –Reference variables contain references, not data values –Cannot use comparison operator (==) –Must use String methods: equals equalsIgnoreCase
13
Chapter 3 - Java Programming With Supplied Classes13 Using the Vector Class Vector Class –Contained in java.util package must be imported –Array that is dynamically resizable Can contain different class data types Cannot contain primitive data types (need wrapper) –Declaration Vector v = new Vector(3); –Creates vector with three elements
14
Chapter 3 - Java Programming With Supplied Classes14
15
Chapter 3 - Java Programming With Supplied Classes15 Working With Dates Classes for Working with Date Values –In java.util package: Calendar Class –Contains methods and constants Date Class –An instance contains the actual date value
16
Chapter 3 - Java Programming With Supplied Classes16 Working With Dates Classes for Working with Date Values –In java.text package: DateFormat Class –An instance provides several data formats for display purposes
17
Chapter 3 - Java Programming With Supplied Classes17 Wrapper Classes –Contains primitive data inside an object instance –Reside in java.lang package –Named same as primitive counterpart with the first letter capitalized e.g., double Double, float Float, etc. Except for Integer: int Integer Using Wrapper Classes
18
Chapter 3 - Java Programming With Supplied Classes18 Using Wrapper Classes Converting Primitive to Wrapper and Back –Primitive to wrapper: Instantiate the appropriate wrapper class using the primitive variable as the argument Double d; Double doubleWrapper = new Double( d ); –Wrapper to primitive: Use instance method named xxxValue (where xxx is the primitive data type) e.g., d = doubleWrapper.doubleValue( );
19
Chapter 3 - Java Programming With Supplied Classes19
20
Chapter 3 - Java Programming With Supplied Classes20 Using Wrapper Classes Converting String to Primitive and Back –String to primitive : Use instance method named parsexxx (where xxx is the primitive data type) –String s1 =new String(“2.2”); –doublePrimitive = s.parseDouble(s1); –Wrapper to primitive: Use instance method named toString that creates a String instance containing the primitive value –String s2 = Double.toString(doublePrimitive);
21
Chapter 3 - Java Programming With Supplied Classes21 Using Wrapper Classes Converting String to Wrapper and Back –String to wrapper: Use static wrapper method named valueOf that creates a wrapper instance from String instance –doubleWrapper = Double.valueof(s1); –Wrapper to String: Use wrapper method named toString that creates a String instance from the wrapper instance –s1 = doubleWrapper.toString( );
22
Chapter 3 - Java Programming With Supplied Classes22 Using the Applet Class Writing a Simple Applet –Import: Graphics Class from java.awt package Applet Class from java.applet package –Subclass of Panel: »GUI window without a title bar –Executed in a browser window Requires HTML file
23
Chapter 3 - Java Programming With Supplied Classes23
24
Chapter 3 - Java Programming With Supplied Classes24
25
Chapter 3 - Java Programming With Supplied Classes25 Using the Applet Class Controlling Color and Font –Font Class from java.awt package Specifies font name, style, and size –Color Class from java.awt package Specifies colors –Wild card character Used with import statements to specify multiple classes import java.awt.*
26
Chapter 3 - Java Programming With Supplied Classes26
27
Chapter 3 - Java Programming With Supplied Classes27
28
Chapter 3 - Java Programming With Supplied Classes28
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.