Docsity

Object Creation, String Class, and Graphical Components in Java - Prof. James Cross, Study notes of Computer Science

Toggle sidebar
bg1
Slide 3 - 1
COMP 1210 – Fundamentals of Computing I
3. Using Classes and Objects
(Adapted from Lewis and Loftus text/slides)
•Chapter 3 focuses on:
object creation and object references
the String class and its methods
the Java standard class library
the Random and Math classes
formatting output
enumerated types [skip]
wrapper classes
graphical components and containers
labels and images
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download Object Creation, String Class, and Graphical Components in Java - Prof. James Cross and more Study notes Computer Science in PDF only on Docsity!

3. Using Classes and Objects^ (Adapted from Lewis and Loftus text/slides) •^ Chapter 3 focuses on:^ ƒ^ object creation and object references^ ƒ^ the^ String^ class and its methods^ ƒ^ the Java standard class library^ ƒ^ the^ Random^ and^ Math COMP 1210 – Fundamentals of Computing I

classes ƒ formatting output ƒ enumerated types [skip] ƒ wrapper classes ƒ graphical components and containers ƒ labels and images

Outline^ Creating Objects^ The String Class^ Packages^ Formatting Output^ Enumerated Types (skip)^ Wrapper Classes^ Components and Containers^ Images COMP 1210 – Fundamentals of Computing I

Creating Objects • Generally, we use the COMP 1210 – Fundamentals of Computing I

new^ operator to create an object^ title^ =^ new^ String

(“Intro^ to^ Computing"); This calls the Stringconstructor, 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

Invoking Methods • We've seen that once an object has beeninstantiated, we can use the COMP 1210 – Fundamentals of Computing I

dot operator^ to invoke its methods (e.g. the Scanner object)^ count = title.length(); • A method may^ return a value

, which can be used in an assignment or expression • A method invocation can be thought of asasking an object to perform a service (or, insome cases, tell us about itself)

Assignment Revisited • The act of assignment takes a copy of a valueand stores it in a variable • For primitive types: COMP 1210 – Fundamentals of Computing I

(^38) num1 (^) Before: (^96) num2 (^) num2 = num1;^38 num1 (^) After:^38 num

Reference Assignment • For object references, assignment copies theaddress:^ name1^ Before:^ name2^ name2^ =^ name1; COMP 1210 – Fundamentals of Computing I

"Steve^ Jobs" "Steve^ Wozniak" name1 name After:

"Steve^ Jobs"

Garbage Collection • When an object no longer has any validreferences to it, it can no longer be accessedby the program • The object is useless, and therefore is called garbage • Java performs^ automatic garbage collection periodically, returning an object's memory tothe system for future use • In other languages, the programmer isresponsible for performing garbage collection COMP 1210 – Fundamentals of Computing I

Outline^ Creating Objects^ The String Class^ Packages^ Formatting Output^ Enumerated Types (skip)^ Wrapper Classes^ Components and Containers^ Images COMP 1210 – Fundamentals of Computing I

String Methods • Once a String^ object has been created,neither its value nor its length can be changed • Thus we say that an object of the COMP 1210 – Fundamentals of Computing I

String^ class is^ immutable • However, several methods of the

String^ class return new^ String^ objects that are modifiedversions of the original • See the list of^ String

methods on page 119 and in Appendix M

String Indexes • It is occasionally helpful to refer to a particularcharacter within a string • This can be done by specifying the character'snumeric index • The indexes begin at zero in each string • In the string^ "Hello" COMP 1210 – Fundamentals of Computing I

, the character^ 'H'^ is at index 0 and the^ 'o'^ is at index 4 • See^ StringExample1.java

Q

Class Libraries • A class library^ is a collection of classes that wecan use when developing programs • The Java standard class library COMP 1210 – Fundamentals of Computing I

is part of any Java development environment • Its classes are not part of the Java languageper se, but we rely on them heavily • Various classes we've already used (

System, Scanner,^ String) are part of the Javastandard class library • Other class libraries can be obtained throughthird party vendors, or you can create themyourself

Packages • The classes of the Java standard class libraryare organized into^ COMP 1210 – Fundamentals of Computing I

packages

-^ Some of the packages in the standard classlibrary 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 Network communication Utilities XML document processing

-^ See jGRASP “Help” – “Java API”

The import Declaration • All classes of the^ java.lang COMP 1210 – Fundamentals of Computing I

package are imported automatically into all programs • It's as if all programs contain the followingline:^ import java.lang.*; • That's why we didn't have to import the System^ or^ String^ classes explicitly in earlierprograms • The^ Scanner^ class, on the other hand, is partof the^ java.util^ package, and therefore mustbe imported

The Random Class • The Random^ class is part of the COMP 1210 – Fundamentals of Computing I

java.util package • It provides methods that generatepseudorandom numbers • A^ Random^ object performs complicatedcalculations based on a

seed value^ to produce a stream of seemingly random values • See^ RandomNumbersExample.java