Handouts:
Console,
Random Generator
Worked Examples:
E=MC2,
Fibbonacci,
Carbon Dating
Write the following three programs to get warmed up to Java variables.
Day2: HelloWorld.java
Write a console program that displays hello world (or any message you like):
Day2: Moon.java
The user (an earthling) enters his or her weight on earth. Your program will print the user's weight on the moon, which is 16.5% of his or her weight on earth.
Day2: RandomNumbers.java
Write a program that prints 1,000 random numbers between 0 and 100.
The file RandomNumbers.java has code that generates a single random number.
public class RandomNumbers extends ConsoleProgram { // A random number generator private RandomGenerator rgen = new RandomGenerator(); public void run() { // change this code to print 1000 random numbers // in the range 0 to 100. int example = rgen.nextInt(0, 10); println(example); } }
In order to generate random numbers, create a RandomGenerator
instance variable (a variable declared outside all methods) like so:
private RandomGenerator rgen = new RandomGenerator();
You can then use the variable to generate random numbers.
rgen.nextInt(min, max) // generates a random int in the range (min, max) inclusive