CS 201 - Computer Science I - Fall 2002
Lab 4


Loyola College > Department of Computer Science > CS 201 > Labs > Lab 4

Due

Wednesday, November 6 at 11:59pm

Purpose

This lab uses event listeners, if statements, and methods from the Math class.

Introduction

Read chapter 6 before doing this lab.

The applet given below changes its background or foreground (text) color when you click one of the buttons. There are three new topics that must be introduced before you can write this applet.


Hold Control and click the reload icon if this applet does not work

Event Listeners

When the user interacts with an interface component, that component fires an event. We can set things up so that when components fire events, other objects respond to the events by executing certain methods. Those responding objects are called listeners.

Different components fire different kinds of events. Buttons, for example, fire ActionEvents. An object that listens for ActionEvents is called an ActionListener. When an action listener "hears" an event, it executes its actionPerformed method. The actionPerformed method should then contain the statements you want executed when the user clicks a button.

In order to set this up in Java, we need to do several things.

if statements

An if statement allows sequences of statements to be executed or skipped depending on the outcome of a test. The general form is
if (condition)
{
   // statements to execute if condition is true
}
else
{
  // statements to execute if condition is false
}

condition must be an expression that results in a boolean value, for example, one that uses the comparison operators <, >, ==, <=, >= and !=. The else case is optional. The braces are also optional if there is only a single statement in each case.

When execution gets to the beginning of the if, the condition is evaluated. If the condition is true, then the statements after the if are executed and the statements following the else are skipped. If the condition is false, then the statements after the if are skipped and the statements after the else are executed.

For example, assume that grade was declared as an int and warning was declared as a String. Then the following piece of code sets the String to "Failure" if the value stored in grade is less than 60. Otherwise, it sets the String to "None".

if (grade < 60)
{
  warning = "Failure";
}
else
{
  warning = "None";
}

The Math class and random numbers

Most methods ask about the state of an object or change the state of an object. In either case, in order to use the method we need to specify which object we need to work with by putting its name in front of the method's (with . in between).

Methods that don't have anything to do with the state of an object can be declared as static methods. Mathematical functions are a good example of these: in order to compute a sine or cosine, you don't need to know anything about objects. Java supplies a Math class that contains a lot of these functions. To call a static function one gives the name of the class where the name of an object would normally go. For example, Math.sin(0.0) will return the sine of 0 radians.

Math supplies a method called random that returns a random double in the range [0.0, 1.0) (the '[' means that the lower endpoint is included; ')' means that the upper endpoint is not). We can use this method to obtain random int in any desired range by multiplying the result of Math.random() to scale it to the desired range and then truncating it with the (int) type conversion operator. For example, (int)(Math.random() * 10) gives a random int between 0 and 9 inclusive (Math.random() gives a number in [0.0, 1.0), multiplying by 10 gives a number in [0.0, 10.0), and truncating gives an integer in [0, 9]).

Assignment

Implement the applet given above as a class called Lab4 by following the steps lsited below.

Submissions

Submit your lab by attaching the file containing your final Java source code to an e-mail message sent to jglenn@cs.loyola.edu.