Overview · Learning Goals · Files · How to Work · Tasks · Testing · Checkpoint · Before Moving On
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.
By completing this lab, you should become more comfortable with:
Download both files and place them in the same directory.
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.
LabA_v2.java so that it works
correctly with the tester.
Open LabA_v2.java in VS Code. The file contains method
definitions with TODO: comments describing what you need
to implement.
Work through the tasks in order. Compile and test your program periodically rather than waiting until you have completed everything.
These first tasks provide practice with Java method syntax, boolean expressions, conditionals, and return values.
Implement:
public static boolean isMultipleOfThree(int value)
Return true if value is evenly divisible
by 3 and false otherwise.
Implement:
public static int compare(int a, int b)
The method should return:
1 if a > b0 if a == b-1 if a < bThe next tasks introduce Java arrays and provide practice traversing, creating, and returning arrays.
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.
Implement:
public static int arraySum(int[] values)
Compute and return the sum of all values in the array.
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}
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.
These tasks combine conditionals, loops, array traversal, and simple algorithmic reasoning.
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'.
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
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
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
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 ===
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 beginning Project A, make sure that:
LabA_v2.java and
LabA_v2_Tester.java compile without errors.