CSCI 161 — Lab 1

Due: Tuesday, September 5, 2017 @ 12:00PM (noon)

Part 1

For this lab, you will log in to a PC running the Linux operating system and a GNOME desktop environment. You will open a terminal window, create a directory (folder) for storing your work for this course, create a small text file containing some basic information about you, and then submit the text file using our submit system. These steps are detailed below. Each step is summarized in bold and then described in more detail.

Goals

How to Use the Linux/GNOME Environment

Log in

You should see a login panel asking for your username. You will use your Millersville credentials (i.e., username and password). Type your Millersville username, which is a combination of your first initial, middle initial, and last name (not all characters may be used, and numbers may be added). E.g., Susie P. Que's username will likely be "spque". Press tab to move to the password field. Type your Millersville password. Be sure you use the number keys above the letters and not the numeric keypad. You won't see your password displayed on the screen. Press enter to login.

Open Terminal, Chromium, and Eclipse

Although GNOME presents a graphical user interface (GUI), you can interact with the system by typing commands. (If you connect to the lab machines remotely, you will use a similar terminal interface with typed commands.)

Once you complete the login process, you should see a desktop like the one in Figure 1, without the three windows (which you'll open later). Along the top is a task bar (similar to the task bar that's at the bottom of a Windows-based computer). The left end of the task bar displays the Activities button (it's clickable), and the right end shows a couple icons (a speaker and a power button).

Figure 1

Figure 1

Click the Activities button (you could also press the "Windows" key, the one to the left of the spacebar with the Windows logo on it). This will bring up the Activities screen as shown in Figure 2. Notice the Search Bar at the top.

Figure 2

Figure 2

Now, to bring up the three Windows in Figure 1, follow the steps below.

Open a Terminal window. Type the word terminal into the search bar and press enter.

Open the Chromium web browser. Type the word chromium into the search bar and press enter. Chromium is an open-source browser on which Google Chrome is based.

Open the Eclipse development environment. Type the word eclipse into the search bar and press enter. Ensure you do NOT open "Nsight Eclipse". IGNORE the Workspace Launcher dialog box for now. For now, focus on the terminal window. You will type commands in this window. If you wish to adjust its size, drag the lower right corner. Many of the mouse gestures work the same under Linux as in Microsoft Windows. However, the main purpose of the terminal window is to allow you to type text commands that perform actions in a Linux system, rather than doing things with a mouse. Notice the prompt message that is displayed in the Terminal

  [wkillian@boole: ~]$

followed by the prompt symbol (a black rectangle, which should actually be blinking). Your prompt will be different. The part after the [ and before the @ is the username, in this case wkillian and the part after the @ is the machine name, in this case boole. When a prompt is display the machine is ready for you to type in a command. The next several steps of this lab will all be done within this terminal window.

Create and Open 161 Directory

In the terminal, type mkdir 161 (with a space between the mkdir and the 161). This will create a new directory (folder) named 161 in your home directory (/home/students/<username>). Now type ls (for 'list directory contents'; this is the letter 'L' followed by the letter 'S', both in lower case) and you will see your new directory in the list.

You will create and modify your programs for this class in your 161 directory.

Move to 161 Directory in Terminal

The current directory is displayed as part of your terminal prompt:

  [<username>@<machine>: <working directory>]$

The terminal starts at your home directory every time you start a terminal session or log in. You can tell you're in your home directory when the prompt has a ~ (tilde) as the working directory (between the colon and the dollar sign).

In order to work with your files for this class in the terminal, you must make sure that the terminal's working directory is 161. Do this by typing cd 161 (cd is short for 'change directory') at the prompt. You can check what the current working directory is by typing pwd or looking at your prompt. The output of "pwd" should be /home/students/<username>/161. Note the 161 on the end. (Note: if it doesn't say 161 at the end, you are NOT in the right directory!)

Initializing Eclipse

Now, focus on the Eclipse window. We will be using the Eclipse Integrated Development Environment (IDE) in this class. You have already opened Eclipse via the search bar. It is also possible to open Eclipse from the terminal by typing eclipse & (with a space between the second e and the ampersand), but DON'T do this if you've already opened Eclipse. The & (ampersand) instructs the operating system (in this case, Linux) to run Eclipse in the background, leaving the terminal free for other commands.

Eclipse will display a window asking for your workspace directory (see Figure 3 below). Delete the word eclipse-workspace and replace it with 161 (See Figure 3). The workspace directory should now be /home/students/<username>/161. Click on the box below which asks if you want to use this as your default workspace directory so that this window is not displayed each time that you open Eclipse.

Figure 3

Figure 3

The first time you use Eclipse, it typically starts up with its Welcome window displayed (see Figure 4 below). Each of the icons in this window give you an introduction to some part of the Eclipse environment, except the one in the upper right which looks like an arrow.

Figure 4

Figure 4

That arrow will launch the Workbench. Go ahead and click it. Your window should now look similar to Figure 5 below. There are the usual toolbar and menus across the top, and three panes within the window labeled Package, Outline, and Task List, and a fourth currently unlabeled pane (the largest one visible). You can safely close the Outline and Task List panes; we won't be using them this semester, and closing them gets them out of your way and leaves more room for the Package Explorer pane.

Figure 5

Figure 5

Create an Eclipse Java Project

Create a new Eclipse project by selecting File -> New -> Java Project. You should see a dialog box similar to the one in Figure 6. Type Lab1 as the Project Name. In the Project layout portion of the panel, click the Configure Default link. On the new panel, click the Project button under Source and output folder. Click Apply. Click OK. Select the Use project folder as root for sources and class files button. Note that these settings should now be the default for future projects so you will not have to repeat this. But it is a good idea to check to see that this button is correctly selected each time. Finish the project creation by clicking the Finish button at the bottom of the window.

Figure 6

Figure 6

Now, create a new text file by selecting File -> New -> Untitled Text File from the Eclipse menu. This will create a blank text file that will be opened in an editor pane (called 'Untitled 1') in the middle of the Eclipse workbench. Your cursor will automatically move to the top of the blank editor pane.

Type your name and list the reasons you are taking this class. If your only reason is because the class is required, describe why you think it's required. Also indicate what you currently know about programming and computer science. Feel free to share something interesting about you, as well. Proofread your typing. You can use the mouse to move around.

Save the file using File -> Save As to save and name your file. Select Lab1 in the white middle pane so that it is displayed as the parent folder. Enter MyInfo.txt as the filename. Be sure to include the .txt extension. Click the 'ok' button.

Go back to your terminal window (if you closed it earlier, open a new one). In the terminal window, verify that you are in the 161 directory (type cd 161 if you're not). Type ls and you will see your new Lab1 project directory, which will contain any files associated with the project. Type cd Lab1 to enter the Lab1 directory. Type ls again and you will see your MyInfo.txt file.

To display the contents of your MyInfo.txt file, type cat MyInfo.txt. You could also type part of the filename (as in cat My) and then hit the TAB key to complete the filename.

Practice Some Terminal Commands

Learning a few terminal (Unix bash shell) commands will make it easier for you to use the system. You'll learn these commands better as time goes by. If you are connecting to the lab machines from outside the lab, you will get a terminal when you connect. In the terminal, type the commands in the left column of the table below. The right column describes what each command does. When you complete that series of commands, you should be back at your 161 folder. If not, figure out how to get there. Remember that you always need to switch to your ~/161 directory to work on files for this course.

Command Meaning
cd move to your home directory
pwd print the current directory name
ls list files in current directory
ls -l list files, last time changed, size, and permissions
cd 161 move into 161 directory
cd .. move one directory up in the file hierarchy
!p repeat most recent command that started with p (pwd)
cd 161 move into 161 directory
!! repeat the previous command. NOTE: this will give you an error because there is no ~/161/161 directory
man pwd show manual page for pwd command; press spacebar to advance down on the page if necessary; press q to quit`

Log Out

First, ensure that you do NOT turn off the machine. Other students in other courses need to use them too! You are only going to log out, not shut off the machine.

In the upper right corner of the screen, as shown in Figure 7, click the power icon. Then click in the area that displays your full name. A drop down menu with "Log Out" will become visible. Click "Log Out". You are logged out when you see the login panel on the screen. Before you leave the lab, you should log out this way. Save any work before logging out.

Figure 7

Figure 7

You will log back in for the second part of the lab.

Part 2

This activity will introduce you to the Java programming language. You will type in the Java program shown below, compile it, execute it, and then modify it. You will submit your modified program.

Goals

Sample Program

/* Filename:    Hello.java
 * Author:      Your name
 * Course:      CSCI 161.01
 * Date:        Today's date
 * Assignment:  Lab 1
 * Description: Traditional first program
 */

public class Hello {
  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    // Console input and output
    System.out.println("Hello, world!");
  }
}

Creating a Java Program

Open a terminal window and change your working directory to be your ~/161 directory (type cd ~/161). Now open Eclipse by going to the activities screen and typing eclipse and clicking on the correct icon. When the Eclipse workbench appears, your Lab1 project will appear in the Package Explorer at the left of the window. Your MyInfo.txt file will be open in the editor pane.

Make sure that Lab1 is selected under the Package Explorer pane on the left side of the window (it will be highlighted).

Create a new Java class file by clicking on the New Java Class button (it is a green circle with a 'C' and a little '+'). If by mistake you click the arrow next to the New Java Class button (which causes a drop-down menu to appear), you need to select Class (likely the top entry).

A new dialog box will appear as shown in Figure 8 below. Make sure that Lab1 is the source folder name (make it so, if it isn't). Type Hello as the Name.

Figure 8

Figure 8

Under "Which method stubs would you like to create?", click on the box to create public static void main(String[ ] args) so that a check mark appears.

Now click the Finish button.

In the editor pane, you will see a new tab entitled Hello.java. This file automatically has focus, i.e. it's where things that you type will show up. This file contains the basic outline or stub of a Java class called Hello — the first line should read public class Hello {. You will fill in this stub with the rest of the code shown above.

Begin typing in the Java program shown above, including the comment block above the line public class Hello {. Include your name in the comments, along with today's date. As you type statements, pay close attention to capitalization (some letters must be capitalized) and spacing, both within a line and between lines. You want your code to look exactly like that shown above.

Get into the habit of indenting the program as shown above. The Eclipse editor makes it easy to use consistent indentation by automatically indenting the next line after the enter key is pressed. If you press Ctrl-Shift-F, Eclipse will automatically indent all of your code.

This program has minimal comments. We will expect more on later assignments. Comments and indenting are essential to the readability of your program.

Type in the comment regarding console input and output, and the println statement, as I have done above. Note that Eclipse will attempt to assist you as you are typing in the code by suggesting possible completions to what you have typed so far. You may ignore these suggestions for now.

Once you've typed in the entire program as shown above, click the Run button (the green circle with the white triangle that looks like a play button). (If you accidentally click the down arrow beside the Run button instead, you will need to select Run…). If Eclipse cannot automatically create a launch configuration, a dialog box will appear. Highlight Java Application and click the new button (a page with a plus sign; if you hover, it says New launch configuration). Hello is added below the Java Application and it will be highlighted. The Name should default to Hello, the project to Lab1, and the Main class (which is the name of the class containing the method or function main) to Hello.

Click the Run button to launch your application! If you have not saved the Hello.java file, you will be prompted to save your files — you should click the box for Always save resources before launching to avoid being prompted to save in the future.

This program is a very simple example of console output. When you run it, the sentence Hello, world! will appear in the console window (located at the bottom of the workbench window in the Console tab).

Debugging Your Program

If Eclipse detects a mistake in your code as you are typing in your program, the line will be marked with a red wavy underscore. You should attempt to correct the error before running your program. If you do run a program and run-time errors occur, error messages will be shown in the Console pane. Continue to debug (correct) your program until you are able to run it without exceptions.

Modifying the Hello Program

For this next activity, you will modify the program you created above. You will make two changes.

  1. Make at least Two Small Changes
  2. Edit the program to modify the console output.

Have the program greet you by your first name, for example, Hello, Jane! instead of Hello, world!.

Add another println statement below the first to print out something interesting, cool, or fun.

Finally I want you to make the program print out what you wrote in MyInfo.txt. Copy and paste all of the lines from the text file and enclose them in println. When the program runs, I should see:

  1. The modified program output of Hello, <name>
  2. Some other text
  3. A list of sentences describing yourself (follow the directions from Part 1)

Run the Program

This time, you can just click the Run button.

Submitting the lab

We will be using Autolab (http://autolab.millersville.edu) for assignment submissions this semester.

First, you should see the login page for Autolab, as shown in Figure 9

Figure 9

Figure 9

Next, you should click the Reset Password Link and enter your Millersville email address. You'll receive an email which allows you to create a password. Figure 10 shows the Password Reset form.

Figure 10

Figure 10

Once you log into Autolab, you should be presented with a CSCI 161 Course Page and a Lab 1 Link. Figure 11 shows the course page view. Click on the Lab 1 Link.

Figure 11

Figure 11

The lab 1 assignment page should then be shown. To submit your Java file, click the Submit File button and browse to the location of your Hello.java file (~/161/Lab1/) and click Choose/Okay.

Once you upload your submission, you will be presented with an upload page, depicted in Figure 12.

Figure 12

Figure 12

You will be able to view your score after waiting about 10-15 seconds. You may click on the Score link to see the output of the autograder. A sample output is shown in Figure 13.

Figure 13

Figure 13

Congratulations! You have completed the first lab!