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_v1.java Download LabA_v1_Tester.java

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.

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

How to Work

Open LabA_v1.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 — Even or Odd

Implement:

public static boolean isEven(int value)

Return true if value is even and false otherwise.

Task 2 — Positive, Negative, or Zero

Implement:

public static int signOfNumber(int value)

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 Largest Element

Implement:

public static int maxElement(int[] values)

Traverse the array and return its largest value.

Task 4 — Array Average

Implement:

public static double arrayAverage(int[] values)

Compute and return the average of the values in the array.

Think about Java arithmetic: What happens when one integer is divided by another integer? Your method returns a double.

Task 5 — Swap Two Values

Implement:

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

Return the two values in reversed order.

For example:

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

Task 6 — Merge Two Arrays

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.

Part III — Conditionals and Array Traversal

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

Task 7 — Check Character Case

Implement:

public static int checkCharCase(char ch)

The method should return:

Use character comparisons involving the ranges 'A' through 'Z' and 'a' through 'z'.

Task 8 — Adjacent Twos

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

Task 9 — Count Occurrences

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

Task 10 — Is the Array Non-Decreasing?

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
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_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 ===
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_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 Moving On

Before beginning Project A, make sure that: