The following three programs all work with Java variables. This is a topic we have NOT gone over in class yet. We will explain the basics on this page, let you explore the topic, then go over the material again during the lecture this afternoon.

Console Programs

Console programs take text as input and/or give text as output. Console programs allow a program to talk to the user and for the user to talk back. Look on the 'Handouts' tab on the website for the 'Console Reference' page. It will show you how to print information on the screen or get information from the user.

1. Hello world

Write a console program that displays hello world (or any message you like) using the println method shown on the 'Console Reference' page. That page shows the method like this: println(String printMessage); Instead of 'String printMessage' you can put whatever you would like there, but it MUST be surrounded by double quotes. So I could try something like this: println("Hi Everyone!");


A variable is just a box, a box inside your computer. Your computer has TONS of these boxes, billions of them. In each of these boxes, your computer can store some information. Your computer could store a number, a character, whatever it wants. The only rule is that you have to tell your computer what you want to put inside the box, so that the computer knows how big to make the box. So every variable must look something like this:

Every variable must have 3 things: A type, a name, and a value. The example above has a type of 'int', meaning integer. The name is 'myNumber', and it has a value of 6. There are all sorts of cool types that you can use in your programs. If you want a whole number like 6, use an int. If you want a number with a fraction like 2.5, use a double. You can also use a type called 'boolean' that can hold a value of either 'true' or 'false'. Check out the examples under the 'Examples' tab on the website. These show a lot of variables being used in many different ways.

2. Moon Weight

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. But first, you will need to get the user's weight. See if you can figure out how to do that by looking at the 'Console Reference' sheet mentioned above.

3. Random Numbers

Write a program that prints 1,000 random numbers between 0 and 100.

The file 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);

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