Unit 5 Boolean & If Statements
Avinh Huynh
public class WordMatch {
/** The secret string. */
private String secret;
/** Constructs a WordMatch object with the given secret string of lowercase letters. */
public WordMatch(String word) {
/* implementation not shown */
}
/** Returns a score for guess, as described in part (a).
* Precondition: 0 < guess.length() <= secret.length()
*/
public int scoreGuess(String guess) {
int count = 0;
for (int i = 0; i < word.length()-guess.length(); i++) {
if (word.substring(i, i+guess.length()).equals(guess)) {
count++;
}
}
return count * guess.length() * guess.length();
}
/** Returns the better of two guesses, as determined by scoreGuess and the rules for a
* tie-breaker that are described in part (b).
* Precondition: guess1 and guess2 contain all lowercase letters.
* guess1 is not the same as guess2.
*/
public String findBetterGuess(String guess1, String guess2) {
/* to be implemented in part (b) */
}
}
public class MemberInfo {
/** Constructs a MemberInfo object for the club member with name name,
* graduation year gradYear, and standing hasGoodStanding.
*/
public MemberInfo(String name, int gradYear, boolean hasGoodStanding)
{ /* implementation not shown */ }
/** Returns the graduation year of the club member. */
public int getGradYear()
{ /* implementation not shown */ }
/** Returns true if the member is in good standing and false otherwise. */
public boolean inGoodStanding()
{ /* implementation not shown */ }
// There may be instance variables, constructors, and methods that are not shown.
}
public class ClubMembers {
private ArrayList<MemberInfo> memberList;
/** Adds new club members to memberList, as described in part (a).
* Precondition: names is a non-empty array.
*/
public void addMembers(String[] names, int gradYear) {
for (String name : names) {
memberList.add(MemberInfo(name. gradYear, true));
}
}
/** Removes members who have graduated and returns a list of members who have graduated
* and are in good standing, as described in part (b).
*/
public ArrayList<MemberInfo> removeMembers(int year)
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods that are not shown.
}