Quiz

I learned that Static Initialization Blocks are mainly used to initialize static fields of a class.

Methods of an object class include equals(), hashCode(), and notify().

FRQ

1a

public int scoreGuess(String guess)
{
    int count = 0;
    for (int i = 0; i <= secret.length() - guess.length(); i++)
    {
        if (secret.substring(i, i + guess.length()).equals(guess))
        {
            count++;
        }
    }
    return count * guess.length();
}

1b

public String findBetterGuess(String guess1, String guess2)
{
    if (scoreGuess(guess1) > scoreGuess(guess2))
    {
        return guess1;
    }
    if (scoreGuess(guess2) > scoreGuess(guess1))
    {
        return guess2;
    }
    if (guess1.compareTo(guess2) > 0)
    {
        return guess1;
    }
    return guess2;
}

Key Learning on Concatenation

In Java, Strings can be concatenated with mixed types. Examples shown below.

public class Strings {
    public static void main(String[] args) {
        String str1 = "Hello World ";
        int int1 = 1;
        String str2 = ". Introducing... World ";
        double double1 = 2.0;
        // We now concatenate using the "+" operator for strings.
        // Because Strings are immutable, this creates a new String object.
        String strConcat = str1 + int1 + str2 + double1;

        System.out.println(strConcat);
    }
}
Strings.main(null);
Hello World 1. Introducing... World 2.0