Programming Fundamental I ACS-1903 Chapter 4 Classes in the Java Class Libraries 1
Random Random The Random class provides a capability to generate pseudorandom values pseudorandom because the stream of values we can get are generated algorithmically if the initial value used and the algorithm are known, we can predict the sequence of random values For more details you can consult the book The Art of Computer Programming Volume 2 But in practice we just consider this pseudorandom is close enough to true random 2
Random Methods for random values The Random class provides methods to generate random values in boolean, integer, and double types The Math class also has a method random() that can be used to generate random double values between 0.0 (inclusive) and 1.0 3
Random To use these methods to generate random values, a program must instantiate an object from the Random class Just like what we do in creating a new scanner object before we can take any user s input There are two constructors for this purpose one that takes an argument (a seed or initial value) The advantage to using a seed is that the stream of values is always the same this can assist in debugging code. one that does not take a seed (the no-arg constructor) it actually bases its seed on the system time One cannot predict the values to be obtained from this measure Also do not forget to use the import statement to refer to java.util.random in your program 4
Random Examples Simulate rolling a six-sided dice Random has a method nextint(... ) that returns an int value between 0 and the argument provided Note the value represented by the argument is excluded! If g is a Random object, then to obtain random values as if one is rolling a sixsided die one uses: g.nextint(6)+1 Program: RollDice 5
Random Simulate tossing a coin There could be a number of approaches to do this nextint(2) generates 0 and 1 nextrandom() generates true and false nextint() generates integers - approximately half are negative and the other half are positive (or half are even and half are odd) Program: TossCoin 6
Character Character The Character class has many static methods that can be used Because the methods are static the programmer does not instantiate an object But these class methods must be prefixed with Character. Like: Character.toLowerCase(ch) 7
Character Some commonly used static methods of the Character class 8
Character Examples Detecting the type of character Still, we use the charat() of String class to access the individual characters in a string one-by-one Then determine the type of each character using isdigit() and isletter() Program: CharacterTypes Getting the numerical value of a numeric character The Character method getnumericvalue() can be used to obtain the decimal value of a character Program: SumNumericValues 9
Character Validating input In many situations a user's input must be validated before the input can be processed Without validation, a program could be crashed if the user s input is of improper type Program: ValidateStudentNumber Using the internal representations of characters Instead of using Character method getnumericvalue() we can use characters in arithmetic expressions directly Program: Exercise 5 (modified SumNumericValues) 10
Scanner We touched this before It is used to obtain data from the user via the standard input device keyboard The input from the user is a sequence of tokens Tokens are strings separated by delimiters Delimiters by default are whitespace Whitespace includes spaces tabs new line and a few other characters A programmer can define the delimiters of a token Consider that a program could be reading a file where tokens are separated by commas In this course we will stick with the defaults 11
Scanner The purpose of a Scanner object is said to parse the input stream making tokens available The use of Scanner objects we cover three usages for the Scanner class: As discussed earlier for handling input from standard input: System.in Obtaining tokens from a string Obtaining tokens from a file To use the methods in the Scanner class you must include java.util.scanner in the import statement instantiate a Scanner -- for above three usages Scanner s = new Scanner("System.in"); Scanner s = new Scanner(s); //s is of type String Scanner s = new Scanner(f); //f is of type File 12
Scanner Use Scanner on a text file In order to read a text file we must declare a Scanner object associated with that file Let us consider only files that are in our BlueJ project -- like the readme.txt Why: when the file is contained in BlueJ s project we only need to use its name as a string Scanner f = new Scanner(new File("Readme.txt")); Methods for Scanner Use with caution as a mismatch type error could cause the program to crash Like use nextint() to get a character 13
Scanner 14
Scanner Examples Reading Readme.txt DisplayReadme while (f. hasnext ()) to avoid exception error Use java.io.filenotfoundexception to for exception handling in case there is no Readme.txt in this project Scanning a string ScanString while (s. hasnext ()) again! 15
Math It contains fields for pi and e and methods for performing basic numeric operations These methods include exponential logarithm square root trigonometric other functions They are all static methods So no need to instantiate an instance first but again you must use the method name together with the prefix Math 16
Math Some of the methods we may use Refer to the table on page 135 Example FindMax Exercise 14 17
Integer It has many static fields and methods that can be used on integer These methods are static No need to instantiate an object When used must be prefixed with Integer Example TotalQuantity 18
Integer Some methods 19