This is the second lab for CPSC 124: Introduction to Programming. In the first part of this lab, you will use some graphics subroutines to draw a picture on the screen. In the second part, you will use input statements to get information from the user as well as output statements to display information. One of the main points that you should note is that Java lets you use subroutines that have already been defined by other people to perform complex tasks. You don't need to understand how the subroutine works in order to use it -- you just need to know its name.
The exercises for this lab are due in lab next Tuesday. The programs that you write should be in your cs124 directory, so that they can be tested.
For Fun: Open a command-line window, and type in the command fortune. You'll get a quote randomly selected from a large collection of quotes (with probably less than half of them being interesting or amusing). If you would like to see a "fortune" every time you open a command window, edit the file named .bashrc. To edit this file, use the command nedit .bashrc noting that there is a period at the beginning of the file name. Add a line to this file that says fortune. (If you already had a file named .bashrc, you can add this line to the end of the file. If not, you can make a new file that only contains this line.) Save the file. The .bashrc file is a list of commands that are executed whenever you open a command window. You can put other commands in .bashrc to customize your command-line environment. One common thing is to define "aliases," which are abbreviations. For example, if you get really tired of typing "javac" and would like to be able to abbreviate it to "jc" you could put the command alias jc=javac in your .bashrc file.
Practical Knowledge: Although you have to use the command line for the javac and java commands, you can do most things in Linux using the graphical user interface. For example, you can open a window where you can browse through your files. Clicking on the small icon at the bottom of the screen that looks like a house will open a window on your home directory. You should see the cs124 directory in your home directory. Click the cs124 icon once (or twice, if you've configured Linux for double-clicking), and you will see the files that you created last week. Right-click the file window to see a menu of actions that you can perform, including creating new files and folders. You can cut-and-paste and drag files and folders between file windows, just as you are probably used to doing in other operating systems.
The thing about a subroutine is that once it's written, you can forget about how it works and just remember its name and the task that it accomplishes. If someone else has written a subroutine for you, you can use it without having any idea how it works. In fact, Java comes with a huge number of predefined subroutines, and becoming a skilled Java programmer is largely a matter of leaning about them. In this part of the lab, you will use some of Java's built-in graphics subroutines to draw a picture on the screen.
You will need copies of several files to do this lab. The files are in the folder named /classes/f05/cs124/lab2. You should copy this folder into your cs124 folder. To do this on the command line, change to the cs124 directory, if you have not already done so, with the command cd cs124. Then use the following command to copy the directory:
cp -r /classes/f05/cs124/lab2 lab2
The cp command means "copy". The "-r" in this command means to copy an entire directory, rather than just a single file. You can also use the file window interface, described above, to copy the directory: Open a file window, enter /classes/f05/cs124 into the "Location" box at the top of the window, and then either drag or copy-and-paste the lab2 folder into your cs124 folder. (Alternatively, you can get the files you need off the Web using these links: Picture.java and TextIO.java.)
Compile your copy of Picture.java with the javac command, then run the program using the java command. A window will open showing a very simple drawing. The picture will stay on the screen until you close it by clicking the window's close box.
Use nedit to edit the file Picture.java. (To open the file using the GUI interface, right-click the file icon, select "Open With", then select "Other". Type nedit into the dialog box that appears, and click OK. If you check the "Remember application association for this type of file" box before clicking OK, then in the future you can open a java file with nedit just by clicking the file icon.)
Picture.java has a rather strange looking main routine, which you don't have to understand at this point. The picture is actually drawn by another subroutine called paintComponent. You can change the picture that is drawn by changing the code inside this subroutine. There are several subroutines that you can use for drawing:
Technically, g is an object, and the subroutines are inside that object, but that is not important here. What is important is understanding the x and y. The x and y are coordinates that specify a point in the picture. The x specifies the number of pixels over from the left edge of the picture. The y specifies the number of pixels down from the top of the picture. Since the picture is 500-by-500 pixels, both x and y can be in the range from 0 to 499.
Exercise 1: The first exercise for this lab is to modify Picture.java to draw a different picture. While it does not have to be extremely elaborate, it should be significantly more complicated than the picture drawn by the original program. If you can, try to draw something recognizable, such as a snowman, sailboat, house, or truck.
The graphics subroutines used in Part 1 of the lab are a standard part of Java, so you don't need to do anything special to use them. However, it is also possible to use non-standard subroutines, as long as you have the files that define them. The file TextIO.java defines some non-standard subroutines that can be used for command-line input and output. This file is discussed in Section 2.3 of the textbook, and we have already looked at some aspects of it briefly in class. For this lab, the only TextIO subroutines that you might need are: TextIO.getlnInt(), TextIO.getlnDouble(), and TextIO.getln(). Remember that each of these subroutines returns a value that you will want to assign to a variable, as in:
name = TextIO.getln();
Remember that since TextIO is not a standard part of Java, you need a copy of TextIO.java (or at least TextIO.class) in the same directory as the main program that uses it. The lab2 directory already contains a copy of TextIO.java, so if you create your program in the lab2 directory, you will be OK. For output, you can use the standard subroutines System.out.println() and System.out.print().
Exercise 2: Write a program that has a conversation with the user. Ask the user's name, and then greet the user by name. You could ask the user's birthday, favorite color, a movie they've seen recently. For full credit, do some simple calculations with the user's input. For example, ask them what year they were born in, and tell them how old they are. Or ask them how tall they are in inches, and convert their height into feet or meters. Try to use your imagination.