CMPU 101 - Lab 5

$Revision: 1.2 $

In this lab you will use a Scanner object to read values from the keyboard.  You will also use the methods of the String class to manipulate string values.

Getting Started

Start DrJava.  Today you will be starting from scratch, defining your own Java classes.

Start a new Java source file in DrJava.  Define a public class called CompoundInterest.  It should start out looking like this:

public class CompoundInterest {


Save the file.  DrJava will prompt you for a file name: make sure the class is saved in a file called "".  Make sure the file is saved in the directory "/home/cs101".  In DrJava, you can go to this directory from any file browser dialog by clicking on the "Home" button that looks like this:

Define a main method inside the CompoundInterest class.  This method should be defined as follows:

public static void main(String[] args) {


Add the following statement to the beginning of the method:

System.out.println("Welcome to the compound interest calculator");

Click "Compile All".  Make sure the program compiles successfully.

So far, we've been running Java programs from within DrJava.  Today, you will run your programs in a terminal window.  Start the terminal application by clicking the toolbar button that looks like this:

At the prompt, type the command

java CompoundInterest

and hit return.  You should see the output

Welcome to the compound interest calculator

followed by another prompt.  This means that the CompoundInterest class's main method was executed, did its work (printing the message), and then returned.  When the main method returns, that means that the program has finished.

First Task

Add code to the main method of the CompoundInterest class so that it prompts the user for an initial balance, an interest rate, and the number of times to add interest to the balance.  Each of these quantities should be typed by the user.  After this information has been entered, the program should print the compounded balance.  Here is a transcript of the program running one time.  The user input is shown in bold.

Welcome to the compound interest calculator
Enter balance: 1000.0
Enter interest: .05
Number of times to compound: 5
New balance is 1276.2815624999998


Run the program from the terminal window and make sure it works as expected.

Second Task

Open a new file and define a class called SecretMessage.  Add a main method to the class.  Your task is to read tokens of input from the keyboard as Strings and print out the characters contained in each token in reverse order.  Each (reversed) token should be printed on a separate line.  If there are no more tokens to read, or a token matching the string "quit" is read, then the program should exit.  Here is an example transcript:

No one will be able to read this secret message

You can run this program by running the command java SecretMessage from the terminal prompt.


When You Are Done

Demonstrate your completed programs to an instructor or lab coach.

Submit both files ("" and "") using the CS 101 Submission WebsiteMake sure you submit the Java source files: the filenames should end in ".java", not ".java~" or ".class".