Hands on Testing Java: Lab #1

Computing, Editing, and Compiling Environments

Introduction

The main purpose of this lab is to introduce you to the computing environment that you'll be using. This includes a brief introduction to your computer's operating system, the editor you'll use to edit your programs, and the compiler you'll use to compile your programs.

You will use the ideas from this lab again and again throughout this course, so you should make every effort to understand not only what you're doing, but why you're doing what you're doing at each step.

Helpful hint: Ask questions. Ask the students around you. Ask the grader. Ask your instructor.

You're going to need to know the answer to the following questions to get started (hopefully your instructor will oblige):

  1. Should you turn the computer on and off, or does it remain on all of the time?
  2. You should have your own account on the lab's computers:
    1. What's your username?
    2. What's your password? And how do you change it?
  3. In your windowing environment (e.g., MacOS, KDE, Microsoft Windows, etc.):
    1. How do you use the environment?
    2. How do you exit your environment?
  4. What must you do to leave for the day?

Question #1.01 True or False: You have an answer for each of these questions.
To be answered in Exercise Questions/lab01.txt.

For now, jot down your answer for this question on a piece of paper (by hand). Later in this lab you will be told how and where to create a text file for you to write your answer in.

And if you answered this first question "false", then you have some questions to ask...

About this Manual

Fonts

Throughout this lab manual, instructions will be printed in this default font (the one you are reading). To help you distinguish the instructions from what appears on your screen, text that you should see displayed on your screen will be shown in a mono-spaced font. Text that you are to type or enter will be shown in a stronger font.

Typing and Entering Data

Terminology

Each lab exercise will introduce many new terms. The first time a new term is encountered, it will appear like this. Make a note of the word, and figure out a one or two sentence definition of the word. (Often the term is explained without being explicitly defined.) This will be a good habit since knowing the terms will make implementing the concepts that much easier. The terms are also collected and listed at the end of the each exercise.

Some terms are followed by this icon: wikipedia. This icon was stolen from the on-line Wikipedia because it is used to link you to an appropriate Wikipedia entry. You are not obliged to read the article, but you may find it useful if a particular term causes you confusion.

Getting Started

Before you can create a program, you must first become familiar with how to control basic operations on the computer. This is one of the fundamental purposes of an operating system wikipedia. Modern computers use two basic ways of interacting with a user: a GUI or a CLI.

An environment in which you use a mouse to interact with menus, windows, and icons on a computer's screen is called a graphical user interface wikipedia (GUI) environment. MacOS, Windows-95, Windows-NT, X Windows (with the CDE, KDE, or Gnome) all provide GUI environments.

In contrast to a GUI, a command-line interface wikipedia uses this interaction:

  1. You are prompted for a command.
  2. You enter a command.
  3. The machine displays the result of the command.
  4. Repeat.

MS-DOS (a precursor of Windows-95 and Windows-NT), UNIX, and Linux are examples of operating systems that use the command-line environment.

In order to use a command-line environment, you must learn the commands that the environment understands. This generally makes a command-line environment more difficult to use than a GUI, since you must be able to recall the right commands to use the system and each system has their own set of commands. However, you have a very rich vocabulary in a command-line interface, so they're well worth learning.

In contrast, users of a GUI environment need only be able to recognize the proper menu choice, link, or icon they need to select in order to make something happen. Unfortunately, a GUI gives you a limited vocabulary, and sometimes this can be a seriously annoying problem.

Follow your instructor's instructions for beginning a session, then click on the appropriate link for the operating system that you are using to continue. The operating-system instructions will have questions for you to answer; again, write these by hand on paper for now.

You should also get started with your development environment:

A Program's Journey

A program starts out as a simple text file of raw Java code which you type in. The Java code is translated by a compiler wikipedia into byte code wikipedia. The byte code is then interpretted by a virtual machine wikipedia (VM).

Let's break this journey down into steps.

The first step is you entering the programming into a text editor wikipedia. A text editor differs from a wordprocessor (like Microsoft Word or StarOffice Writer) in the way their data is stored. A wordprocessor must save all of the formatting that you put into a document; this formatting is unnecessary for the programs that you enter, so all that needs to be saved is the raw text of the program itself. Hence, you can use a simpler editor that stores only that text---a text editor.

Unfortunately, the Java code which you type in is not easily understood by the computer. The second step of your program's journey translates the text file containing raw Java code into byte code; this process is known as compilation which is carried out by a program called a compiler. The compilation process does a lot of work to make your program understandable by the computer, but it does not do all of the work.

Java's byte code is easily portable from one type of machine to another. The Java program you compile on a Linux machine can be executed on a Macintosh or Microsoft Windows machine without recompiling (assuming that Java is installed on these other machines). But, as mentioned in the previous paragraph, none of these machines actually understand the Java byte code. These machines need a Java virtual machine wikipedia (JVM); a JVM is implemented as an interpreter wikipedia which interprets the byte code and immediate executes the instructions they represent.

The Journey's Particulars

So to write and execute a Java program, you need at least three tools: a text editor, a Java compiler, and a JVM.

Sun Microsystems (the company that manages Java the programming language) provides the official, standard Java compiler and JVM. Its tools work primarily in a CLI. So to get a minimal development environment, you'd only need to provide a text editor, which all operating systems provide.

However, that is truely a minimal environment. On the other end of the spectrum is the integrated development environment wikipedia (IDE). An IDE is a GUI that provides a text editor, a compiler (perhaps using Sun's standard compiler), a JVM (also perhaps using Sun's JVM), and much, much more.

The text editor in an IDE usually has many bells and whistles which makes writing your code much easier than using an operating system's plain text editor. Most modern IDEs also use an incremental compiler which compiles your code as you type it in (and does not require a separate processing step). All IDEs have a fairly easy way to run your program in the IDE itself.

Writing a Program

Your first step is to write the program itself. In this lab, "writing" will consist of a copy and paste, but you need to paste the program into your text editor.

Driver Classes

A program in Java is written as a driver class. There are many different types of classes in Java; a driver class is the most rare, but it's the only way to get an executable program.

For these first labs, our drivers will use command-line interfaces (CLIs). So that it's easier to recognize the classes which implement a command-line interface, we're going to use this convention:

Programming Convention: Use a CLIDriver suffix on the classes which implement a CLI driver.

The rest of the name will be based on the purpose and responsibility of the driver. The program that you will "write" later in this lab is a CLI driver that computes a few multiplications, so you'll call it MultiplyCLIDriver.

Classes Behind the Scenes

If you're using an IDE (like Eclipse or IntelliJ), this behind-the-scenes look is for information only. If you're using a plain text editor (like emacs or vi), then read carefully!

A Java class is supposed to be stored in its own file. The name of the class determines the file name; a class MultiplyCLIDriver, for example, should be stored in a file named MultiplyCLIDriver.java.

This lab manual is written with class names in mind. Most IDEs work in terms of class names and manage the file names behind the scenes. If you're not using an IDE, make sure you save files with the .java extension, otherwise the compiler might not work properly.

Packages

Related classes are kept together in a package. This is so that the hundreds of classes that you create just for these labs don't end up all in the same place.

For this lab manual, keep the classes for one lab or project in their own package:

edu.institution.username.hotj.task

The interpretation of this package is that you, username, are working at institution.edu on "Hands on Testing Java" (i.e., HoTJ) assignments.

Packages Behind the Scenes

Again, an IDE (like Eclipse or IntelliJ) will manage packages for you; read this for your own edification. The rest of you, read carefully!

Packages are implemented as directories. Our package becomes edu/institution/ username/hotj/ task. The Java files must be stored in that directory.

Creating the Class

So let's create the program. First, you need to create the CLI driver class and start editing it.

You should now have your text editor running, ready to enter in your MultiplyCLIDriver class. Here are the contents that should go into the editor:

package edu.INSTITUTION.USERNAME.hotj.lab01;

import ann.easyio.Keyboard;
import ann.easyio.Screen;

/**
 * Class for a simple driver to test our a Java development
 * environment.
 *
 * @author     Joel Adams
 * @author     Jeremy D. Frens
 * @assignment HoTJ Lab #1
 * @course     ???
 * @semester   ???
 */
public class MultiplyCLIDriver {

  public static void main(String[] args) {
    Keyboard theKeyboard = new Keyboard();
    Screen theScreen = new Screen();
    theScreen.println("\nThis program inputs an integer" +
                       "\n\tand displays 2x, 4x, and 8x its value.");
    theScreen.print("\nPlease enter an integer: ");
    int aNumber;
    aNumber = theKeyboard.readInt();
    theScreen.println("\n\nTwice " + aNumber + " is " + (2*aNumber) +
                      ",\n\tand four times is " + (4*aNumber) +
                      ",\n\tand eight times is " + (8*aNumber));
  }

}

If you're using an IDE, compare the code here with the code that your IDE provides to you. It's fine to replace the IDE's code with the code you see here, but in future labs you will not be given the whole program, so it's useful now to compare what a complete program looks like compared to a minial class.

Do this...

Consider this bit of code in the program:

 * @author     Joel Adams
 * @author     Jeremy D. Frens
 * @assignment HoTJ Lab #1
 * @course     ???
 * @semester   ???

Officially, these lines of code are actually part of a comment (denoted with the /** and */ character sequences). Comments are ignored by the compiler, and so you can put nearly anything in a comment. Good programmers put useful information there, information you can't figure out easily from the program itself.

Programming Convention: You must include information about the author, the assignment, the course, and the semester in a comment before every class you write. Your instructor may require you to put even more information into these comments.

Do this...
Add to and modify the class's comment to include the necessary information about you.

Keep in mind that you are an additional author of the program (or you will be later on in this lab). This means that you must be included as an author, but you must not remove the names of the other authors---to do so is the programming equivalent of plagerism.

Translating and Running a Program

When your source program is entered and saved, it is time to execute the program and look at the results. Again click on the appropriate link to see how it is done:

Applying the Scientific Method

An important part of any science, including the science of computing, is to be able to observe behavior, form hypotheses, and then design and carry out experiments to test your hypotheses---a process known as the scientific method wikipedia. The next part of this exercise involves applying the scientific method to infer some special output codes from the MultiplyCLIDriver class.

Since two heads are (often) better than one, feel free to work through this section with the person sitting next to you.

Observe

Study the code in the MultiplyCLIDriver class and the output from the program. There are many English or near-English words which make at least most of the program meaningful, especially when you compare the driver to the output of the program.

But what are those \n characters doing in the program? What purpose do they serve?

Hypothesis

Construct a hypothesis (i.e., a statement) that states what you think the \ns do.

Question #1.02 What's your hypothesis?
To be answered in Exercise Questions/lab01.txt.

Don't worry, you can't get this question wrong (unless you leave it blank or give a very stupid answer).

Experiment

Design an experiment using the MultiplyCLIDriver class that tests whether or not your hypothesis is false. (Recall that the scientific method can only prove that a hypothesis is false; a successful experiment can only increase our confidence in the hypothesis, not prove it.)

Your experiment should involve adding or removing \n characters. Based on your hypothesis, you should then predict how the output will change.

For example, if I thought that the \n causes monkeys to magically appear out of my computer, then if I added a hundred \ns to MultiplyCLIDriver, I could open my own simian zoo. Fortunately, my hypothesis is wrong, and so when I run my program with a hundred \ns in it, no monkeys appears. This demonstrates that my hypothesis is false.

I can tweak my hypothesis and devise new experiments until I figure out a confirmable relationship between \ns and the number of monkeys that a program spits out. However, there are actually other ways to deduce that monkeys will never fly out of my computer, so perhaps it's better if I come up with a completely new hypothesis.

Your hypothesis and experiment should follow this same form. Just make sure that your hypothesis describes the relationship between \ns at the output of the program (i.e., what you see on the screen).

Question #1.03 What is your experiment? That is, how are you going to change MultiplyCLIDriver?
To be answered in Exercise Questions/lab01.txt.

Question #1.04 How do you expect your program to behave with these changes? Be sure to tie your expectations to your hypothesis; that is, if your hypothesis is true, what do you expect the output to look like.
To be answered in Exercise Questions/lab01.txt.

Do this...
Modify MultiplyCLIDriver as necessary to perform your experiment, compile the code, and run it again.

Question #1.05 Did your experiment confirm your hypothesis?
To be answered in Exercise Questions/lab01.txt.

If these results indicate that your hypothesis is false, come up with a new hypothesis and run a new experiment. Repeat this process until you get a meaningful hypothesis that holds up to your meaningful experiment. Record each hypothesis and test that you come up with.

Take a Deep Breath

If your head feels ready to explode, don't panic! This first lab covers a lot of material, that you will use over and over again, and as you do so, you will begin to naturally memorize those commands that you use most frequently. You can speed up the process by reviewing each of the steps you took in this exercise and practicing in your free time.

Submit

Terminology

byte code, command-line interface, comment, compile, compiler, driver class, graphical user interface, GUI, IDE, incremental compiler, integrated development environment, interpret, interpreter, operating system, package, scientific method, text editor, virtual machine, virtual machine, VM