Lab 0: Hello Java

Aaron Bauer

January 3, 2022

Lab 0: Hello Java

Assignment Manifest

Each CS 201 assignment starts with an assignment manifest describing Due dates, the Collaboration policy, starter code or other materials in the lab Handout, how to Submit your work, and a list of topics or other resources that may be helpful to Reference.

(Orange boxes mark key information or warnings.)

Overview

This lab is designed to help you get started programming in Java and to introduce you to the tools we’ll be using to write and run Java programs. First, you will install Java and Visual Studio Code on your computer. Second, you will write a couple of simple Hello, World-style programs and learn how to configure VS Code to use command-line arguments. Finally, you will practice a bit of Java by writing a program to draw a picture.

Setting Up Your Programming Environment1

Coding Pack for Java

The easiest way to get set up for Java programming is to install the Coding Pack for Java, which includes VS Code, the Java Development Kit (JDK), and essential Java extensions. The Coding Pack can be used as a clean installation, or to update or repair an existing development environment. If you run into any issues installing this, the system administrator Mike Tie (and his assistant sys admins) are a great resource for solving tech issues. You can email him at mtie@carleton.edu or stop by his office in Olin 337.

Install the Coding Pack for Java - Windows

Install the Coding Pack for Java - macOS

Creating a source code file

Now that your programming environment is installed, time to take it for a spin. Create a folder for your Java program and open the folder with VS Code. Then in VS Code, create a new file and save it with the name Hello.java. When you open that file, the Java Language Server automatically starts loading, and you should see a loading icon on the right side of the Status Bar (at the bottom of the VS Code window). After it finishes loading, you will see a thumbs-up icon.

Note: If you open a Java file in VS Code without opening its folder, the Java Language Server might not work properly.

Follow along with the video below and write a Java program that prints out "Hello, World!". Note how the video uses some of the handy snippets that VS Code provides to quickly fill in standard Java syntax.

Running and debugging your program

To run you Hello program either press F5 on your keyboard or use the Run > Start Debugging menu item. You can also use the Run|Debug options in the editor that appear above the main method. VS Code will automatically compile your code whenever you go to run it. You should see a terminal pop up at the bottom of the screen and your message appear.

After the code compiles, you can see all your variables and threads in the Run view.

This is a good opportunity to try out some of the nice debugging features VS Code has to offer. Follow along with the video to

Command-Line Arguments

What if instead of "Hello, World!", we want our program to take in a string on the command line and print "Hello, THAT_STRING"? We need to configure VS Code to let us input command-line arguments for our program:

More features

VS Code has a lot of bells and whistles when it comes to Java. We won’t need to use most of them in this course, but here are links to more information if you’re curious:

Draw a Picture

Your last task is to practice Java syntax by writing a program to draw a picture. To do this you will use the StdDraw class, which is part of the algs4 library from Princeton2 that we will use at various points in the course. It can be a single image or an animation. How complicated to make it is entirely up to you—a few shapes will suffice as far as meeting the grading expectations. I encourage you to experiment and be creative! I’ll try and put a gallery of everyone’s drawings.

The handout contains a starter project for this part of the lab. Download the zip file, unzip it into its own folder, and then open that folder in VS Code.

Project Folders in VS Code

Most labs in this course will use external libraries (i.e., code someone else wrote, contained within a .jar file). VS Code will only be able to find libraries included in the lab handouts if the folder containing the lib folder is the one opened in VS Code. So you want the Lab 0 handout to look like this when opened in VS Code:

If you have another folder open instead, such as a folder containing the lab0-handout folder, you’ll get a StdDraw cannot be resolved error message:

This bad state is easy to get into on Windows because by default Windows will unzip the handout into a new folder with the same name as the .zip file (macOS does not do this). On Windows, either open the .zip file and drag the lab0-handout folder to your desktop (or wherever you want your CS 201 files) or right-click on the .zip file, select Extract All…, and make sure to delete the “lab0-handout” portion of the path where you want to extract the files.

The handout contains MyDrawing.java. Use the main method of the MyDrawing class to create your image or animation. You will also find a subdirectory lib containing algs4.jar. A .jar file is one way collections of Java classes get distributed. VS Code will automatically import from .jar files in the lib directory.

There is good documentation and examples for StdDraw here. If you wish, you are welcome to use the flying-shapes demo from Friday’s learning block as a starting point.

What to Turn In

The only file you need to submit is MyDrawing.java. Do not change the name of this file! Upload it directly (not inside a zip file) to the Lab 0 assignment on Moodle

Grading

This assignment is graded out of 10 points as follows:

Requirement Points
MyDrawing.java compiles without errors or warnings 2 points
MyDrawing.java produces a picture consisting of at least a few shapes when run 5 points
Comments with name and description at the top of MyDrawing.java 2 points
Correct submission format (i.e., a file called MyDrawing.java) 1 point

  1. Adapted from https://code.visualstudio.com/docs/java/java-tutorial↩︎

  2. https://algs4.cs.princeton.edu/code/↩︎