CS 101 - Introduction to Computing, Spring 2010

Lab 2

For this assignment, we will write a simple Java program in the DrJava environment. You should look at the program Lect121a.java that was written in lecture on 1/21. This program shows the basics of input, calculations and output which you will need to do for this program.

Java Programming

When doing Java Programming,it is very important to follow a basic Java Template. Below is a file called Template.java

/**
 * Class for creating a template for a simple Java program
 * 
 * @author Pat Troy: troy AT uic DOT edu
 */

// Note the name of the class in the following line MUST
// match the name of the file.  This is stored in a file
// named: Template.java 
public class Template 
{
  
  public static void main (String[] args) 
  {
    System.out.println("Begin Java Exection");
    System.out.println("");


    // put your Java Program here


    System.out.println("");
    System.out.println("End Java Exection");
  }
} // end of Template class

Starting up DrJava in the ACCC Labs

In the ACCC labs, DrJava is one of the programs that can be found via the start menu. If you are trying to set this up on a different machine, you can download DrJava from http://www.drjava.org.

To use the code provided with the text book, you need to place the bookClasses directory in a writable space on the computer. The bookClasses directory is available on the disk provided with the book or in this zip file: bookClasses10-1-07.zip or at bookClasses-7-22-09.zip

To use the bookClasses, you will need to add an extra classpath to refer to the bookClasses directory. If you click on the Edit menu item and DrJava and then select Preferences, you will open the Preference's window. Click on Add below the Extra Classpath textarea and specify the location of the bookClasses directory. This information is also discuss in Chapter 2.2 of the text book (don't forget you can find a PDF file of the first 4 chapters from the main web page for the course).

Running DrJava in the CS Labs

To run DrJava in the CS Labs, you will first need to copy the bookClasses directory and set the classpath. There is a script (i.e. small operating system program) that will do this for you. From a terminal window, enter the following:

     ~i101/setupDrJava
Only enter the above command once.

After the bookClasses directory has been set up, enter the following command to start DrJava:

     ~i101/runDrJava
Once DrJava is up and running, it should work the same as it has on the ACCC's windows machine. Actually is should work better. At least, it will run faster.

The Basic UNIX Commands

Since we are using the LINUX machine, you should be aware of a number of basic UNIX Commands. The commands are broken down into two lists. The first list is the most common commands that you may likely need to complete this lab. The other list are commands that you will find useful over time.

The common UNIX commands that you may need for this lab are:

Other UNIX Commands

There a literally hundreds of UNIX commands and I doubt that anyone knows all of them. A few other UNIX commands that you may want to know are: For more information on UNIX commands check out the ACCC's web page UNIX 101. It gives a nice summary on various UNIX commands.

Lab Assignment 2

Due: Thursday 1/28/2010 by 11:59 pm (i.e. midnight)

The Secret of 42

You are to write a program that will always produce the result of 42. This is not really a useful thing, except you can maybe cause someone to think that you can read their mind. This type of trick is sometimes called a calculator problem. The idea was that the person was to be given a calculator, told to do pick some number they wished to use, and told to perform various operations using that number. At some point in time the person was told what number would be on the calculator. Thus trying to make the person think that their mind had just been read.

  1. The first thing the program is to do is to display a message something like: "I can read you mind and make you preform a set of operations that will always result in the number of 42 no matter what number you start with." Use the showInformation() method of the SimpleOutput class to do this.

  2. Now, ask the user to type in any 4 digit number except 0000, and read this number from the keyboard. Use the getIntNumber() method from the SimpleInput class to do this.

  3. Now tell the user that we want to make an 8 digit number that contains the 4 digit number listed twice (i.e. if the user enter 1234, we want to create the number 12341234). Tell you use you will help them create this 8 digit number by multiplying the original number by 10000 and then adding the original number.

  4. Create the 8 digit number and show the user the 8 digit number so we know that it was modified correctly.

  5. Now tell the user that this number is divisible by 137 and they are to perform that division. Again tell the user that you will help them by performing the operation. Show them the result of the division.

  6. Now tell the user that this new number is the divisible by the original 4 digit number. Again tell the user that you will help them by performing the operation. Show them the result of the division.

  7. Now tell the user you have one more operation to perform. You will need to subtract 31 from this new number and you will arrive at the predicted result of 42. Once more, tell the user that you will help them by performing the operation. Show them the result of the subtraction.

  8. Ask the user if they are impressed with "my mind-reading abilities" and then quit the program.

The key to this program is that result from step 6 is always 73 no matter what 4 digit number they start with. This can be proven by a fairly simple mathimatical proof that you could figure out if you wish to.

Note the program only has one value it needs to read in from the user (this is done in step 2). The other steps are with just print statements (steps 1 and 8) or print statments with some calculations (steps 3 through 7). Be sure to display the various results as we work our way through the problem.

Submission of the Lab

The lab must be submitted electronically to the Digital Drop Box inside of Blackboard. Obviously, this needs to be to the Digital Drop Box for the CS 101 course. You will only need to submit the java source code file. You are to name your file with both the lab number and your NetID so we can find it easier. For someone with the NetId of tsmith14 the file should be named:

     Lab2tsmith14.java