The Random and String Classes The import statement
- Slides: 19
The Random and String Classes The import statement Blanca Polo ICS 111
Creating Objects A variable holds either a primitive type or a reference to an object Object object. Name = 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 ICS 111 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 ICS 111 3
Invoking Methods return. Value =Object. method(parameters); Will I always have a return value? Are parameters always present? Blanca Polo ICS 111 4
Aliases • Two or more references that refer to the same object are called aliases of each other Before: name 1 ”Tom Hanks" name 2 ”Kurt Russell" name 2 = name 1; After: name 1 ”Tom Hanks" name 2 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 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 ICS 111 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 ICS 111 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 ICS 111 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 ICS 111 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 ICS 111 11
Counting Blanca Polo ICS 111 12
The Random Class • The Random class is part of the java. util package • It provides methods that generate pseudorandom numbers Blanca Polo ICS 111 13
Random Class Methods • Constructor: Random( ) Random r = new Random( ); • float next. Float( ) Returns a random number between 0. 0 and 1. 0 inclusive float f; f = r. next. Float( ); Blanca Polo ICS 111 14
More Random Class Methods • int next. Int( ) Returns a random number that ranges over all possible int values positive and negative int i; i = r. next. Int( ); • int next. Int( int num ) Returns a random number between 0 and num-1 (inclusive) int i; i = r. next. Int(5 ); // generates a number between 0 and 4 Blanca Polo ICS 111 See 15 Java/Numbers/Gen. One. Random. java Java/Numbers/One. Random. 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 ICS 111 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 ICS 111 17
String Methods üString to. Upper. Case( ) üString to. Lower. Case( ) üint length( ) üString substring(int begin, int end) üString replace( char old. Char, char new. Char) üString trim( ) üint index. Of(char any. Char) üint index. Of(String any. String) üchar. At(int position) üString concat(String another. String) Blanca Polo ICS 111 See java/String. Methods. java 18
Questions? Blanca Polo ICS 111 19
- Licenseid=string&content=string&/paramsxml=string
- Using the random class requires an import statement.
- Import java.applet.applet
- Java 讀檔
- Import numpy as np import matplotlib.pyplot as plt
- Import java.awt.event.*
- Strings in java
- Import java util random
- Import java.util.* class vector
- Java random
- From random import *
- Const char *s
- Private string name;
- String[::-1]
- Classes e subclasses das palavras
- Pre ap classes vs regular classes
- Random assignment vs random sampling
- Random assignment vs random selection
- Java string conditional statement
- Difference between import and direct query power bi