CSCI 241 Labs: PreLab 3

CSCI 241 Labs: PreLab 3
Practice, Practice, Practice


Introduction

Have you ever tried learning to play a musical instrument? Perhaps you were inspired to play guitar because of a favorite musician's work. It is a sobering experience to pick up that instrument for the first time and find that placing your fingers for even a basic chord is awkward and difficult.

One encounters similar challenges when learning a computer programming language. The task becomes even more difficult when the language is as complex as Java. Writing your first program from scratch can seem overwhelming.

When learning a new programming language, we must begin with the "building blocks", i.e., the basic statements which are used in nearly every program. Chapter 2 contains these types of statements.

The first part of a programmer's job is to break the assignment into parts. Many computer programs use the same basic general structure, sometimes called "IPO" (Input-Process-Output):

  1. Obtain input from the user.
  2. Process that input.
  3. Generate output that is sent back to the user.

Seems simple, right? This "to-do" list creates all kinds of questions, however:

For example, picture your last grade report that you received at the end of the semester (college or high school). A non-programmer decided on what kind of information it should contain, and also its appearance (layout). A programmer typically receives a "mock-up" (sample) of the desired resulting report and needs to work backward, figuring out where the data comes from, and what kind of work the program needs to do to create the resulting grade report. If there are questions that the mock-up doesn't answer, the programmer goes back to the person requesting the work for clarification.

Write, Test, Write Some More

Some students, especially if they feel they already "know" the language, may be tempted to write the entire program before compiling it. This only works if the programmer has a lot of luck! In the "old days" (when your instructor was in college), computer processing time was expensive and the goal was to compile as few times as possible. We don't have that restriction today.

Perhaps you have already written a program in which you copied and pasted a line of code many times, only to later discover that you made a typo in the original. Better to make sure the original works first and copy the code only when you know it works correctly.You can write just a little code at a time, trying it out, and adding more after you know what you have so far has worked.

For example, look at the ComputeArea program in your textbook. You see it in several versions within section 2.2. Each of those versions will compile without errors. The comments describing the steps give the programmer an outline to follow, filling in the details a piece at a time.

Choosing Test Data

Choosing good test data is an art in itself. Some companies have entire teams of programmers and analysts whose jobs are to create and test a wide range of test values for the programs that others have written. It is good software engineering practice to detect errors as early in the process as possible because errors propagate quickly.

Remember the program you worked on in last week's lab that calculated the hypotenuse of a right triangle? A 3-4-5 right triangle is an easy example to try. Certainly, you should can use numbers that are easy to calculate as test data. But also, think about what kinds of values are "at the edge" of what seems reasonable for your program to handle. Here are some ideas of what you might want to test for numeric data in that particular example:

  1. Integer values that one can calculate easily, such as 3(side a) and 4 (side b), giving 5 for a hypotenuse
  2. Real values - what about 3.25 and 4.999 for sides?
  3. Zero - will the program still calculate "correctly" if one of the values is zero? We would need if statements to avoid zero as an input value, so we won't worry about values that couldn't make up a right triangle. We just want to make sure the program doesn't crash during its run.
  4. What about negative values? Integers or real numbers?

In this week's lab, you'll practice writing simple programs and designing test data for them.