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_v1.java
The second file:
LabA_v1_Tester.java
is a test harness that will call your methods and report whether the supplied tests pass.
LabA_v1.java so that it works
correctly with the tester.
Open LabA_v1.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 isEven(int value)
Return true if value is even and
false otherwise.
Implement:
public static int signOfNumber(int value)
The method should return:
1 if the number is positive0 if the number is zero-1 if the number is negativeThe next tasks introduce Java arrays and provide practice traversing, creating, and returning arrays.
Implement:
public static int maxElement(int[] values)
Traverse the array and return its largest value.
Implement:
public static double arrayAverage(int[] values)
Compute and return the average of the values in the array.
double.
Implement:
public static int[] swap(int a, int b)
Return the two values in reversed order.
For example:
swap(3, 7) → {7, 3}
Implement:
public static int[] mergeArrays(int[] a, int[] b)
Create and return a new array containing all
elements of a followed by all elements of b.
For example:
a = {1, 2, 3}
b = {4, 5}
result = {1, 2, 3, 4, 5}
Do not modify either of the original arrays.
These tasks combine conditionals, loops, array traversal, and simple algorithmic reasoning.
Implement:
public static int checkCharCase(char ch)
The method should return:
1 if the character is uppercase0 if the character is lowercase-1 if the character is not a letter
Use character comparisons involving the ranges
'A' through 'Z' and
'a' through 'z'.
Implement:
public static boolean has22(int[] values)
Return true if the array contains a 2
immediately followed by another 2.
For example:
{1, 2, 2} → true
{1, 2, 1, 2} → false
{2, 2, 2} → true
Implement:
public static int countOccurrences(int[] values, int target)
Count and return the number of times target
occurs in the array.
For example:
countOccurrences({1, 2, 2, 3}, 2) → 2
countOccurrences({5, 5, 5}, 5) → 3
countOccurrences({1, 2, 3}, 4) → 0
Implement:
public static boolean isNonDecreasing(int[] values)
Return true if every value in the array is greater
than or equal to the value immediately before it.
For example:
{1, 2, 2, 3} → true
{1, 3, 2} → false
{5} → true
Make sure both Java files are in the same directory. From the terminal, compile them with:
javac LabA_v1.java LabA_v1_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-A Test Harness ===
[PASS] isEven(4)
[PASS] isEven(7)
[PASS] signOfNumber(10)
...
=== End of Tests ===
LabA_v1.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_v1.java and
LabA_v1_Tester.java compile without errors.