January 3, 2022
MyDrawing.java
to Lab 0Each 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.)
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.
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
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.
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
"Hello, World!"
string and use the Show Fixes button (the light bulb icon) to automatically refactor your print statement to use a local variable for this string. Refactoring is when you restructure your code in some way to improve the internal design without changing the external behavior.main
method. The program will halt at the line with the breakpoint and switch the left panel to the debugging view. Among other things, this displays the current values of your local variables, and even lets you change their values! Hit F5
or click the blue arrow icon on the bar at the top of your screen to resume program execution.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:
Go to Run > Open Configurations. This will create a folder called .vscode
in your project directory. Inside that folder will be a file called launch.json
that defines the different ways we can run the current project. It will look something like this:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Launch Hello",
"request": "launch",
"mainClass": "Hello",
"projectName": "hello_1896eb18"
}
]
}
Don’t worry about understanding every detail here, most of the time you won’t need to mess with these. All we want to do for now is to modify the second configuration, the one for our Hello
class. Add the line "args": "${command:SpecifyProgramArgs}",
to the top of that configuration so it looks like this (your "projectName"
may be different):
{
"args": "${command:SpecifyProgramArgs}",
"type": "java",
"name": "Launch Hello",
"request": "launch",
"mainClass": "Hello",
"projectName": "hello_1896eb18"
}
Save the launch.json
file, and modify your print statement in the main
method of the Hello
class to use the first elements of the args
array like so:
System.out.println("Hello, " + args[0]);
Run your program. You should see a box pop up at the top of the window prompting you to enter program arguments. Enter a word or a name and hit enter. You should see your input printed as part of the program out in the terminal.
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:
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.
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.
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
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 |