CS 101 - Introduction to Computing, Fall 2011

Lab 2

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

Running DrJava in the CS Labs

DrJava can be found on the MAC Machines in the CS Lab in SEL 2254 by
  1. clicking an the APP Folder icon on the bottom (right side) of the terminal screen
  2. Then click on the DrJava logo that appears in the pop-up folder

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

Output in Java

To output to the screen, we use the method System.out.println(). Between the parenthesis, we place the information that we wish to display. To display a string of values (i.e. words), we place the words in double quotes. Such as:
     System.out.println ("Hello.  Hope you have a good day.");
To print out a number or the value of a variable, we place that information between the parenethesis. For example:
     System.out.println (57);
     System.out.println (number);

We can print both some text and a value by placing both of these in between the parenthesis and separating them with a + sign, such as:
     System.out.println ("The result of the operation is " + number);
Note, there also exists a method System.out.print(). This method will have the next printed item begin on the same line as this one ends. The System.out.println() always has the next printed item begin at the beginning of the next line.

Also note that to print a blank line in the output (i.e. to skip a line), use System.out.println() with nothing between the parathesis. In the program Template.java, this same thing was done by printing an empty string:

     System.out.println("");
Both produce the same result. You will notice that often the same result can be produced via different approaches.

Lab Assignment 2

Due: Thursday 9/8/2011 by 11:59 pm (i.e. midnight)

Write a Java Program that will:

  1. Print out your name
  2. Print out your net-id
  3. Print out CS 101
  4. Print out your lab time
  5. Print out the additional information as described below:

As I Was Going to St. Ives

"As I was going to St Ives" is a traditional English language nursery rhyme which is generally thought to be a riddle.

The most common modern version is:

As I was going to St. Ives
I met a man with seven wives
Each wife had seven sacks
Each sack had seven cats
Each cat had seven kits
Kits, cats, sacks, wives
How many were going to St. Ives?

The answer to the riddle is commonly thought to be 1, the narrator of the rhyme. The man, wives, cats and kits are thought to be going the other way (i.e. coming from St. Ives). You can check out the Wikipedia page on this for more information than you would ever need to know.

For the final output of the lab, print out the information about who the narrator "met" while going to St. Ives. That is, print out:

You are required to use variables, multiplication and addition operations to determine the answers. For example:

int numMan, numWives;   //declaring variables 

numMan = 1;             // determining the number of men
numWives = numMan * 7;  // determining the number of wives
Just storing the value of 7 into the variable numWives is not enough for full credit for the assignment.

You are also required display the values in the variables when showing the information about who the narrator met. When doing this you must also display some text describing any values prior to displaying the values. For example:

System.out.println ("The number of wives are: " + numWives);

Submission of the Lab

The lab must be submitted electronically to the Assignment Link for Lab 2 inside of Blackboard. You will only need to submit the java source code file (the ".java" file).