CPSC 225 Intermediate Programming Spring 2025

Lab 1: Linux and Eclipse

Due: Thu Jan 30 at the start of lab

Introduction

This lab will introduce you to the computing and programming environment used in many of the CS courses at HWS. You'll learn how to access Linux, both in a computer lab and remotely, and navigate the desktop; how to navigate the file system, copy files, and hand in your work; and how to use Eclipse, a professional-grade tool for Java development. You'll also get your Linux working environment set up for use in this course, and, optionally, set up your own computer as an alternative to accessing Linux remotely when you aren't working in the lab.

Much of what is contained in this handout may already be familiar to you. While you can skip some of the informational parts in that case, be sure to complete all of the steps in the exercises and do all of the reddish-boxed steps in the Eclipse tutorial even if you have used Eclipse before.

About Due Dates

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

For this lab, while not officially due until next Thursday, you should complete it as soon as possible so that you are ready to work on programming tasks.


Exercises

To complete this lab, read through this section and complete the bulleted steps, in order. If you'll be bringing a laptop to class and have it with you, you are strongly encouraged to test connecting to the Linux VDI before the end of lab so that any issues can be worked out before class.

Important! How to read instructions: Don't stop with just the first sentence, as there is often additional information about how to do the step following the initial statement of the step. Look through the whole section containing the steps first to get a big picture view of what you'll be doing and to determine if there's information after the steps themselves that you'll need to carry out the steps, then repeat the process as you go through each step — read more carefully through the whole bullet point or paragraph before taking any action so that you understand how to proceed.

If You Have Time (Optional)

If you finish the lab early, create a new main program within your lab1 project and work on the Collatz sequence problem from the class prep for Friday.



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 and/or the Macintosh operating system. 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. HWS uses a version called Ubuntu. Different distributions of Linux all work the same way underneath, but they may look different or be configured differently.

Accessing Linux

Linux computers are available in Rosenberg 009 and in the Math/CS department lab in Lansing 310.

You can also access a Linux virtual desktop remotely from your own computer. There are two options:

Off-Campus Access

Note that you must be on the HWS campus network (connected to HWS-Private wifi or using an Ethernet cable on campus) in order to access the Linux virtual desktop. To access the HWS network from off-campus, you will need to first download and install the VPN client (Mac, Windows). Once set up, connect to the VPN (Connecting to HWS Network from Off-Campus (VPN)) before connecting to the Linux virtual desktop.

Logging In and Logging Out

Once you get to a Linux login screen, whether you are in the lab or using a virtual desktop, enter your HWS username and password to log in.

When you are done working or will be away from the computer for a while, it is important to log out (even on a virtual desktop). One reason is security — if you don't log out, anyone who comes along will have access to your account and all your files. Another important reason is etiquette — neither the number of physical computers in the lab nor the number of virtual desktops are unlimited, so tying up a computer you aren't using may block someone else out entirely.

If you will only be away from the computer for a short time, such as for a quick trip to the bathroom, you can lock the screen instead of logging out, but the same etiquette guidelines apply — a locked screen prevents others from using the computer, and a computer locked for a long period of time may get rebooted which risks you losing work if your files aren't saved.

How to log out or lock the screen is covered in the next section ("Navigating the Desktop").

Navigating the Desktop

Unlike Windows and MacOS, where the desktop that you interact with is bundled with the rest of the operating system, the desktop in a Linux system is just another component and you'll likely encounter different-looking desktops if you use different Linux distributions.

For a brief introduction to the Ubuntu desktop, see A Beginner's Guide to Ubuntu Linux.

Working With Files and Directories

As with Mac and Windows, files in Linux are organized into a hierarchical collection of directories. ("Directory" is the traditional Linux term while "folder" is more associated with Mac and Windows; there are technical distinctions between the terms but from a user perspective both terms refer to the same concept and can be used interchangeably.) Each directory can contain files and/or subdirectories, which in turn contain files and/or subdirectories, and so forth.

Use the Files application to manage your files and directories — copy, paste, rename, delete, and (for directories) create.

Naming Files and Directories

Unlike Windows, Linux is case-sensitive so cs225 and CS225 are two different things. Be careful to name things exactly as directed (including case).

When choosing names for files and directories, 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 in some contexts, causing problems when I am printing your programs for grading. Please don't use spaces in file and directory names!

Home, Root, and Pathnames

You have a home directory which contains your personal files. (This is similar to "My Documents" in Windows.) To go to your home directory, click the Home shortcut in the Files application.

Another directory is the root directory. This directory is 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, click on "Other Locations" in the Files application and then click on "Computer" (which should be the first thing under "On This Computer").

Root and home directories are well-known, and you can simply say "the root directory" or "my home directory" and people will know what you are talking about. (There's also the Home shortcut in Files which lets you jump straight to your home directory.)

For files and other directories, however, it is necessary to give a pathname which specifies where the thing is located within the system's directory structure. Pathnames can be absolute, which means that the path starts from the root directory, or relative, which starts from some other directory.

The notation for writing down pathnames differs from one operating to another. In Linux:

For example, the absolute path /home/alice/work/todo.txt refers to a file todo.txt which can be found by starting at the root (/), opening the subdirectory home, opening the subdirectory alice within home, and opening the subdirectory work within alice.

Your home directory is /home/username, where username is your actual username, not the word "username". (For example, the user alice's home directory is /home/alice.) This is the directory you are taken to when you click on the Home shortcut in the Files application; the pathname tells you that you can also get there by starting at the root directory (/), opening the home subdirectory, and then opening the directory with your username within home.

~ provides a shortcut for referring to home directories. This is convenient because every user has their own home directory. For example, ~/work/todo.txt refers to a file todo.txt which can be found by starting in your home directory and opening the subdirectory work within your home directory. ~alice/work/todo.txt refers to a file todo.txt which can be found by starting the user alice's home directory and opening the subdirectory work within alice's home directory. Careful attention to detail is required here — ~alice refers to alice's home directory (/home/alice) while ~/alice refers to a subdirectory called alice within your home directory (/home/username/alice).


Eclipse

Eclipse Concepts

Programming is typically done within an integrated development environment (or IDE), which combines the basic tools of software development — an editor and a compiler — with other useful things such as automation of the build process, a debugger, code-generation tools, and support for quickly browsing and navigating the files in a large software project. Programming can certainly be done without an IDE, but a good IDE can help streamline the process and make development faster and easier.

We will be using Eclipse, an open-source IDE originally developed by IBM. Learning to use an IDE will make it easier to develop and manage large programs, and is a useful "real world" job skill. Eclipse, in particular, is nice because it is freely available and supports multiple languages (so you don't have to switch IDEs if you switch programming languages). It is also a real piece of software, used in actual software development environments — it's not a simplified teaching tool.

This lab will introduce some of the fundamental concepts in Eclipse as well as some of its most useful features. There's a lot more Eclipse can do (though not all of it is relevant at this point) — feel free to explore.

In Eclipse, a project contains all of the files associated with a single program while a workspace contains a collection of projects along with preferences and settings (like code formatting conventions) common to those projects. This organization allows you to configure Eclipse in different ways simultaneously without having to repeat configuration steps when settings are common to many different projects.

When you run Eclipse, what you see in the Eclipse window is the workbench. The workbench window contains various editors and views allowing you to see and interact with your program. A particular configuration of editors and views is known as a perspective. We will primarily use the Java perspective in its default configuration, though it is possible to customize any perspective if you wish.

Eclipse Workspace Setup

To create and set up an Eclipse workspace for this course:

Using Eclipse

This section is meant as a tutorial — doing the steps outlined with reddish boxes (and not just reading them) is part of the lab.

Java Perspective

Java development work is done using the Java perspective. Eclipse should open in the Java perspective by default, and should look similar to what is shown below.

Eclipse workbench

The first time you start Eclipse with a new workspace, it may display a welcome message. Close the Welcome tab if there is one. You should then see something like what is shown.

If your Eclipse window doesn't look like what is shown above, it may be in the wrong perspective. To switch to the Java perspective: choose "Window"→"Perspective"→"Open Perspective"→"Java (default)". If you don't see "Java (default)" as an option, instead choose "Other..." and then choose "Java (default)" from that list and click "Open".

If your Eclipse window still doesn't look right, ask for help.

Hello World

There are four basic steps to creating a program using Eclipse: creating a project to contain your program's files, creating a main program, writing some code, and running the program.

Syntax Errors

As you type code, Eclipse tries to be helpful — it pops up warnings and suggestions for code completion, and puts all sorts of little markers and icons in and next to your code indicating syntax errors and other things. To further explore how Eclipse can help you find and fix syntax errors, do the following to your (working) hello world program:

As you make these errors (deliberately), notice that a squiggly red underline marks the location of a potential problem, red X icons in the left margin of the editor tab mark lines with errors, and several red rectangles in the right margin of the editor tab indicate the presence of errors within the file as a whole.

You should notice that red Xs indicating the presence of errors now appear next to the file and the project in the Package Explorer.

Now, let Eclipse help you fix the errors:

Packages

Packages are an important way of organizing the potentially many classes within a program and Eclipse is eager to put new classes into a package. For now, however, using the default package is fine. (That's why you deleted the entry in the "Package" box when you created HelloWorld.)

If HelloWorld.java isn't listed under "(default package)" in the Package Explorer:

Formatting

Another handy feature in Eclipse is auto-formatting — so there is no excuse for handing in poorly-formatted code!

Frequently auto-formatting your files helps with readability and can help reveal bracket-nesting errors.

Importing Files and Using Objects

You will typically make use of objects of many different types in a program, and the class definitions for each of the objects you want to use must be available to both the compiler (javac) and the runtime system (java). By default, the system looks for any classes that aren't part of the standard Java libraries in the same directory as the rest of your program — which, for Eclipse, means they are included in the same project.

Often you will be creating classes from scratch, but Eclipse also allows you to import existing files into a project. To do this:

Importing files like this copies them to your project directory — any changes you make will not affect the original copy.

Finally, modify the main program to make use of the Person class:

Testing Your JavaFX Setup

JavaFX is used for creating GUI programs. Actually programming with JavaFX won't be covered until later in the semester, but code may be provided as part of labs or programming assignments that uses JavaFX so you need to be able to run JavaFX programs. This takes a bit of setup because JavaFX is not distributed with the rest of the Java JDK.

Earlier in the lab you configured Eclipse to be able to find the JavaFX libraries on the system. Now you should test your configuration to make sure it works:

If there are compiler errors after you import the file or runtime errors when you try to run the program, there is something wrong with your JavaFX setup. Double-check the last two steps in the Eclipse setup section above (the two that have to do with JavaFX) and if there are still errors, ask for assistance. If everything is well, running the program should pop up a window with several buttons that you can click.

Getting Help

Eclipse has an extensive online help system available through the "Help" menu. "Help"→"Welcome" is a good place to start for an introduction to Eclipse; "Help Contents" and "Search" are useful if there is something in particular you are looking for help with. "Dynamic Help" can also be handy — it brings up help topics associated with what you are currently doing.

Exploring (Optional)

Eclipse is a powerful tool, and does much more than has been described so far or that will be covered in this course. Feel free to explore and to experiment, either now or during the semester. A few things of particular interest:


Handing In Your Work

To hand in your work:

See Working With Files and Directories to review what a pathname starting with / means, and to navigate the filesystem, copy files, and find /classes using the Files application. If you are working on your own computer (not using the Linux VDI), you'll need to use FileZilla to copy the files into the Linux filesystem. See Using Your Own Computer for more information.


Using Your Own Computer

It is recommended that you use the Linux VDI to access Linux remotely as that simplifies having to copy files across filesystems and means that your files are included in the regular system backups.

You will need to download and install three things: Eclipse, FileZilla, and JavaFX. (JavaFX is the trickiest part to set up. It will only be needed for some assignments, so an option is to skip JavaFX and just use Linux in those cases.)

Eclipse

Eclipse 2024-06 is recommended if you don't already have Eclipse installed on your computer, though any recent version should be fine. Choose the "Eclipse IDE for Java Developers" package (not the installer) from the download link, picking the right version for your computer.

Once you have Eclipse installed, create a workspace directory and repeat the Eclipse workspace setup steps. (Skip the Java 17 syntax rules and the JavaFX setup steps for now.)

FileZilla

You will also need to be able to copy files between your computer and the Linux filesystem in order to get provided code and to hand in your work. FileZilla is a free program you can use to do this. Download the FileZilla client (not server)!.

Once you start FileZilla, do the following to connect to the Math/CS department server:

If the connection is successful, you'll see two directory listings. On the left side, labeled "Local site", you'll see your computer. On the right side, labeled "Remote site", you'll see the Linux filesystem. To copy files, navigate to what you want to copy on one side and where you want to copy it on the other, then drag the file(s)/directories(s) from one side to the other.

As with the Linux VDI, you must be on the HWS campus network in order to connect with FileZilla. See the section about off-campus access in the Accessing Linux section above.

JavaFX

JavaFX is not part of the standard Java distribution. You can find download links here — download the right SDK (not jmods) for your computer and Java version. Eclipse 2024-06 includes a Java 21 JRE.

The downloaded SDK will be a compressed archive file. Double-clicking on the file will likely either extract the contents or open a program you can use to extract the contents. You can put the resulting directory (which will have a name like javafx-sdk-21.0.6, depending on the version) anywhere, but you'll need to know where you put it.

Do the JavaFX configuration steps from the Eclipse workspace setup, adjusted for your JRE version and where you put the JavaFX SDK directory. Come to office hours if you need help with this — it can be tricky to get right.