Homework 4: Scrabble: Two Basic Classes¶
This assignment focuses on classes and objects. You will also write and submit your own JUnit tests for grading.
Important
Starter code is not provided for this homework.
You will need to create a hw4
folder in your CS159/src/hws
folder.
All classes must be defined in the hws.hw4
package.
Please review the Grading Criteria at the end of this page before you start programming.
Submission
You will submit four files: Hand.java, HandTest.java, Letter.java, LetterTest.java
HandTest.java is provided to you. To receive full credit, your LetterTest.java must pass and cover every line in Letter.java. Javadoc comments are not required for test methods.
Learning Objectives¶
After completing this homework, you should be able to:
- Create classes with attributes and methods.
- Instantiate objects from classes.
- Utilize constructor methods to initialize object properties.
- Implement methods to manipulate object data.
- Write JUnit tests to validate class functionalities.
This assignment must be completed individually. Your work must comply with the JMU Honor Code. Authorized help is limited to general discussion on Piazza, the lab assistants assigned to CS 159, and the instructor. Copying code from someone else, including the use of generative AI, is prohibited and will be grounds for a reduced or failing grade in the course.
Introduction¶
Scrabble is a game where players get a set, or "hand", of wooden pieces, each one with a letter and point value on it. On their turn the player forms a word using some of the letters from their hand and places them on the board. The board has places for the wooden pieces, marked with special symbols, some of which will have a “multiplier” effect on the letter point value that is played in that location. In the real Scrabble, the board is a large square and letters must be played in such a way that each word connects to the others that have been played similar to a crossword puzzle.
The Short Cut Scrabble aims to create a simplified version of the traditional Scrabble board game. In this assignment, we will implement two essential classes, Letter.java and Hand.java, based on the provided UML diagrams. The game involves a single horizontal strip as the board, and players score points by strategically placing words on this strip. We will implement more classes related to Scrabble game in homework 5 and 6.
UML Diagram¶
classDiagram
class Hand{
+MAX_SIZE: int = 8$
-hand: Letter[]
-size:int
+Hand(size: int)
+Hand()
+getSize() int
+getLetter(index: int) Letter
+insert(letter: Letter, index: int) boolean
+remove(index: int) Letter
+indexOf(letter: char) int
+toString() String
}
class Letter{
-letter: char
-points: int
+Letter(letter: char, points: int)
+setLetter(letter: char)
+setPoints(points: int)
+getLetter() char
+getPoints() int
+equals(other: Object) boolean
+toString() String
}
Hand -- Letter
Create Letter.java¶
Letter.java The Letter class represents an individual letter tile in the game. Implement the class and methods according to the UML and specifications.
Letter Constructor and Accessors The Letter constructor and accessors should be implemented to initialize the character and points with the constructor parameters as shown.You can assume that the arguments passed to the constructor parameters will be correct. In other words, no parameter checks are required.
Mutator methods The setLetter and the setPoints methods should be implemented to update instance variables letter and points.
Accessor methods The getLetter and the getPoints methods should be used to return the values of instance variables letter and points.
equals The equals method should return true if both the character and the point value associated with this letter are equal to the character and point value of the other letter.
toString The toString method should return a String describing the letter consisting of the character followed by colon, then followed by the points. For example the String returned by a Letter with letter assigned ’t’ and points assigned 3 will be: “t:3”
Create Hand.java¶
Implement the class and methods according to the UML and specifications. The Hand class represents the player's hand, which holds a collection of Letter tiles.
Hand The no-argument constructor should create a hand of MAX_SIZE.
Hand(int size) This constructor should create a hand of size. If size is less than zero, a hand of size zero should be created. If size is greater than MAX_SIZE, a hand of MAX_SIZE should be created.
insert If index is within range and there is not another letter in the hand at index, the letter should be inserted into the hand at index and return true. If those conditions do not hold, false should be returned.
For example:
// assume the MAX_SIZE is 8 in the Hand class
Hand hand = new Hand();
System.out.println(hand.insert(new Letter(‘A’, 1), 0) // true
System.out.println(hand.insert(new Letter(‘B’, 2), 0) // false
System.out.println(hand.insert(new Letter(‘F’, 1), -1) // false
toString This method should return a String consisting of the following for each letter in the hand: the Letter toString followed by a comma. If there is no letter at index, i.e. the location is null, then insert a dash ‘-’ where the letters toString would be printed.
For example:
// assume the MAX_SIZE is 8 in the Hand class
Hand hand = new Hand(3);
hand.insert(new Letter(‘a’, 1), 1);
System.out.println(hand.insert(new Letter(‘b’, 2), 0);
System.out.println(hand) // output is “b:2,a:1,-”
remove If index is within range, the letter at index should be removed from the hand (the location set to null) and the letter that was at index returned. If there is no letter at index, return null.
indexOf This method should search through the hand and return the index of the first occurrence of the letter. If the letter is not in the hand, return -1.
getLetter(int index) This method should return the value at the given index in the hand.
getSize This method should return the size the hand was initialized to.
Recommended Process¶
Create the Letter class in your HW 4 project:
- Write up all of the methods (with appropriate signatures)
- Initially, each method should return some artificial value
- Add javadoc comments to the Letter class
- Ensure your file has no checkstyle errors
Create the Hand class in your HW 4 project:
- Repeat the above steps for Hand.java
Run the unit tests for the project
- Expect most or all tests to fail at this point
Implement the Letter class:
- Add the fields listed in the UML diagram
- Add the constructor listed in the UML diagram
- Implement the methods listed in the UML diagram
- Write accessors (getters) first
- Write the equals method
- Write the toString method
Implement the Hand class:
- Add the fields listed in the UML diagram
- Add the constructors listed in the UML diagram
- Implement the methods listed in the UML diagram
- Write accessors (getters) first
- Write the toString method
- Write the insert method
- Write the remove method
- Write the indexOf method
Ensure that all provided tests pass:
- Run the unit tests for the project
- Expect all tests to pass
Grading Criteria¶
Your code will first be graded by Gradescope and then by the professor. The grade you receive from Gradescope is the maximum grade that you can receive on the assignment.
After the due date, the professor may manually review your code. At that time, points may be deducted for inelegant code, inappropriate variable names, bad comments, etc.
Your code must compile with the official tests and pass a Checkstyle audit for you to receive any points. For full credit, your code must pass all JUnit tests that you submit, and your JUnit tests must cover every line in your code.
Gradescope will provide you with hints but might not completely identify the defects in your submission. You are expected to test your own code before submitting.
There is no limit on the number of submissions and no penalty for excessive submissions. Points will be allocated as follows:
Criterion | Points | Details |
---|---|---|
Compile | 0 pts | Success Required |
CompileOfficialTests | 0 pts | Success Required |
Style | 0 pts | Success Required |
SelfTests | 5 pts | Partial Credit Possible |
Coverage | 15 pts | Partial Credit Possible |
OfficialTests | 80 pts | Partial Credit Possible |