Learnings

This unit had some very important learnings for me. The first thing that I learned was that if two strings are created in the format of String string1 = new String("string"), the .equals() method should be used to compare them.

The second thing that I learned was that there is a way to compare if the attributes of two objects are the same using the .equals() method. However, this requires some manual overriding of the .equals() method because by default, .equals() works the same as ==, which compares the object memory address. You can still compare if two objects are the same object with ==.

Notes

Objects are instances of a class. There are three steps to create an object: declaration, instantiation, and initialization. Declaration is specified through a type followed by the variable name. Instantiation instantiates the object with the new keyword. Initialization occurs the name of the constructor method is specified after the new keyword.

Creating objects

The syntax to create objects is: Class-name object-name = new constructor-name

Methods

Methods are defined with a return type (int, bool, etc. or void (no return type)) and the method name.

Static vs non static

A static method can only access other static methods, while a nonstatic method can access both static and nonstatic methods.

Math class

Important methods: Math.abs, Math.random()

Comparing

Numbers

Numbers can be compared using ==. Numbers can always be compared with == because they are primitive types and not objects.

int a = 5; 
int b = 5; 

if (a == b) {
    System.out.println("a = b"); 
}
a = b

Strings

There are two ways to compare strings: == and .equals().

You can use == to compare strings when they are declared like this: String name = "string";

An example is shown below:

String a = "foo"; 
String b = "foo"; 

if (a == b) {
    System.out.println("The two strings are equal"); 
}
The two strings are equal

However, if your two strings are objects, you can not use == to compare them (see below):

String a = new String("foo"); 
String b = new String("foo"); 

if (a == b) {
    System.out.println("The two strings are equal"); 
} else {
    System.out.println("The two strings are not equal"); 
}
The two strings are not equal

Instead, you need to use .equals() to compare the two string objects.

String a = new String("foo"); 
String b = new String("foo"); 

if (a.equals(b)) {
    System.out.println("The two strings are equal"); 
} else {
    System.out.println("The two strings are not equal"); 
}
The two strings are equal

Note: a and b are different objects, but they contain the same string of foo, which is what .equals() checks for.

Objects

There is a way to compare if two objects have the same attributes using the .equals() method. However, you need to first override the method to do so.

Below is an example:

To override the .equals() method, you need to create the method that takes in an object. In this example, the object is the format of Object obj. In the .equals() method, Object obj refers to test2. Next, obj needs to be typecasted to the class, in this case ObjectTest, for comparison to work. The return statement checks to see if the attributes of the two objects are equal. Note: this refers to test1

public class ObjectTest {
    String attribute1;
    String attribute2; 
    
    // IMPORTANT: forgot to specify String in parameters
    // see https://www.daniweb.com/programming/software-development/threads/346224/identifier-expected-in-constructor-definition
    // youtube reference for comparing objects: https://www.youtube.com/watch?v=X2AjBFZfFCY
    public ObjectTest(String attribute1, String attribute2) {
        this.attribute1 = attribute1; 
        this.attribute2 = attribute2; 
    }

    @Override
    public boolean equals(Object obj) {
        ObjectTest test = (ObjectTest)obj; 
        return this.attribute1.equals(test.attribute1) && this.attribute2.equals(test.attribute2);
    }

    public static void main(String[] args) {
        ObjectTest test1 = new ObjectTest("a", "b"); 
        ObjectTest test2 = new ObjectTest("a", "b"); 

        
        if (test1.equals(test2)) {
            System.out.println("Same attributes"); 
        }
    }
}
ObjectTest.main(null)
Same attributes

1a

Initial attempt:

Comments: I started off alright; however, I was soon stuck at the for loop part. I knew that I had to use a for loop to perform iteration that will compare the substring to secret, but I did not know how to compare to certain characters within a string. After looking at part of the answers, I found that the program utilized the substring() method, which I did not know what it did. After some googling, I applied my knowledge to the code below in my second attempt.

public int scoreGuess(String guess) {
    int substring; 
    int length = guess.length(); 
    int score;

    for ()


    score = substring * length * length;
}

Second attempt

Comments: This attempt mostly matched the answer key. One thing that I did wrong, however, was that in my comparison in the if statement, I used == instead of .equals. I have seen .equals() before, but I have never used it, so once again, I had to do some googling. I learned that if you want to compare two objects, you would need to use the .equals() method. In this case, secret is an object, as indicated by this line of code: private String secret;

public int scoreGuess(String guess) {
    int length = guess.length(); 
    int count; 
    int score;

    for (int i = 0; i < (secret.length() - (length - 1)); i++) {
        if (guess == secret.substring(i, length)) {
            count++; 
            length++; 
        }
    }


    score = count * length * length;

    return score; 
}

Correction:

public int scoreGuess(String guess) {
    int length = guess.length(); 
    int count; 
    int score;

    for (int i = 0; i < (secret.length() - (length - 1)); i++) {
        if (guess.equals(secret.substring(i, length))) {
            count++; 
            length++; 
        }
    }


    score = count * length * length;

    return score; 
}


1b

Initial attempt:

Comments: I made this a lot more complicated than it needed to be 😳

Mistakes: I forgot that objects do not need to be created within the findBetterGuess method since findBetterGuess and scoreGuess are in the same class.

Looking at the answer, I learned about the method called compareTo(), which allowed for comparison between strings. compareTo() returns an integer, in which if the integer is smaller than 0, the first string is alphabetically smaller than the second string. If the integer is greater than 0, the first string is alphabetically greater than the second string. If the integer is equal to 0 --- you guessed it ---, both strings are the same.

public String findBetterGuess(String guess1, String guess2) {
    WordMatch string1 = new WordMatch(guess1);
    WordMatch string2 = new WordMatch(guess2);
    
    int score1 = string1.scoreGuess(guess1);
    int score2 = string2.scoreGuess(guess2); 

    if (score1 > score2) {
        return guess1; 
    } else if (score1 < score2) {
        return guess2; 
    } else {
        int smallGuess = Math.min(guess1.length(), guess2.length());
        for (int i = 0; i < smallGuess; i++) {
            String individualCh1 = guess1.substring(i, i);
            String individualCh2 = guess2.substring(i, i);

            int chNum1 = individualCh1.indexOf(individualCh1);
            int chNum2 = individualCh2.indexOf(individualCh2);

            if (chNum1 > chNum2) {
                return guess1;
                break;
            }

            if (chNum2 > chNum1) {
                return guess2; 
                break; 
            }

        }
    }

}

Correction

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;
    } else {
        return guess2; 
    }

}