CPSC 124 Introduction to Programming Spring 2024

Lab 1
Introduction to Linux and Java

Due: Tue 1/30 at the start of lab


Introduction

This lab will introduce you to the computing environment used in CS courses at HWS.

Successfully completing this lab means that you have correctly set up your account for use in this course and will be able to:

This handout contains a lot of information. Don't worry about memorizing all of the details right now! Make sure you understand the material as you work through the lab, but the main thing is to be aware of what information there is so that you can come back and look up the specifics when you need them.

To distinguish between informational instructions (telling you how to do something) and steps you should actually carry out to complete the lab, steps are marked as shown below.

Look for the box! Steps you should actually do as part of the lab are distinguished like this.

This handout is meant to be read in order, from beginning to end, and the boxed steps should be done as you encounter them. However, you should look through each section or subsection in its entirety before beginning to actually carry out any actions contained in that section, so that you understand the context of what you are doing and also so that you notice if there are important explanatory details about a particular step written after the step itself.

Ask if you're not sure how to do something, or if what you do doesn't seem to work correctly (for example, you get an error message and you aren't sure why). Computers demand that you get every detail of each command exactly right in order for it to work, and those details can be pretty small - even just whether something is capital vs lowercase.

You should still read carefully through the whole handout even if you have used Linux before, as you might not be familiar with the specific things you will need to use in this course. You will also need to do boxed steps in order to be ready for this course.

Collaboration

Make sure you have read and understand how academic integrity applies in this course (and specifically how it applies to labs).

For this lab, all of the marked steps must be done by each person individually, and everyone needs to be able to navigate the Linux environment for themselves. It's fine to give and receive help, and even to go through the steps collaboratively with someone else, but everyone needs to actually carry out the steps and be able to navigate the Linux environment for themselves.)

For the questions in the exercises, the usual collaboration rules apply: help (such as a hint about where to find the information) is fine, but you may not work together with others to answer the questions.

About Due Dates

Labs are due at the start of lab on the date listed. It is OK to take a few minutes at the beginning of lab to hand in the previous lab, but you should not be spending part or all of a lab period finishing the previous week's lab.


Linux

Computer science courses at HWS make use of an operating system called Linux. An operating system is the basic software that runs the computer - it controls the hardware of the computer (such as disks and memory) and runs application software (like word processing programs and web browsers). It is the first thing loaded when the computer starts up. You are most likely already familiar with the Microsoft Windows operating system (Windows 10 and 11 are the most recent versions) and/or the Macintosh operating system (macOS Sonoma and Ventura are the most recent versions). Linux is a version of Unix, an operating system often used for servers and in academic environments. One reason for Linux's popularity is that it is free and open source, meaning that not only can you download and run it for free, the underlying code is available to anyone to add to and modify.

There are actually many different versions of Linux, known as distributions. You'll encounter two different distributions in this course: the computers in the labs run a version called Linux Mint, while the VDI environment uses Ubuntu. All versions of Linux work the same way underneath, but different distributions may look different or be configured differently.

Accessing Linux

There are two ways to access a Linux desktop at HWS: in the Rosenberg 009 and Lansing 310 computer labs, and through the Linux VDI (Virtual Desktop Interface). This section will only address using the lab computers.

The computers in Lansing 310 run only Linux. If the computer is off, you'll need to turn it on and wait for it to boot, but otherwise, when you sit down at one of those computers you should see the Linux login screen.

The computers in Rosenberg 009 are dual-boot, meaning that they are configured to run both Windows and Linux. Only one operating system can control the computer at a time, so if the computer is off or running Windows when you sit down, you will need to reboot it to switch to Linux:

As the computer restarts, you will eventually see a screen with a menu that allows you to choose between Windows or Linux.

Wait some more, and eventually you will get the Linux login screen.

You will be provided with a username and password in lab. Note that while your Linux username might be the same as your HWS email login, this is a separate account from your HWS network account and has a separate password. (This also means that if you change the password for one account, it will not affect the other.)

[Side note: if you were in CPSC 120 last semester, this is a new account with a lowercase username. Use this account going forward, as the uppercase-username accounts created last semester will eventually be removed. Stop by office hours for help with moving files you want to keep from your old account to this one.]

It may take a little while for the desktop to start up, especially the first time you log in. You may also get some windows popping up with welcome information or tips about configuring your desktop. It's OK to close or otherwise dismiss those windows (ask if you get one that you aren't sure about). If you don't want the pop-ups every time before you log in, look for a little checkbox you can select to turn off this behavior before dismissing the window.

You should end up with something similar to the following: (though your desktop probably won't look exactly the same - there are lots of things you can customize)

desktop

Navigating the Linux Desktop

Unlike Windows and MacOS, where the desktop that you interact with is bundled with the rest of the operating system, Linux supports many different desktops with different features and appearances. (There can even be different desktops available within a single Linux distribution.) The desktop used with Linux Mint at HWS is called Cinnamon.

The Cinnamon Desktop is most similar to the desktop in Microsoft Windows, but there a number of things in common with both Windows and MacOS - as you explore Cinnamon, keep in mind what you already know about other systems to help you figure things out.

The bar running across the bottom of the screen is known as the panel; Windows calls it the taskbar.

panel

The panel can be configured to your liking, but it commonly has the elements described below. (Again, what you see may be a little different than what is shown, but you can mouse over the icons you see to get popup text describing what each is for to help you figure things out.)

Finally, on the desktop itself, you may see two icons labeled "Computer" and "Home" in the upper left corner - these provide access to your files. (More on that later.)


Logging Out

When you are done working, it is important to log out - this allows others to use the computer, and also protects your account and files from others who might come along.


Continuing On

To save paper, the remainder of this lab and all other labs for the course will be only posted online.

Once Firefox starts up:


More Linux

Changing Your Password

You should change the random password you were assigned to something only you know and which is easier for you to remember. You can use the same password as your HWS network account if you want or something different. (Just remember that your CS account is separate from your HWS network account, so changing the password for one does not affect the other.)

You can use any characters except spaces in your password - you should avoid real words (including real words spelled backwards), and use a mix of letters and non-letters. Also, your password is case-sensitive, so make sure the "Caps Lock" key isn't on unless you mean for it to be.

To change your password, you'll need to make use of the command line. As with Windows and Mac, much of your interaction with Linux can happen by clicking on icons and menus. However, some tasks - like changing your password - can't be done this way and instead require you to type in the command you want to run. (Both Windows and Mac also provide a command line interface - perhaps you've used it.)

To access the command line, you need to open a terminal window:

To change your password:

If your password was successfully changed, you should see a message to that effect after you press Enter the last time.


Working With Files and Directories

As in any operating system, files in Linux are organized into folders (often called directories in Linux). Each folder can contain files and/or subfolders, which in turn contain files and/or subfolders, and so forth.

Cinnamon provides a tool similar to the Windows File Explorer or the macOS Finder for navigating directories and managing files and folders.

To start the file manager:


Navigating Directories

To open a folder, you can double-click on a folder icon as you would in Windows or on the Mac.

For a file or directory, the sequence of directories containing that file or directory is known as its path. The file manager shows the path for the directory shown in the current view near the top of the window. Click on a folder name in the path display to jump to that folder.

You can back up to a previously-viewed folder by clicking on the back arrow in the upper left corner of the file manager window.

You can also go straight to certain directories by clicking on one of the entries displayed in the "My Computer" and "Bookmarks" sections on the left side of the file manager window.


Special Directories

Home Directory

You have a home folder (or in Linux terminology a home directory), which contains your personal files. (This is similar to "My Documents" in Windows.)

To go to your home directory:

If the directory currently being viewed in the file manager is contained within your home directory, you can also go to your home directory by clicking on the house icon (also labeled with your username) in the path display.

Note that while everyone has a home directory, each user has their own home directory. (My home directory is not the same directory as yours!)

Root Directory

Another special directory is the root directory. This is the directory at the top of the file system - all other files and folders are contained, either directly or indirectly, within the root directory. In other words, you can start at the root directory and repeatedly open subfolders to reach any other file or folder on the system.

To go to the root directory:

You can also get to the root directory by looking for a similar icon in the path display. (If the first element of the path display has the home icon, click on the left arrow on the left end of the path display.)

Path Names

For some special directories, like the root directory and your home directory, you can simply say "the root directory" or "my home directory" and people know what you are talking about. However, for other files or folders, the only way to specify which thing you are talking about is to explain how to find it - and it is useful to have a concise way of writing down this explanation. The exact representation varies from operating system to operating system, but Linux concatenates the names of each folder from some starting point to the folder containing the item, putting a slash (/) between each folder name. This is known as the path to the item. For files, the name of the file is appended to the path.

Note: details matter! ~bridgeman means bridgeman's home directory while ~/bridgeman means the directory called "bridgeman" within your own home directory.

/classes/cs124 will contain files that you need for labs and projects, as well as the folder where you will hand in your programs.

Creating Folders

To create a new folder, navigate to where you want the folder to go, then right-click on the background of the file manager window and choose Create New Folder from the popup menu. (Be careful not to right click on any particular file or folder.) This will create a new folder called "Untitled Folder", with the folder name highlighted and ready for editing - type in the new name.

To keep things organized, you should create some directories to hold the files you'll create for this course:

Naming Files and Directories

Names are case-sensitive - capitals and lowercase are different.

Stick to alphanumeric characters (letters and numbers) and the symbols - (dash) and _ (underscore). Some other characters, such as spaces, are legal to include but require special handling when using the commandline, which causes problems when I am printing your programs for grading. Please don't use spaces in file and directory names!

Renaming Files and Folders

To rename a file or folder, right click on the item you want to rename, select "Rename...", and type in the new name of the file. Make sure you include the appropriate extension (e.g. ".txt" with text files).

Selecting Files and Folders

You can select files and folders like you do in Windows - click on a file or folder to select just that item, or click on a file or folder and then shift-click on another file or folder to select the range of items between the two, or click on a file or folder and then ctrl-click on others to include them, or click in the background of the file manager window and drag to select all of the files/folders in a region.

Moving and Copying Files and Folders

To move files and folders from one place to another, you can open two file manager windows, navigate to the folder containing what you want to move in one and the target location in the other, and drag the file(s)/folder(s) from one window to the other.

You can also select the item(s), cut or copy, navigate to the target folder, and paste to move or copy files. Access cut/copy and paste by right-clicking on a selected item (cut/copy) or in the background of the file manager window (paste).

Deleting Files and Folders

To delete file(s) and/or folder(s):

Both of these options cause the selected item(s) to be moved to the trash. The trash can be accessed via the Trash item on left side of file manager window if you want to restore an item or empty the trash. (There is another menu item "Delete" - using this instead of "Move to Trash" causes the item to be removed immediately. Use this option with caution, since you can't restore an item deleted in this way.)


Java

Programming in Java involves three steps: creating a file containing the text of the program, compiling the program to translate the Java instructions to a lower-level language that is closer to something the computer can actually run, and then actually running the program.

VS Code

In order to create or edit a Java program, you need to use a text editor. Text editors are different from word processors like Microsoft Word because they are only concerned with editing plain text - they don't do all the formatting with fancy fonts, colors, tables, pictures, and all the things you can do in Word.

Text editors intended for use by programmers often provide additional useful features:

We will be using a tool called VS Code for Java programming. This tool is freely available, multi-platform (so you can run it on Mac and Windows, as well as Linux), and provides the useful features just mentioned. Actually, VS Code goes far beyond these basic features, aiming to build a full - and highly configurable - integrated development environment providing many tools useful for developing larger software projects and supporting many different programming languages. We will be ignoring most of those elements for this course.

Running VS Code

To start VS Code:

When you start VS Code for the first time, you'll see a welcome screen offering some customization options.

Go ahead and close the welcome tab.

Setup

VS Code is designed to be highly extensible so that it can support a wide variety of programming uses. For this course, we will use an extension that provides auto-formatting and some other convenient features for Java programming.

To install the Java language support extension:

The top match should be "Language Support for Java(TM) by Red Hat".

If you get a prompt to install additional recommended extensions, just close the prompt without installing.

You should only need to do this step once.

Writing, Compiling, and Running Java Programs

First, open your ~/cs124 folder:

VS Code remembers what folders and files you have open from one launch to the next, so you will likely only need this step the first time you run VS Code, or if you switch to a different folder at some point.

The Explorer tab may open automatically at this point. The Explorer shows you the contents of the open folder so you can open files you want to work with, and is visible towards the left in the picture below. (If you don't see something similar, you can always open the Explorer tab yourself by clicking on the icon in the upper left of the VS Code window.)


Expand/collapse directories in the Explorer tab by clicking on the little arrows to the left of the directory names.

You can open an existing file for viewing and editing by clicking on it in the Explorer tab. A new editor tab appears, labeled with the filename.

To create a new file, go the File menu, and choose New File... Type the desired file name into the box and press Enter. This pops up Create File dialog box. Verify that the filename you want appears in the Name box at the top of the dialog, and in the dialog navigate to the folder that you want the file to go into and click Create File. A new editor tab labeled with the filename will appear, and the new file will be shown in the display in the Explorer tab.


Compiling and running the program is done through the command line. Instead of opening a standalone terminal window like you did to change your password, open one within VS Code:

Open a terminal within VS Code:


The prompt shows the current working directory. In the picture above, the current working directory is ~/mathcs/courses/cs124/s24/cs124. You will likely see something more like ~/cs124.

In order to compile and run your program, the current working directory needs to be the directory where the Java file is. (In this case, ~/cs124/lab1 because HelloWorld.java is in your lab1 folder.)

In the terminal, cd is the command to change the current working directory - cd .. will change to the directory containing the current working directory (i.e. up one level), and cd dirname will change to the directory dirname within the current working directory.

Compile the program using the javac command, followed by the name of the program file including the .java extension:

This creates the file HelloWorld.class, which is what you can actually run.

Run the program with the java command, followed by the name of the program (without the .java or .class extension):

You should see the text "hello world!" get printed.

Make sure your program's output matches what is shown exactly, including the two leading spaces on the first line and the right number of o's and .'s on the third line - if it doesn't, the program doesn't meet the specifications!

Finally, you can auto format your program using the Format Document command:

You can also invoke auto-format with the shortcut ctrl-shift-I.


Finishing Up

Complete the following exercises.

  1. Using Visual Studio Code, create a new file called lab1writeup.txt in your lab1 directory. (Make sure your file is named exactly as directed, lowercase and all.) Put your name at the beginning of the file, and then answer the following questions.

    1. Is it legal to put spaces in filenames in Linux? Is it a good idea? Explain.

    2. For each of the following, give the full path name for the file or directory mentioned. If there is more than one way to write the path (e.g. using shortcuts), give at least one alternative in addition to the full name.

      1. Your lab1 directory.
      2. The lab1writeup.txt file where you are writing these answers.
  2. The Using Linux at HWS web site also contains a lot of information about the Linux environment at HWS. Use the "Emergencies" section to answer the following questions: (add the answers to lab1writeup.txt)

    1. If you accidentally delete a file, is there anything you can do to get that file back? Explain.

    2. Rebooting the Lansing lab computers should be avoided if possible because other users might be logged into the system remotely. If a program crashes or the computer freezes, what are two strategies for fixing problems that don't involve rebooting the computer?

Hand in your lab again so that your lab1writeup.txt file gets handed in.

If Lab is Over and You Aren't Done...

You will not generally be able to finish the lab during the lab period. You can use the computers in Rosenberg 009 (during open lab times in the evenings and on the weekends) and Lansing 310 to complete your lab.

Extra Credit

There's not any graded extra credit for this lab, but if you have additional time:


Handin

Work for this course will be handed in electronically - no need to print anything! Handing in consists of copying your files to a special handin directory that has been set up so that I can access your work for grading.

Be sure to copy the right thing - hand in the folder for the lab, not the files contained in that folder or your entire cs124 directory. To check that you handed things in correctly:

If you do not see this, fix it!