Download The Random and String Classes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
The
Random
and
String
Classes
The import statement
Creating Objects
A variable holds either a primitive type or a reference to an
object
Object objectName = new Object (0 or more parameters);
Object o;
In the above declaration, what will be the value of o?
Object x;
x=o;
Explain the above situation…
Blanca Polo
ICS111
2
Question
Object o;
In the above declaration, what will be the value of o?
Object x;
x=o;
Explain the above situation…
Blanca Polo
ICS111
3
Invoking Methods
returnValue =Object.method(parameters);
Will I always have a return value?
Are parameters always present?
Blanca Polo
ICS111
4
Aliases
• Two or more references that refer to the same object are
called aliases of each other
Before:
name1
”Tom Hanks"
name2
”Kurt Russell"
name2 = name1;
”Tom Hanks"
name1
After:
name2
Blanca Polo
ICS111
5
Garbage Collection
• When an object no longer has any valid references to it, it
can no longer be accessed by the program
• The object is useless, and therefore is called garbage
• Java performs automatic garbage collection periodically,
returning an object's memory to the system for future use
• In other languages, the programmer is responsible for
performing garbage collection
Blanca Polo
ICS111
6
Class Libraries
• A class library is a collection of classes that we can use
when developing programs
• The Java standard class library is part of any Java
development environment
• Other class libraries can be obtained through third party
vendors, or you can create them yourself
Blanca Polo
ICS111
7
What is in a Package
• A package is a directory of files that contain utilities.
• All classes of the java.lang package are imported
automatically into all programs.
• The String class, and the System.out class are part of this
package.
Blanca Polo
ICS111
8
Packages
• The classes of the Java standard class library are
organized into packages
• Some of the packages in the standard class library are:
Package
Purpose
java.lang
java.applet
java.awt
javax.swing
java.net
java.util
javax.xml.parsers
General support
Creating applets for the web
Graphics and graphical user interfaces
Additional graphics capabilities
Network communication
Utilities
XML document processing
Blanca Polo
ICS111
9
The import Declaration
• When you want to use a class from a package, you could
use its fully qualified name
java.util.Random
• Or you can import the class, and then use just the class
name
import java.util.Random;
• To import all classes in a particular package, you can use
the * wildcard character
import java.util.*;
Blanca Polo
ICS111
10
The import Declaration
• Import statements are always at the top of the program.
import java.lang.*;
• Many of the classes that we will use in this class will need
to be imported.
• Different JAVA programming environments have different
libraries
• We will use the standard libraries that can be found in any
programming environment.
Blanca Polo
ICS111
11
Counting
Blanca Polo
ICS111
12
The Random Class
• The Random class is part of the java.util package
• It provides methods that generate pseudorandom
numbers
Blanca Polo
ICS111
13
Random Class Methods
• Constructor:
Random( )
Random r = new Random( );
• float nextFloat( )
Returns a random number between
0.0 and 1.0 inclusive
float f;
f = r.nextFloat( );
Blanca Polo
ICS111
14
More Random Class Methods
• int nextInt( )
Returns a random number that
ranges over all possible int values
positive and negative
int i;
i = r.nextInt( );
• int nextInt( int num )
Returns a random number between
0 and num-1 (inclusive)
int i;
i = r.nextInt(5 );
// generates a number between 0 and 4
Blanca Polo
ICS111
See
15
Java/Numbers/GenOneRandom.java
Java/Numbers/OneRandom.java
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 String class is immutable
• However, several methods of the String class return
new String objects that are modified versions of the
original
Blanca Polo
ICS111
16
String Indexes
String s = “Hello world”;
H
0
e
1
l
2
l
3
o
4
5
w
6
o
7
r
8
l
9
d
10
•The length of s is 11
•It has positions from 0 to 10
Blanca Polo
ICS111
17
String Methods
String toUpperCase( )
String toLowerCase( )
int length( )
String substring(int begin)
String substring(int begin, int end)
String replace( char oldChar, char newChar)
String trim( )
int indexOf(char anyChar)
int indexOf(String anyString)
char charAt(int position)
String concat(String anotherString)
Blanca Polo
ICS111
See java/String/StringMethods.java
18
Questions?
Blanca Polo
ICS111
19