CSC 120(A) - Principles of Computer Science I Prof. Nadeem Abdul Hamid Fall 2004 Lab 2 Thursday, September 2, 2004 Lab session: To begin, start the "BlueJ" program and choose "New Project" from the "Project" menu. Navigate to your home directory (in the "S:\" drive or else in "M:\HOME\...") and give your project a name like "CSC120_Lab02". BlueJ will now create a folder with that name in your home directory. For each program (class) that you have to write or edit for the lab exercises below, make sure you have this project open in BlueJ and click on the "New Class" button. Type in the appropriate name for the class (watch spelling and uppercase/lowercase) and press "OK". Now you can double-click on the icon that BlueJ makes for your new class and go through the contortions of selecting all the stuff BlueJ puts there, deleting it, and copying or typing in the new stuff, as we did in Lab 1. Remember to put header comments at the top of each file that you write. This way, you will have one project (and one folder) for the entire lab, and each individual exercise or program will be a class in the same BlueJ project. Follow this same approach for future labs as long as we are using BlueJ. In addition to having the Java source code files for this lab posted on Viking Web, I will try to have a few floppy disks with the files for this lab on them so you can just copy them to your hard drive without having to deal with the problems of downloading files from Viking Web. The sample program in the Prelab (pages 28-29) may be helpful when completing Lessons 2-2 through 2-4 (although it is missing a closing brace, } , on line 22 on page 29). As before, you may hand in your written answers at the end of the lab session or else upload them to the Coursework section of Viking Web. Exercises: 1. Do the Prelab assignment on page 33. The answers are on page 35 so you can check yourself. If you do not understand any of the answers, ask the instructor. We have not discussed constants in class yet -- "constants" are data or values that don't change throughout the entire program. "Named constants" are like variables, which associate an identifier with a value, except that unlike variables, the value of a named constant cannot be changed once it is set. "Literal constants" are literal data values you type in your program, like a number 345 or a string "Hello" or a character 'A'. 2. Lesson 2-2, all exercises. Upload your final "Greet.java" program to the appropriate Coursework section on Viking Web. 3. Lesson 2-3, all exercises. Upload "Shell.java" to the Viking Web. 4. Lesson 2-4. To begin working on Lesson 2-4, create two new classes in your BlueJ project, one named "Message" and the other named "TestMessage". Exercises 1, 2, 3, and 5 of this lesson should be typed in the "Message" file and Exercises 4 and 6 go into the "TestMessage" file. Thus, in Exercise 6, where it says "Fill in the body using results of exercises 1-5" instead just fill in the body using only the result of exercise 4. In Lesson 2-4, Exercise 1, the last blank extends after the semicolon (name = _____________;___). There is nothing that goes after the semicolon -- it's just a typographical error. In Lesson 2-4, Exercise 5, you should use "public class Message" instead of "static class Message" to define your Message class. Upload your final "Message.java" and "TestMessage.java" files to Viking Web. In addition, print out a copy of these files for yourself so we can discuss them in class tomorrow. 5. Lesson 2-5, all exercises. When you copy the program Dinner2 into BlueJ, it should have the class defined as "public class Dinner2 {" instead of just "public class Dinner {". If you finish early, it might be a good idea to use the time to work on your programming assignment for homework 2.