Java Class Session Guide: 6 MARCH 2017

Learning Goals

  1. Code up a few main() methods that use timers and random numbers
  2. Diagram class colleagues' object methods and member variables
  3. Prepare for object presentations on Wednesday

Warm-up: Please do this on your own as soon as you get to class

  1. Open up our shared google doc and find your code partner. Read his/her object description and begin to think about how to diagram the inputs, transformations, and outputs.

Exercise 1: Tinker with sleeping and random numbers

Sleeping programs

Your objects might benefit from a little jazz, and sleep orders and random numbers are a great place to start. Let's see how to use these two types of code.

It might be the case that you'd like to simulate your code pausing for a moment, perhaps to "think" or you're simulating a behavior that takes time to execute, such as making coffee. We can ask the computer to stop what it's doing for some number of milliseconds very easily. We call the sleep() method on the Thread object. A thread is a programming structure that acts as an independently executing chunk of code. We always have at least one thread running, and once we grow in our Java learning, we can fire up a few threads at a time. This is handy when we want the program to be able to do two operations at the same time. The code to sleep a program is super simple. This line pauses the program for 1 second

Thread.sleep(1000);

You can call this line anywhere in your program, meaning inside any method, or even a constructor. It can be in main() or in a method that does something specific.

Random Numbers!

We can also do fun things when we can implement a random number sequence in code. This might be handy if an object needs to exhibit seemingly "chosen" behavior that the user doesn't determine. There are several ways to create random numbers in Java, and which one you choose depends on your intended use of these numbers. For this exercise, we'll look at how to generate random integers. The first thing we do is import the java.util.Random class:

import java.util.Random;

Then we create an instance of this object which has embedded in it a long list of random numbers that it pulls from whenever we ask it to:

Random rand = new Random();

So our Random object is pointed to by our variable called rand. We can then call the variable nextInt() to get a random integer. If we call nextInt() without any input parameter, it will generate a random integer between zero and the maximum value of integers, which is quite large. We probably want to limit the range of random numbers, and we do that by passing in the upper bound value when we call nextInt(). If we want a number between 0 and 10 chosen randomly, we pass in the max integer we want + 1 and store it in a correctly typed variable. We can print out the value to test our generator:

int randomInt = rand.nextInt(11);
System.out.println("the random number is: " + randomInt);

Once we have that random number in our integer variable, we can do whatever we want with it. We can also get random doubles or a random true/false value with logically named methods:

double randomDouble = rand.nextDouble();
boolean randomBoolean = rand.nextBoolean();

Note that we didn't specify an upper bound when we called nextDouble() since the method will by default return a random double value between 0.0 and 1.0. Boolean values are either true or false and there is no need for an upper bound.

Try including one or both of these functions in your code!

Exercise 2: Diagramming our objects for the Object Project

Use the handout from Eric, also linked here, to diagram the five methods that one of our class colleagues proposes coding. Please use this as a thinking exercise. Your diagram should include:

You'll check your colleague's diagram to make sure it's correct, and give it to Eric for scanning for Wednesday's class

Exercise 3: Prepare for your object project presentation

Take this time to get ready for Wednesday's presentation. Review the Object Project description and pay close attention to deliverables so you come prepared on Wednesday!