CS46B Lab 2

Copyright © Cay S. Horstmann 2010-2015 Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

Modified by:

Instructions

Working in Pairs

Objectives

Learning Outcomes

A. Removing Directory Entries

  1. Start with lab2.zip and create a project lab2, following Step 1 of these instructions. You'll need the file deptdir.txt again. If you don't have it any longer, download it as deptdir.txt onto your computer.

    Run the AddressBookDemo (Check Lab 1: E.1 in case you forget how.). In the file chooser, navigate to whereever you saved the deptdir.txt file, and click on Open.

    Look up the phone number of your Dr. Khuri. (Enter the number of the command. The key is Phone.)

    Then select Remove Entry.

    What happens? Why? (You'll need to read through the source code.)

  2. In the AddressBookDemo class, implement the removeEntry method, using the lookupEntry method for guidance.

    What is the code of your removeEntry method?

  3. Start the program again as described before and select Remove Entry.

    What happens? Why? (You'll need to read through the source code.)

Ok, we'll need to implement the remove method of ArrayListAddressBook. And then we'll need to test it. Chances are you won't get it right the first time.

In that case you'll run the program, select the directory, click Open, select Remove Entry, find it didn't work, run the program, select the directory, click Open, select Remove Entry, find it didn't work, run the program, select the directory, click Open, select Remove Entry, and on and on and on. That's a lot of clicks.

For now, we will not implement the remove method, but learn how not to click so much.

B. Launching a Terminal Window

If you need to repeat the same set of operations many times over, there is a better way: the command line interface. The command line interface is harder to use initially, but it gives you a lot of power.

Look at it this way. If you just want to snap vacation photos, you'll use a "point and shoot" camera. It requires no training and takes decent pictures.

.

But if you want your photos to be published in a magazine, you will need a professional camera. You will need to spend time learning rather technical concepts such as f-stops and shutter speeds.

.

In this lab, you will begin to learn the command shell. You will use the command shell throughout the semester for many tasks.

On Lubuntu, type Ctrl-Alt-T to open a terminal or launch the LXTerminal app in the Accessories menu.

If you know how, you can also use the terminal on Mac OS X, but you are completely on your own. You cannot do this lab with the Windows terminal.

You will get a window that looks somewhat like this.

Never seen one of these before? Congratulations—you have just reached level 2.

C. Viewing Directories

  1. You type commands into the terminal, and you get responses in plain text.

    After each command, you hit the Return key. Let's try it out. Type ls followed by Return. (ls stands for “list”.)

    What happens? If nothing interesting happens, try ls -a instead (i.e. ls, a space, a hyphen followed by a without any space, and finally Return. This shows all files, including the “hidden” ones.)

  2. When you first open a terminal window, you are in your home directory. Its location depends on the operating system and your user name. To see exactly what that directory is, type pwd followed by Return.

    What is your home directory?

  3. To see the contents of another directory, use ls followed by the directory name. Try listing the contents of your downloads directory. In Lubuntu, it is located at /home/user/Downloads. On the Mac, if you don't already know or you can find it yourself, this would be a good time to launch the VM. What files did you find?

    Let's try that again. Try typing ls /home/user/D, then hit TAB.

    What happens when you hit TAB?

  4. This auto-completion feature is similar to the Ctrl+Space feature in Eclipse, and it is a huge time saver. Experienced shell users almost never type a full file name but use TAB whenever possible. It is faster and it cuts down on errors.

    Next, let's find the lab2 Eclipse project. The details depend on where you unzipped the lab2 folder. If it's on the desktop, try a directory such as /home/user/Desktop/lab2. You aren't actually using the desktop, right? You should have it in some place such as /home/user/cs46b/lab2.

    If you are using a virtual machine, but did part A outside the virtual machine, you need to find out how to access the “outside world”. Look in the /media directory for a link to the home directory of the host. If there is no such link, you need to repeat the installation of part A inside the virtual machine.

    In which directory is your project? What happens when you type ls followed by that directory?

  5. Another useful feature of the shell window is command line recall.

    Hit the ↑ and ↓ arrow keys a few times. What happens?

  6. Now use the ↑ and ↓ arrow keys to recall the command that lists the lab2 directory, then add path_to_your_project/bin (without a space) after lab2.

    What files are listed in that directory?

    These are the compiled files of your application.

D. Copying Files

  1. The cp command copies one or more files. We will use its simplest form here. We want to copy the deptdir.txt file to the current directory.

    Where did you put the deptdir.txt file? Give the full path name.

  2. Now type

    cp -v /path/to/deptdir.txt .

    Replace /path/to with the actual path to deptdir.txt, such as  /home/user/Downloads/deptdir.txt.

    Type a space and a period after the full path.

    The period indicates the current directory (the one that is printed by pwd). You are copying the file to the current directory.

    The -v indicates the “verbose” option, in which you get some feedback what happens.

    What feedback did you get?

  3. Now type

    ls

    What happens? Why?

  4. We will soon be modifying our deptdir.txt file. Let's make a copy to be safe. Copy deptdir.txt to deptdir.original.txt in the current directory, using the cp command.

    What command did you use?

E. Running a Java Program

  1. Type javac -version followed by the key labeled ↲, Return, or Enter in the shell window.

    That's javac, not java.

    What happens?

  2. Now type java -classpath followed by the name of the full path to lab2/bin and AddressBookDemo. For example,
    java -classpath /home/user/cs46b/lab2/bin AddressBookDemo

    Be careful with the letter case in AddressBookDemo.

    Don't type this from scratch. Hit ↑ until you find the ls command from C.6. Add a space and AddressBookDemo. Then hit Home (Ctrl+A on the Mac), erase the ls and add java -classpath. Hit ↲ when you are done.

    What happens when you run the AddressBookDemo?

  3. Don't bother entering a file name into the file chooser. Just hit Cancel. We'll work on that later.

    Now run the program again, using only two keystrokes.

    What are the keystrokes?

F. Installing a Text Editor

You need a professional text editor for this lab. Mac TextEdit or Lubuntu gedit will not suffice. Some students have a professional text editor that they like and know, but more often than not, it turns out that they don't actually know it all that well. So, even if you love your text editor, you'll need to install the one that we use in the lab. 

The professional text editor that we use in this lab is called Emacs. It is available on all major operating systems, so you won't have to learn another editor if you switch operating systems—which most professional programmers need to do at least occasionally. It is very powerful and has a huge set of extensions. 

“Emacs outshines all other editing software in approximately the same way that the noonday sun does the stars. It is not just bigger and brighter; it simply makes everything else vanish. ”—Neal Stephenson

If someone tells you that Emacs is old-fashioned and hard to use, don't listen—modern versions of Emacs have menus and dialogs and require no special training.

Emacs is already installed on the Lubuntu VM. Mac users can install Emacs for Mac OS X, but as always, they are on their own working out the details.

  1. Start Emacs. In Lubuntu, pick Emacs from the menu. Or, in a terminal, type

    emacs &

    Select Options -> Use CUA keys(Cut/Paste C-x/C-c/C-v) and Options->Save Options. At the bottom of the window, in a ”status line”, Emacs tells you that it saved the options in a file.

    (If you can't see the status line, maximize the Emacs window.)

    What is the name of this file?

  2. Select File -> Open File..., click the "Show Hidden Files" checkbox, and select the file named .emacs. Note the period (.) before the filename.

    (Mac OS X users: If you do not see such a file, you now know why we keep pestering you to use the virtual machine. For now, cancel the file dialog and type the key combination C-x C-f .emacs RET, that is, hold down Control and X, let go of X but not of Control, type F, let go of both, then type the six characters .emacs behind the ~/ and then hit the Return key.)

    At the end of the .emacs file, add the following lines:

    (prefer-coding-system 'utf-8-unix)
    (setq indent-tabs-mode nil)
    (setq require-final-newline t)

    Just copy from this page and paste it in with Ctrl+V.

    Select File -> Save from the menu.

    What does the status line at the bottom of the window say?

  3. Exit Emacs and start it again. Use File -> Open and open the file ~/.bashrc.

    Note the dot (.) before the letter b. It is a part of the file name.

    The ~ character denotes the “home directory” of the current user.

    What is the contents of the file?

  4. Open a terminal window. A few letters will appear in the top left corner. These letters are called the “prompt”. Typically, they contain one or two lines of text, including the name of the current directory. Sometimes, they contain the user name.

    What is the prompt in your system?

    Close the window when you are done.

  5. At the bottom of the file that you opened in step 3, add a new line
    export PS1='\w\$ '

    exactly as it appears here, as a string of eighteen characters, including two spaces, two backslashes, two single quotes, and two uppercase letters. Just copy/paste it.

    Save the file. Open another terminal window.

    What prompt do you get now?

    PS1 is an environment variable, a variable that controls the behavior of the shell. PS1 sets the prompt string. The \w sequence denotes the path to the current directory. The \$ sequence yields a $ for a non-administrator, a # for an administrator. (This is useful to alert you when you are logged in as an administrator.)

    Mac users—this probably didn't work for you, unless you configure your system so that it loads .bashrc—Google for "bashrc mac". Or you could just launch the VM.

G. Entering Special Characters

  1. Open the deptdir.txt file in Emacs. At the bottom, enter
    Peña
    Email
    Jose.Pena@sjsu.edu
    Phone
    (408) 924-5060
    

    How did you enter the ñ?

  2. In this case, it was easy to copy/paste the special character. Or maybe you know how to set your keyboard to type Spanish characters? Emacs can do better—you can type in any Unicode character. Type C-x 8 ~ n, that means Ctrl-X, then 8, then ~, then n. What happens?
  3. Type C-x 8 3 / 4, that is Ctrl-X, then 8, then 3, then /, then 4. What happens?
  4. Now highlight the ¾ character and type M-x describe-char, that is, hit Alt+X followed by describe-char and the Return key.

    What happens?

  5. There is a lot of information here, but the most important are the Unicode code point, which is shown as decimal 190, and the Unicode name VULGAR FRACTION THREE QUARTERS.

    What are the Unicode code point and name of ñ?

  6. Now type C-x 8 RET, that is, Ctrl-X followed by 8 and the Return key. Then type VULGAR and hit the TAB key. What happens?
  7. Hit TAB again. What happens?
  8. Pick one of them with the mouse. What happens?
  9. Type C-x 8 RET and SMILING. Hit TAB twice until you get a similar list. Pick SMILING CAT FACE WITH HEART-SHAPED EYES. What happens? (If you got a funny looking box, then select Options -> Set Default Font and change the font to Sans Regular.)
  10. What is the code point of that character?
  11. Visit this page. Where can you find the code point?
  12. You were probably told in your CS46A class that a Java String is a sequence of 16-bit char values, each between 0 and 65535, which represent the Unicode code points. How can you tell now that they didn't tell you the whole truth?
  13. Highlight the word Email (any one of them), and type M-x morse-region RET. That is, Alt+X, morse-region, and the Return key. What happens?
  14. Hit C-z to undo that. Also, to get rid of that other window with the character information, hit C-x 1.
  15. If you have a favorite editor, how do you insert and inspect special characters in it? How do you convert to Morse code?
  16. If you are feeling depressed now, type M-x doctor.