Overview · Learning Goals · Files · How to Work · Tasks · Testing · Checkpoint · Before Moving On

Overview

Lab A is a Java programming warm-up designed to help you become comfortable working with the basic Java constructs that we will use throughout CS231.

You will implement a collection of small methods involving conditionals, loops, arrays, characters, parameters, and return values. The individual tasks are intentionally small so that you can concentrate on Java syntax and program structure.

Coming from Python? Many of the programming ideas in this lab should already be familiar. Pay particular attention to how Java handles types, arrays, method declarations, return values, and integer versus floating-point arithmetic.

Learning Goals

By completing this lab, you should become more comfortable with:

Files Provided

Download both files and place them in the same directory.

Download LabA_v2.java Download LabA_v2_Tester.java

You will write your solutions in:

LabA_v2.java

The second file:

LabA_v2_Tester.java

is a test harness that will call your methods and report whether the supplied tests pass.

Do not modify LabA_v2_Tester.java. Your job is to modify LabA_v2.java so that it works correctly with the tester.

How to Work

Open LabA_v2.java in VS Code. The file contains method definitions with TODO: comments describing what you need to implement.

Important:

Work through the tasks in order. Compile and test your program periodically rather than waiting until you have completed everything.

Tasks

Part I — Java Basics

These first tasks provide practice with Java method syntax, boolean expressions, conditionals, and return values.

Task 1 — Multiple of Three

Implement:

public static boolean isMultipleOfThree(int value)

Return true if value is evenly divisible by 3 and false otherwise.

Task 2 — Compare Two Numbers

Implement:

public static int compare(int a, int b)

The method should return:

Part II — Arrays and Numeric Computation

The next tasks introduce Java arrays and provide practice traversing, creating, and returning arrays.

Task 3 — Find the Smallest Element

Implement:

public static int minElement(int[] values)

Traverse the array and return its smallest value. You may assume the array contains at least one element.

Task 4 — Array Sum

Implement:

public static int arraySum(int[] values)

Compute and return the sum of all values in the array.

Task 5 — Return Values in Sorted Order

Implement:

public static int[] sortedPair(int a, int b)

Return a two-element array containing the values in ascending order.

For example:

sortedPair(7, 3) → {3, 7}
Java does not provide a built-in tuple type for returning two values. Think about how an array can be used instead.

Task 6 — Copy Array with Offset

Implement:

public static int[] copyWithOffset(int[] values, int offset)

Create and return a new array containing the values from the input array with offset added to each value.

For example:

values = {1, 2, 3}
offset = 5

result = {6, 7, 8}

The original array should not be modified.

Part III — Conditionals and Array Traversal

These tasks combine conditionals, loops, array traversal, and simple algorithmic reasoning.

Task 7 — Is Digit Character

Implement:

public static boolean isDigitChar(char ch)

Return true if ch represents a digit from '0' through '9', and false otherwise.

Use direct character comparisons with the range '0' through '9'.

Task 8 — Adjacent Equals

Implement:

public static boolean hasAdjacentEquals(int[] values)

Return true if the array contains the same value in two adjacent positions.

For example:

{1, 2, 2, 3} → true
{1, 2, 3, 2} → false

Task 9 — Appears Exactly Once

Implement:

public static boolean appearsOnce(int[] values, int target)

Return true if target appears exactly once in the array, and false otherwise.

For example:

values = {1, 2, 3, 2}

appearsOnce(values, 3) → true
appearsOnce(values, 2) → false

Task 10 — Is the Array Strictly Increasing?

Implement:

public static boolean isStrictlyIncreasing(int[] values)

Return true if each value in the array is greater than the value immediately before it.

For example:

{1, 2, 3} → true
{1, 2, 2} → false
Think about adjacent elements. If you compare an element with the one immediately after it, be careful not to move past the end of the array.

How to Test Your Work

Make sure both Java files are in the same directory. From the terminal, compile them with:

javac LabA_v2.java LabA_v2_Tester.java

If the files compile successfully, run the tester:

java LabA_v1_Tester

The tester will run a collection of tests and display [PASS] or [FAIL] for each test.

For example:

=== CS231 Lab 1-B Test Harness ===
[PASS] isMultipleOfThree(9)
[PASS] isMultipleOfThree(10)
[PASS] compare(5,3)
...
=== End of Tests ===
A failing test is useful information. Identify which method failed, inspect that method, make one change, compile again, and rerun the tester.

Lab Checkpoint

Checkpoint: When you have completed LabA_v2.java, run the provided tester and demonstrate your working code to a lab TA or the instructor.

Your checkpoint demonstrates that you have completed the Java foundations work needed to begin Project A.

Once the checkpoint has been completed, move on to Project A and begin applying the Java skills practiced in this lab.

Before Moving On

Before beginning Project A, make sure that: