It is intended that you complete this lab assignment during the lab session on Thursday, January 19. When you have finished, demonstrate your programs to the instructor to receive credit for this lab.
If you do not finish during the January 19 lab session, you may be checked off at the start of the January 26 lab session instead.
Complete the following programming exercises with a programming partner using a technique called "pair programming." Both members of the pair are seated at the same computer, with one partner controlling the keyboard and mouse and the other partner acting as a coach or assistant. Trade roles every 5 to 10 minutes. When your team finishes a programming exercise, you will both receive credit for that exercise.
Before doing the following programming exercises, start TextPad. By default, TextPad starts up with a single document open, called "Document1", which TextPad assumes is a standard text document. In order for TextPad to do automatic colorizing and formatting of Java programs, we have to let TextPad know that the document we will be working on is actually a Java program. To do that, select "Save As..." from the "File" menu. Use the "Create New Folder" icon on the "Save As" window's taskbar to create a folder in which you will keep your CSC 120 programs. You may wish to create a nested folder named 'Lab1' inside your 'Csc120' folder in order to keep your programs for this lab separate from the programs you will write for future labs. Finally, save the empty document that TextPad had open as 'Greeting.java', which would be a suitable name for the first program you will create in the exercises below.
Before editing the file, configure TextPad as suggested by Cay Horstmann in his "Compiler Tutorial":
You can now edit the files. But before editing them, it is a good idea to adjust the tab settings in TextPad. You don't want TextPad to save your file with tabs. Since the number of spaces per tab is a user-settable value, there is a good chance that your grader, your instructor, or your printer (or all three) have different tab settings. The remedy is to make sure that your editor replaces tabs with spaces.
Select Configure -> Preferences from the TextPad menu. In the resulting dialog, click on the tree nodes Document Classes -> Java -> Tabulation. Then make sure the check boxes Convert new tabs to spaces and Convert existing tabs to spaces when saving files are both set. Also change the default tab spacing and indent size to 3. This matches the settings in your textbook.
Now you should be ready to start editing Java programs using TextPad.
The textbook has a program that prints the message Hello, World! on the screen. (See 'HelloTester.java', page 17.)
Try changing it to display "Hello, name!", where "name" is replaced by the name of your pair programming partner. Name the class 'Greeting' instead of 'HelloTester', since we told TextPad that this file would be named 'Greeting.java', and the name of a Java file has to be the same as the name of the class it contains.
Remember that to compile a program in TextPad, you can press Ctrl - 1 and to execute it, Ctrl - 2.
Write a program that displays the names of both members of your pair programming team, showing one member of the team at the computer and the other to the right of the computer, like this:
+----+ | | +----+ /====/ Samuel Susan
Hint: You will want the body of the method to consist of several lines of the form
System.out.println("something");
Write a program that computes the sum of the first five powers of two (i.e., 1, 2, 4, 8 and 16). See the hint for Exercise P1.5 on page 30 of the textbook for an example of what is expected for this exercise.
Do Exercise P1.6 on page 31 of the textbook. (You only need to have the second version — the one that gives the correct answer — checked by the instructor.)
Do Project 1.1 on page 31 of the texbook. Note that it can be written as a combination of the programs shown in Exercises P1.7 and P1.8. You may want to type in each of those programs first in order to test that they work as described. (Each pair programming partner should type one of them into TextPad, compile, and execute it.) Then you can copy and paste lines from those programs to construct the program needed for Project 1.1.
Do Exercise P2.9 (page 61).
Do Exercise P2.10 (page 61).
There are numerous opportunities for errors in any program, often in places that seem too simple to require close attention.
What do you think the following program is designed to do?
public class CubeWill it work as shown? If not, what problems can you identify?
Try compiling the program. What were the results?
Fix the syntax errors. What program did you compile successfully?
The program has two logic errors. Fix them and demonstrate the corrected program to the instructor.
Each program in this set of lab exercises will be graded on a "mastery" basis - you will get full credit for the program if it works correctly, or no points if it is not completed. Each program will be worth 1 point, for a total of 8 points for this lab assignment.
Copyright © 2004, 2006 Jonathan Mohr