Homework 1

Homework 1

Homework Reflection SurveyGradescopeStarter Code
Due: Thursday September 18, 2025 5:00PM
Goals:
  • Practice with strings
  • Practice with conditionals and for loops
Submit these files:Palindrome.java,Chessboard.java

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 using the Starter Code button at the top of the assignment. 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 the Gradescope button above), 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).

Note

If you’re having trouble getting started on this assignment, see the companion worksheet from Lab 1.

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:

  • aha
  • noon
  • kayak
  • madam
  • rotator
  • racecar

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:

  • A Toyota
  • If I had a hi-fi,
  • UFO tofu
  • Never odd or even.
  • A man, a plan, a canal - Panama!

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.

Testing your implementation

In addition to the correctness of your code, part of your grade will be determined by how comprehensive your test cases are. This is to encourage you to debug your code locally and to think about edge cases that are important to test!

In each of your .java files, you should write a public static void main(String[] args) that tests your code within that class. This should be fairly simple: you’ll want to call your method on some input and then print out what the output is. I recommend printing both the expected value and what your method returns so that you can easily determine if your implementation is correct.

I have defined a set of requirements that your test cases must meet; these aren’t the most comprehensive requirements I could possibly think of, so I encourage you to think of other special cases that you might want to test!

Palindrome.main

  • isPalindromeWord tested on a palindrome
  • isPalindromeWord tested on a non-palindrome
  • isPalindromePhrase tested on a phrase palindrome that is not a word palindrome
  • isPalindromePhrase tested on a non-palindrome

Chessboard.main

  • isRed tested on a red square in an odd row
  • isRed tested on a white square in an odd row
  • isRed tested on a white square in an even row
  • isRed tested on a red square in an even row

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

Homework Reflection Survey

When you finish, make sure to complete the homework reflection survey (using the button at the top of the page) for participation credit! Note: you won’t get reminders on every assignment 😊.