Homework 1

due: Thursday February 20 at 11:59pm

Goals:

In this first homework assignment, you will use some of the string methods we saw in class and practice some more with the syntax of Java. There are 2 files to modify, which can be downloaded from here. Extract the hw1 folder and then move this folder to your cs201 folder. Then open a new VS Code window (File -> New Window) and once this new window is open, open the extracted hw1 folder using File -> Open Folder.

When you submit your assignment to Gradescope (by clicking on Homework 1 here), please just upload the 2 files (Palindrome.java, Chessboard.java). Do not upload a folder containing these files - Gradescope will expect the files to be uploaded directly (you can drag and drop all files into the Gradescope submission box).

Part 1: Palindrome Word

In the file Palindrome.java, please complete the method isPalindromeWord which will return true if a single word is a palindrome (and false otherwise). A palindrome is a word that can be read the same forwards or backwards. For example, the word racecar is a palindrome.

Test your code with various strings by passing them to the isPalindromeWord method from your public static void main in the Palindrome.java file. Your code will be tested on Gradescope using the following words:

A few misspellings of the words above will also be used to verify your code returns false when a word is not a palindrome.

Part 2: Palindrome Phrase

A palindrome phrase has a similar definition to a single-word palindrome. In this case, we might have punctuation, such as ., ,, !, ', -, and there may be multiple words. The punctuation and spaces between words does not affect whether the phrase is a palindrome. Please complete the isPalindromePhrase method in the same Palindrome.java file as in Part 1.

As a preprocessing step, we recommend removing any spaces and the punctuation characters mentioned earlier. Note that we can remove substrings from a String using the replace method. We covered the replace(oldChar, newChar) method in class, but note that we can actually replace entire substrings with other substrings. In this case, we want to replace a single character (represented as a String) with an empty String. For example,

String message = "hello";
message = message.replace("l", ""); // replace any occurrence of "l" with an empty string

The message variable will now be "heo".

Also, be careful with capitalization: it might be a good idea to use either toLowerCase() or toUpperCase() on the input String before checking if it is a palindrome.

Test your isPalindromePhrase method by calling it from the main method.

Your code will be tested on Gradescope using the following inputs:

Your method should return true for these inputs. Similar to Part 1, a few misspellings will be used to test when a phrase is not a palindrome.

Part 3: Chessboard Square Color

Chessboard squares are labelled horizontally with a letter from a to h and vertically with integers from 1 to 8. A single square is labelled using the letter first, and then the number. For example, the bottom-left square in the chessboard below is square a1 and the top-right square is h8.

(image source)

Note that characters are associated with numbers in the ASCII Table. For example, the character a has the integer code 97, and the character 1 has the integer code 49.

Also note that we can cast a character to an int using the following syntax:

char c = 'a';
int i = (int)c;

The variable i will now have the value 97.

In the chessboard above, the squares are either red or white, which can be determined by the square labels. In the Chessboard.java file, please complete the method isRed which will return true if the input String label corresponds to a red square and false otherwise. For example, isRed("a1") should return true, whereas isRed("e4") should return false.

Test your isRed method by calling it with different inputs from the main method.

Your submission will be tested on Gradescope on all possible squares for a chessboard.