2017年7月27日 星期四

CS106A - Programming Methodology - Lecture 8

Information hiding
hide the complexity from users
1 problem each and the problem is able to hide the complicated information from users.





methods is like a black box and once you successfully operate the thing, you don't need to care about how and what is inside the black box.

remember to return your value back to the original run method (the one called the function)

client of the class - user
implementer - coder

pseudo random number

import acm.util.*;
//this is on the top of the code

private RandomGenerator rgen = RandomGenerator.getInstance();
//this is at the end as a private instance variable


gets a random number by the process

instance variable ("ivar") vs. local variable

local is just in the method 
ivar is in the whole class (the constant is ivar)
ivar is a "state" that represents something or some condition

The RandomGenerator class defines the following methods:
int nextInt(int low, int high)
Returns a random number int between low and high, inclusive.
int nextInt(int n)
Returns a random int between 0 and n-1 (like the for loop)
double nextDouble(double low, double high)
Returns number low<=d<high
double nextDouble()
Returns number 0<=d<1
boolean nextBoolean()
Returns true of false like flip a coin
boolean nextBoolean(double p)
Returns the possibility of being true, 0<=p<1
(almost the same as nextDouble())
Color nextColor()
Returns a random color...

Random examples
Roll dices

the random number has actually a complicated process (that you don't need to know) to generate the numbers. like the ones in MatLab... I remember that everyone gets the same number...

sometimes we will give a "seed" at first to make sure we will get the same sequence of numbers every time.
rgen.setSeed(1);
this will starts from 1 as a seed and every time you will get the same sequence to DEBUG!!