Presentation on theme: "The Random and String Classes The import statement."— Presentation transcript:
The Random and String Classes The import statement
Blanca Polo ICS111 2 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 3 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 4 Invoking Methods returnValue =Object.method(parameters); Will I always have a return value? Are parameters always present?
Blanca Polo ICS111 5 Aliases Two or more references that refer to the same object are called aliases of each other name2 = name1; name1 name2 Before: ”Tom Hanks" ”Kurt Russell" name1 name2 After: ”Tom Hanks"
Blanca Polo ICS111 6 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 7 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 8 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 9 Packages The classes of the Java standard class library are organized into packages Some of the packages in the standard class library 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
Blanca Polo ICS111 10 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 11 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 13 The Random Class The Random class is part of the java.util package It provides methods that generate pseudorandom numbers
Blanca Polo ICS111 14 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 15 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 See Java/Numbers/GenOneRandom.java Java/Numbers/OneRandom.java
Blanca Polo ICS111 16 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 17 String Indexes Helloworld 012345678910 String s = “Hello world”; The length of s is 11 It has positions from 0 to 10
Blanca Polo ICS111 18 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) See java/String/StringMethods.java
Your consent to our cookies if you continue to use this website.