CSCI 136 - Spring 2026

Data Structures

Home | Lectures | Labs | Handouts | CS@Williams

Lab 1: Java Basics

This lab is about getting practice with the basics of Java: control flow, arrays, loops, and methods. This is an in-person lab, so you should turn in what you have when the lab time is over.

Obtaining the Starter Code

Open your terminal using Control-` (this is a backtick, not an apostrophe: the key is above your Tab key). Type git pull and then hit enter.

Once you have run the git pull command, use VSCode to open the Lab01 folder in your repo. (Remember to use Open Folder, and not Open File.) You should see a single file, called Lab01.java.

Task 1: Print Array

In class, we saw how to use a while loop to print an array of integers. This same loop is in your starter code; have a look at the method printArrayPrimes() (you'll be modifying this method later). In this task, fill in the method printArray() with a for loop to print an array of integers. Once Task 1 is complete, your code should print the following (the second line is from the printArrayPrimes method which was already filled in):

-3 1 2 3 4 5
-3 1 2 3 4 5

Task 2: Print Array Backwards

In this task, fill in the method printArrayBackwards() with a loop to print the contents of an array of integers in reverse order. You can use any method you want so long as the output is correct. Once Task 2 is complete, your code should print the following:

-3 1 2 3 4 5
5 4 3 2 1 -3
-3 1 2 3 4 5

Task 3: Writing your Own Method: Test for Primality

Write a method called isPrime() to test if a number is prime or not. Your method should take an integer as a parameter (I'll call it num), and should return a boolean value. The method declaration is not in the code; it is up to you to write it.

Your method should first test if num is less than 2; if so it should return false. Otherwise, it should loop through every possible number between 2 and num-1, and check if it divides num evenly using the modulo operator. If any of these numbers does divide num evenly, the method should return false. If none divide it evenly, the method should return true.

Write a simple test in the main() function to make sure that isPrime() is working as intended.

Task 4: Print Primes in Array

Modify the starter code in printArrayPrimes() to only print the prime numbers in your array. Each loop iteration, call your isPrime() method to test if the number is prime; you should only print the number if it is prime. Once task 4 is complete, your code should print the following:

-3 1 2 3 4 5
5 4 3 2 1 -3
2 3 5

Task 5: Test and Submit to Gradescope

Log into gradescope.com using your Williams email address. You should have received an email with an invite link last week; clicking on this link should be sufficient. (Or, you can go to gradescope.com directly and log in).

Once you're logged in, you should see a box labelled "CS136"; click on it. Then click on "Lab 1: Intro to Java". Upload your Lab01.java file.

When you upload your file, the autograder will run. This may take a little bit; be patient. The autograder will run a few tests and say if you passed the tests or not. If you do not pass a test, it will give feedback on what went wrong. Feel free to ask for help if something is unclear.

Once the tests are passed, your code is working!

Commit Using Git

It's time to add, commit and push your file. Enter each of the following commands into your terminal one at a time. (The pdf instructions from Lab00 have more extensive instructions for how to do this.)

git add Lab01.java
git commit -m "finished with lab 1"
git push

After you commit, log into evolene.cs.williams.edu. Click on your repo, then Lab01, then Lab01.java, and make sure that you can see the modifications you made. If you can see the changes, you're all set! If you finish early, please talk to one of the instructors to make sure everything is in the right place before you leave.