Skip to content

Lab 1: Getting Started

This assignment will introduce you to VS Code, reading error messages from the Java compiler and Checkstyle, and submitting to Gradescope. You will also gain experience printing output on the screen, using variables and methods, and performing arithmetic.

Note

Before continuing with this lab, make sure that you have installed Java and VS Code according to the directions here: Installing Java, VS Code.

Starter code is provided for this lab! Download and open (extract) lab01.zip. Move the lab01 folder into your CS159/src/labs folder.

Learning Objectives

After completing this lab, you should be able to:

  • Use VS Code to edit, format, compile, and test a Java program.
  • Identify and correct errors from Checkstyle.
  • Submit to Gradescope and interpret feedback.

Use this assignment to learn the objectives above. You are allowed (indeed encouraged) to get help on labs. But you must get help in such a way that enables you to do this type of work on your own later. Copying code from someone else, including the use of generative AI, often interferes with learning at this stage. Make sure you understand what you turn in, as you may be asked to explain your code at a later date.

Part 1: Correcting compiler errors

Open your CS159 folder in VS Code, and navigate to the src/labs/lab01 folder on the left. Double click on Percent.java to open the file in the editor. Notice that the editor window now shows the opened file.

Important

Make sure you have the entire CS159 folder open in VS Code. Otherwise, none of the settings or libraries will be active. To open the CS159 folder, go to File → Open Folder.

Skim the code noticing that there are several places where part of the code is underlined. If you are using dark mode (the default color theme), these underlines will be in red. Much like in a document editor, underlined text contains a problem.

It is best to fix problems one at a time from the beginning of the program. Often fixing the first problem will solve additional reported problems later in the code. There are three compiler errors in the given code. Follow the notes below to identify and fix them.

Error 1

The first underline should be under the word Demonstration in line 16. What does the problem seem to be? Hover over the underlined text and you will see the message:

Syntax error on token "Demonstration"

Strings must have double quotes at both the beginning and end. Make sure double quotes surround the entire text Percentage Demonstration.

Error 2

The second error is a bit harder to see, as the underline is only under the closing parenthesis in the second statement of main(). Notice how the opening parenthesis for the println() method is also a different color. Hover over the underlined text. This time the message is:

Syntax error, insert ")" to complete Expression

Fix the error by inserting a ) in the appropriate location.

Error 3

The third compiler error is in the first statement in the percent() method. The variable name percentage is underlined. Hover over the underlined text. This time the error is:

Syntax error, insert ";" to complete BlockStatements

Fix the error by inserting a ; in the appropriate location.

Part 2: Correcting Checkstyle errors

Have you been thinking parts of the program are hard to read? Using correct style is an important aspect of creating professional, maintainable code. Similar to the way that we corrected compiler errors above, we will correct style errors. Now the underlines in dark mode are in yellow.

Error 4

The entire first statement in main() is underlined. Hover over the underlined text. The message is:

'method def' child has incorrect indentation level 9, expected level should be 8.

Delete the extra space in the indentation.

Error 5

In the second line in main(), the number 100 is underlined. Hover over the underlined text. The message is:

',' is not followed by whitespace.'

Insert a space after the comma in the method call.

Error 6

Note that the long line in percent() is underlined and hard to read. In fact, the line consists of multiple statements. Hover over the underlined text. The message has several parts. The first part reads:

Line is longer than 80 characters (found 106).

Several additional parts indicate there should be spaces around the operators such as = and +. Rather than correct these lines one at a time, use the formatter to correct ALL of them.

To run the formatter, right-click the code and choose Format Document. Alternatively, you can use the shortcut keys for your operating system.

Note

Compiler errors often have to be resolved before the Checkstyle errors are visible.

Part 3: Correcting logical / programming errors

Now that the compiler and Checkstyle errors are gone, run the program and see what happens. There are multiple ways you can run the program:

  1. Press the F5 key, which is the shortcut for Start Debugging.
  2. Click the small triangle () in the top right corner of the editor.
  3. Go to the Run menu at the top, and click Start Debugging.

When a program is run, a terminal window will open. First the command that was used to run the program is displayed followed by the output. At this point the output is likely:

Percentage Demonstration
25 is 0.0percent of 100

Why is the percent 0.0 in this case? That is not correct. Notice that the parameters to percent are both int. In Java, when integers are divided, the fractional part of the number (the part after the decimal point) is truncated. Any integer division where the dividend is smaller than the divisor will result in a zero value.

Running unit tests

To see this issue more clearly, we can open the file PercentTest.java and run it. Test files are run by clicking one of the green play () buttons in the left margin. Clicking the play button on the public class line runs all test methods in the class. Clicking the play button on a public void line runs that test method only.

Running a test class / method
Play buttons

If a test passes, the green play button remains green. Test methods that fail are indicated on the left hand side with (a small red circle with an X).

Expected vs Actual Results
Test Failure

To correct the integer division bug, change the two parameter types in the method percent to be double instead of int.

There is one more mistake in the output. A space is missing; can you find it? To compare the outputs of each assertEquals(), click on the red error message. Continue running the JUnit tests and get them to pass by making changes to the code.

Part 4: Add a second method call to main

Now that you have corrected the logical errors, add another method call to the main() method. Specifically, add a call to percent() with 6 as the dividend and 80 as the divisor, and print out the result. Insert the call after the similar call using 25 and 100.

Submission

Submit your edited Percent.java to Gradescope by the end of the class period. Submit what you have before you leave. You may continue to work on the lab after class and can resubmit with no penalty.

Grading Criteria

Your code will first be graded by Gradescope and then by the professor. The grade you receive from Gradescope is the maximum grade that you can receive on the assignment.

After the due date, the professor may manually review your code. At that time, points may be deducted for inelegant code, inappropriate variable names, bad comments, etc.

Your code must compile with the official tests for you to receive any points. For full credit, your code must also pass a Checkstyle audit.

Gradescope will provide you with hints but might not completely identify the defects in your submission. You are expected to test your own code before submitting.

There is no limit on the number of submissions and no penalty for excessive submissions. Points will be allocated as follows:

Criterion Points Details
Compile 3 pts Success Required
CompileOfficialTests 0 pts Success Required
Style 3 pts Partial Credit Possible
OfficialTests 4 pts Partial Credit Possible