Computing Concepts with Java Essentials
Laboratory Notebook
Chapter 1 - Introduction

Cay S. Horstmann
Geof Pawlicki

Your name:
Your email address:
Your student ID number:

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.

Lab Objectives

To gain experience with

R1. Becoming familiar with your computer

All modern computers offer their users an interface to their physical, electrical, and digital systems. Your computer's operating system provides fundamental services from behind the scenes. A large part of a computer's work is simply storing and retrieving data so that it can be available to use.

Put simply, the operating system takes care of a lot of the filing work - storing, moving and remembering where things are - so you can do useful things at a higher level. You interact with the operating system via a Graphical User Interface (GUI), for example a windowing system using a pointing device like a mouse, or a Command Line Interface, using only a keyboard and a single window.

What are some examples of tasks you'd tell the computer to do via the Operating System interface?

Operations like browsing the web and editing text in a word processor - most of the things you'd have the computer do - involve your operating system executing some program. The Operating System itself is a program which is running all the time. It executes your instructions and, in turn, it can run other programs.

It is useful to think of a program as a sequence of instructions. Both executable instructions and digital data can be represented as files, for example, on your hard drive. Both are sequences of symbols, just like the letters that make up the words in this sentence. Your job as a programmer is of course to provide the instructions that operate on the data.

Let's start first with a data file. Use your operating system to locate the file and look inside its contents. You may need to ask a friend how to find a file on your computer, or where the files for the CCJ package are located.

What did you do to find

How did you open ?

The program that you'll be running to write computer programs is your text editor, which often is part of an integrated compiler environment. You may be working in a computer lab--then ask your lab guide how to start the editor. Or you may have purchased and installed your own compiler. Then follow the vendors instructions. Go ahead and start it now.

Again, what did you do?

R2. Compiling and running programs from within your compiler

Frequently in these labs you will be asked to compile a sample program. Below is a copy of a Java program that displays a drawing. Copy and paste it into your compiler's editor, and from there save it as

Describe what you did.

public class Art
{  public static void main(String[] argv)
   /* PURPOSE: Display an 'art' drawing
   {  String s1 = " *   *   *   *   *   * ";
      String s2 = "   *   *   *   *   *   ";
      String s3 = "__________________________________\n";
      String s4 = "_________________________________________________________\n";   

      System.out.print(s4 + s1 + s3 + s2 + s3);
      System.out.print(s1 + s3 + s2 + s3);
      System.out.print(s1 + s3 + s2 + s3);
      System.out.print(s1 + s3 + s2 + s3);
      System.out.print(s4 + s4 + s4 + s4 + s4);

Once you have typed in (or, in this case, pasted in) a program, you need to to compile it to to create an executable file. Again, these steps depend on your compilation environment. Find out the steps for your computer system, then go ahead and compile

Describe what you did.

Finally, execute the program. Once again, the steps depend on your computer system.

Describe what you did to execute the program.

Describe what happened when the program executed.

P1. Writing simple programs

Your initial Java programs will be contained entirely in one file, and there are some elements that they all will have because of requirements of the Java language. When you build a program, your compiler looks for code of the form:

public class Classname
{  public static void main(String[] args)
   {  /*
         your work goes here

The textbook has a program that prints the message Hello, World! on the screen.

Try changing it to display Hello, Universe!

Type the program into your compiler's editor, compile and test. Then paste the source code in the following text box.

P2. Detecting syntax and logic errors

There are numerous opportunities for error in any program, many times in places that seem too simple to require close attention. What do you think the following program is suppsed to do?

public class Cube
{  public static void main()
   {  double height = 3.0;  /* inches *\
      double cube_volume = height * height * height;
      double surface_area = 8 * height
      System.out.println("Volume = " cube_volume);
      System.out.println("Surface area = " + surface_area;

Will it work as it is?

Try compiling the program. What were the results? (Use copy and paste to place a copy of your compiler's error messages here.)

Fix the syntax errors. Place a copy of your program's output here.

The program has two logic errors. Fix them both and paste the corrected program here.

R3. Algorithms

An algorithm is a recipe to carry out a task. To be programmable, such a recipe must be

While an algorithm will eventually be expressed in a particular programming language, it is frequently helpful to describe its operation in natural language, before undertaking the task of coding it up. Suppose, for example, that you need to count the number of days that have passed since the first of the year. For instance, May 22nd is (31 + 28 + 31 + 30 + 22) = 142 days from January 1st.

Describe an algorithm to do the computation for any day of the year. (Ignore leap years, that is, assume that February always has 28 days.). Your algorithm should be so detailed that anyone, even a visitor from Mars who has never heard of month names and days per month, can carry out the steps and arrive at the correct answer.

Don't forget to send your answers when you're finished.