Overview · Learning Goals · Files · Rules · Tasks · Testing · Checklist

Overview

In Project A you will build a small library of static methods involving numbers, strings, and arrays.

Each method solves a separate programming problem. Together, the methods provide practice with Java syntax, loops, conditionals, arrays, strings, arithmetic operations, and algorithmic problem solving.

From Lab A to Project A: The lab introduced many of the Java tools you will use here. This project asks you to apply those tools to problems that require more planning and more careful algorithm design.
Specification-driven project. Each method will be tested independently. Read the requirements and restrictions for every method carefully.

Learning Goals

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

Files Provided

Download both files and place them in the same directory.

Download Project_A.java Download Project_A_Tester.java

You will implement your solutions in:

Project_A.java

The file:

Project_A_Tester.java

provides a collection of tests that you can run while developing your project.

The provided tester is not exhaustive. Passing every supplied test does not guarantee that your implementation is completely correct. Course staff may test additional inputs and edge cases.

Implementation Rules

Read these rules carefully.

Project Tasks

Task 1 — Reverse an Integer

Implement:

public static int reverseInt(int n)

Reverse the digits of a non-negative integer.

For example:

reverseInt(123) → 321
reverseInt(5)   → 5
reverseInt(0)   → 0
Restriction: Solve this problem numerically using the modulus (%) and division (/) operators. Do not convert the integer to a String.

Remember that:

int digit = n % 10;
n = n / 10;

allows you to remove and process one digit at a time.

Task 2 — Reverse a String

Implement:

public static String reverseString(String input)

Return a new String containing the characters of input in reverse order.

For example:

reverseString("abc") → "cba"
Remember that charAt(index) allows you to access an individual character in a String.

Task 3 — Palindrome Check

Implement:

public static boolean isPalindrome(String input)

Determine whether the String reads the same forwards and backwards.

For example:

isPalindrome("racecar") → true
isPalindrome("hello")   → false
Think before you loop: Characters can be compared symmetrically from the beginning and end of the String. How many comparisons are actually necessary?

Task 4 — Count Vowels

Implement:

public static int countVowels(String input)

Count and return the number of vowels in the String.

For example:

countVowels("hello") → 2
countVowels("AEiou") → 5

Examine the String one character at a time using charAt(i).

Task 5 — Reverse an Array In Place

Implement:

public static int[] reverseArray(int[] values)

Reverse the elements of the original array in place.

For example:

{1, 2, 3, 4} → {4, 3, 2, 1}
Important: Do not create another array. Rearrange the elements inside the original array and return that same array reference.
Consider using one index at the beginning of the array and another at the end. Swap values while those indices move toward each other.

Task 6 — Swap Two Values

Implement:

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

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

For example:

swap(3, 7) → {7, 3}

Task 7 — Concatenate Two Arrays

Implement:

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

Return a new array containing all elements of a followed by all elements of b.

For example:

a = {1, 2}
b = {3, 4}

result = {1, 2, 3, 4}
Java arrays cannot change size after they are created. Determine the required size of the result before creating the array.

Task 8 — Check String Case

Implement:

public static int checkCase(String input)

Determine whether the letters in a String are all uppercase, all lowercase, or mixed.

The method should return:

For example:

checkCase("HELLO") → 1
checkCase("hello") → 0
checkCase("HeLLo") → -1
You can determine character case by comparing characters with the ranges 'A' through 'Z' and 'a' through 'z'.

Task 9 — Adjacent Value Check

Implement:

public static boolean hasAdjacentValue(int[] nums, int target)

Determine whether target occurs in two adjacent positions in the array.

For example:

hasAdjacentValue({1, 2, 2, 3}, 2) → true
hasAdjacentValue({1, 2, 2, 3}, 3) → false
If you compare nums[i] with nums[i + 1], be careful that i + 1 does not move beyond the end of the array.

Task 10 — Duplicate Detection

Implement:

public static boolean hasDuplicates(int[] values)

Return true if any value appears more than once in the array and false otherwise.

For example:

hasDuplicates({1, 2, 3}) → false
hasDuplicates({1, 2, 1}) → true
Think about the comparisons. When comparing one array element with the others, avoid repeating comparisons that have already been performed.

How to Test Your Project

Place Project_A.java and Project_A_Tester.java in the same directory.

Compile both files:

javac Project_A.java Project_A_Tester.java

Then run the tester:

java Project_A_Tester

The tester will report which supplied tests pass and which fail, followed by a summary of the number of tests passed.

Passing the provided tester is not the finish line. The tester checks several important examples, but your methods should work correctly for all inputs allowed by the specification. Additional tests may be used when your project is evaluated.
Debug one method at a time. Because each project method is independent, use the tester output to identify a failing method, examine that method carefully, make a change, compile again, and rerun the tests.

Before You Submit