Copy the lab materials to your account. A single directory contains all the programs that you need for this lab. You will need to use the -r option on the Unix cp command to copy not only the files in this directory, but also any subdirectories. To copy, enter
cp -r /home/Classes/Cs1/Labs/Lab02 .Don't forget the dot (.) at the end of the command. After running this copy command, you will see a new directory in your account called Lab02. What you copied was actually a directory, plus all of its contents. Everything you need for the lab exercises today is contained in this new directory.
Change directory to Lab02.
Sometimes programmers use code that is already written. Java programmers have to be able to find details about the pre-written Java classes they use. We will use classes that are standard (included with the Java language), classes that other programmers have written, and also classes that you define yourself. Online documentation is always available for the standard Java classes, and often, online documentation is available for other programmer-defined classes as well.
When you have a window-oriented operating system on a machine, the main screen you see is called a 'desktop'. Using Linux and Gnome, you have the potential to work with four 'virtual' desktops (in other words, there are multiple desktops available that you can see one at a time). An easy way to keep Java documentation handy is to have your browser running in one virtual desktop while you write your program in another virtual desktop.
Make sure you have a browser running in one of your desktops. From your browser, go to the web page http://java.sun.com/j2se/1.4.1/docs/api/index.html. This is a link to documentation for the standard Java classes.
An 'Application Programmer Interface' (API) is a collection of software tools that are prewritten for programmers to use. For Java, these tools are called the standard Java classes. In your browser, look at the API Specification window scroll down through the Java 2 Platform packages until you find the package javax.swing. It will be further down this page. Click on it to bring up a page labeled 'Package javax.swing'.
Scroll down to the class summary (past the interface summary) and
click on the JFrame class. This will bring up information
about the JFrame class. You can find information about
other standard Java classes in the same way.
Find the answers to these two questions:
Locate the file called Lab2Program1.java in your current directory. Compile this program and run it, carefully observing its behavior. You can close the window that this program creates by clicking on the 'X' in the top right corner of the window. You may also need to type Ctrl-C to terminate your program.
In an editor window, open the file Lab2Program1.java and examine the code. Notice that this program declares a JFrame named window. It then constructs an instance of the JFrame, followed by setting its size, title and location. Finally, the JFrame is set to be visible, which causes the window to appear on your desktop.
The last message sent to the JFrame object makes the window visible. How can you modify this message so that the JFrame object will not be visible when you run your program? Make your change, compile and run the class again. Does it behave the way you expect it to?
Change the code back to the way it was before, and comment out the message that tells the JFrame object to be visible. Compile and run the program again. Observe its behavior. This shows the default visibility of a JFrame object.
Prepare answers to these questions:
Notice that there is another class used in this program, the Point class. Observe that both the JFrame and Point classes are fully qualified by their package names in this program, namely javax.swing and java.awt, respectively. Add import statements to your program so that the classes can be referred to as simply JFrame and Point.
Compile and run your modified program and observe that it behaves the same.
3
Show us your modified program.
Modify your program to change the size and position of the JFrame to other values of your choice. Compile and run your program and observe the results of your changes.
4
Show us your modified program source code and runtime behavior.
At end of your program code Lab2Program1.java, add a statement that sends a message to the JOptionPane class to create a dialog box that says "Click OK to close me" in the center of your display. Note that you use a null first argument to the showMessageDialog method to do this. Compile and run your program, and observe its behavior.
Now modify the message sent to the JOptionPane class so that the dialog box appears in the center of your JFrame window instead of the center of your display. Compile and run your program and observe its behavior.
5
Show us your modified program source code and runtime behavior.
Open the program Lab2Program2.java in your editor and examine the code. Compile and run this program. Notice that there is a problem with the output: there is no space between your first and last names. Modify the code so that the output will have a space between them.
6
Show us your modified program source code and runtime behavior.
Instead of displaying your full name (first plus last),
modify your program so that it displays your first and last
7
Show us your modified program source code and runtime behavior.
Modify your program to have a single input dialog to enter your full name, with a single space between your first and last names. For example, you might enter "Belle Tway" as your full name. Your program should have an output dialog that simply displays the name you entered. Declare another String object called fullName to use in your input dialog. Compile and run your program to test that it works.
Now modify your program so that it extracts your first and last names from your full name. For example, if you input "Belle Tway", your first name is "Belle" and your last name is "Tway". Use the declared objects firstName and lastName to refer to these strings. Replace your original output dialog with two output dialogs, the first one giving your first name and the second one giving your last name. For the input "Belle Tway", your first output dialog should say "Your first name is Belle", and your second output dialog should say "Your last name is Tway".
8
Show us your modified program source code and runtime behavior.