Chapter 3 Java Programming With Supplied Classes Chapter 3 - Java Programming With Supplied Classes 1 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 Chapter 3 - Java Programming With Supplied Classes 2 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 Chapter 3 - Java Programming With Supplied Classes 3 Chapter 3 - Java Programming With Supplied Classes 4 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”); Chapter 3 - Java Programming With Supplied Classes 5 Chapter 3 - Java Programming With Supplied Classes 6 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() Chapter 3 - Java Programming With Supplied Classes 7 Chapter 3 - Java Programming With Supplied Classes 8 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 Chapter 3 - Java Programming With Supplied Classes 9 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 Chapter 3 - Java Programming With Supplied Classes 10 Chapter 3 - Java Programming With Supplied Classes 11 Creating a String Array • Comparing String objects – Reference variables contain references, not data values – Cannot use comparison operator (==) – Must use String methods: • equals • equalsIgnoreCase Chapter 3 - Java Programming With Supplied Classes 12 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 Chapter 3 - Java Programming With Supplied Classes 13 Chapter 3 - Java Programming With Supplied Classes 14 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 Chapter 3 - Java Programming With Supplied Classes 15 Working With Dates • Classes for Working with Date Values – In java.text package: • DateFormat Class – An instance provides several data formats for display purposes Chapter 3 - Java Programming With Supplied Classes 16 Using Wrapper Classes • 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 Chapter 3 - Java Programming With Supplied Classes 17 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( ); Chapter 3 - Java Programming With Supplied Classes 18 Chapter 3 - Java Programming With Supplied Classes 19 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); Chapter 3 - Java Programming With Supplied Classes 20 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( ); Chapter 3 - Java Programming With Supplied Classes 21 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 Chapter 3 - Java Programming With Supplied Classes 22 Chapter 3 - Java Programming With Supplied Classes 23 Chapter 3 - Java Programming With Supplied Classes 24 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.* Chapter 3 - Java Programming With Supplied Classes 25 Chapter 3 - Java Programming With Supplied Classes 26 Chapter 3 - Java Programming With Supplied Classes 27 Chapter 3 - Java Programming With Supplied Classes 28