Download presentation
Presentation is loading. Please wait.
1
Java so far Week 7
2
What we should know by now
Print statement System.out.print(); Primitive data Int, float, double, long, String, char, boolean, Variable declaration int x ; or int x=1; Concatenation “Hello” + “hellow” = “Hellohello” Arithmetic and Boolean expressions Artithmetic (6+2), (6-2), 6/2, 6%2 , ..... Boolean (1==(3-2)), (3>=2), (3<=2), ((3%2)==1) Assignment statements Int x , y , z; x =1; y=2, y= x+1 ; The use of arguments Command line argument are by default parsed as string, Int x = Integer.parseInt(args[0]); double x = Integer.parseDouble(args[0]) Keyboard interaction with the use of Scanner. Scanner input = new Scanner(System.in); String x = input.nextLine(); Int x = input.nextInt(); Double x = input.nextDouble(); If statements If (true ) then {statments1} else {statments2}
3
The random class Random is defined in the "java.util" library package, so any Java source file that uses Random must begin with a line of the form import java.util.Random; or import java.util.*;
4
Creating Random Number Generators
The easiest way to initialize a random number generator is to use Random generator = new Random(); Int x = generator.nextInt(); // generate negative and positive integers Int x = generator.nextInt(n); // generate an integer between 0 and n (0 inclusive and n exclusive). Int x = generator.nextInt(95) + 5 ; // generate an integer between 5 and 100 double x = generator.nextDouble(); // generate an double number between 0 and 1.
5
Example import java.util.Random; class MyRandom { public static void main(String [] args) Random generator = new Random(); int x = generator.nextInt(); // random integer System.out.println(x); int y = generator.nextInt(100); //random integer x ( 0<= x<100) System.out.println(y); int t = generator.nextInt(95)+5; // random number betweein 5 and 100 System.out.println(t); double z = generator.nextDouble(); // random doube between 0 and 1 System.out.println(z); }
6
Home Work A set of simple exercises can be downloadable from you web site Do please do all these exercises before the 19th of November.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.