2.1: What are objects?

Objects are a reference type, similar to something like a String class. In fact, a String is an object. However, Objects contain primitive data types as well as reference data types. When you reference an object, you are referencing the point at which it is located.

Objects are defined by a class, which describes what attributes the object has and what things it can do with methods.

  • House metaphor: blueprint is the class, each house is an object

An object is not a type of a class, but it is a specific instance.

2.2: Initializing/Constructing

To initialize an object, the syntax goes like:

Person andrew = new Person("andrew", 16);

This creates a new person object called andrew, with attributes of "andrew" and 16.

2.3: Methods

Methods are code that do something, sometimes taking parameters. THey can change data, print text, etc.

There are also void and non void methods. Void methods don't return anything, but change data or print things. The void keyword is used.

Methods can be static or non static.

Static methods are general to the class, and do not have to be in any specific object. It basically changes something general. To make a method static, the static keyword is added.

2.4 Methods with parameters

A method can be created with parameters as well, taking in input and returning some output. There can be multiple methods with the same name but different parameters, which are all valid.

2.6 String Concatenation

Strings can be appended using + or +=. This is also known as concatenation.

String a = "hello";
String b = "world";

String result = a + " " + b;
System.out.println(result);
hello world

Mixing types in concatenation is also possible, but often parentheses are needed to do calculations if using multiple primitives. Otherwise, it will take each number as a string and just append it.

String message = "12" + 4 + 3; // print out 1243
System.out.println(message);
1243
String message = "12" + (4 + 3); // print out 127 with the parenthesis
System.out.println(message);
127

2.7 String Methods + comparing

compare strings

  • you can use equals(String) or compareTo(String) equals returns a boolean, and just returns true if two strings are equal
String a = "blah";

System.out.println(a.equals("bleh")); // false
System.out.println(a.equals("blah")); // true
false
true

compareTo returns an integer, comparing the string relative to the string within the parentheses. It returns a negative if the original string comes before the one inside, or positive if its afetr.

String a = "blah";

System.out.println(a.compareTo("bleh")); // negative
System.out.println(a.compareTo("blah")); // 0
System.out.println(a.compareTo("bl")); // positive
-4
0
2

comparing numbers The normal operations in if statements still apply here, like:

  • <
  • >
  • <=
  • =

  • !=
  • ==

Using these will return true or false depending on if it works.

2.9 Math class with random

The math class has many useful methods to calculate, like abs (absolute value) or sqrt (square root) The most complex of the methods is random, which returns some number between 0 (inclusive) and 1 (non inclusive)

Math.random();
0.5430955616916946

HOMEWORK

Full solution:

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 */
        this.secret = word;
        
    }
    /** Returns a score for guess, as described in part (a).
    * Precondition: 0 < guess.length() <= secret.length()
    */

    // START OF SOLUTION A
    public int scoreGuess(String guess) {

        // Tracks number of times the substring appears
        int occurrences = 0;

        // secret.length() - guess.length() to avoid index overflow with substring later on
        for (int i = 0; i <= secret.length() - guess.length(); i++) {

            // Checks if the substring is equal to the guess
            if (secret.substring(i, i + guess.length()).contains(guess)) {
                occurrences++;

                // Moves forward by guess.length() - 1 to move forward in the string (and avoid repeats)
                // -1 is included because i++ 
                i += guess.length() - 1;
            }
        }
        return occurrences * guess.length() * guess.length();
    }
    // END OF SOLUTION A

    // START OF SOLUTION B
    public String findBetterGuess(String guess1, String guess2)
    { /* to be implemented in part (b) */ 
        if (scoreGuess(guess1) > scoreGuess(guess2)) {
            return guess1;
        } else if (scoreGuess(guess2) > scoreGuess(guess1)) {
            return guess2;
        } else if (guess1.compareTo(guess2) > 0) {
                return guess1;
        } else {
                return guess2;
            }
        }
    // END OF SOLUTION B

    public static void main(String[] args) {

        WordMatch testA = new WordMatch("mississippi");
        System.out.println(testA.scoreGuess("issippi"));
        System.out.println(testA.scoreGuess("mississippi"));

        WordMatch testB = new WordMatch("concatenation");
        System.out.println(testB.findBetterGuess("ten" , "nation"));
        System.out.println(testB.findBetterGuess("con", "cat"));
    }

}

WordMatch.main(null);

part a

scoreGuess, finds times guess occurs as a substring of secret and multiply that by the square of length of guess

public int scoreGuess(String guess) {

    // Tracks number of times the substring appears
    int occurrences = 0;

    // secret.length() - guess.length() to avoid index overflow with substring later on
    for (int i = 0; i <= secret.length() - guess.length(); i++) {

        // Checks if the substring is equal to the guess
        if (secret.substring(i, i + guess.length()).contains(guess)) {
            occurrences++;

            // Moves forward by guess.length() - 1 to move forward in the string (and avoid repeats)
            // -1 is included because i++ 
            i += guess.length() - 1;
        }
    }
    return occurrences * guess.length() * guess.length();
}

The condition secret.substring(i, i + guess.length()).contains(guess) checks if the substring the length of the guess contains the guess itself. If it does, it adds one to the occurences, and moves the pointer to avoid repeats.

Part B

findBetterGuess, returns better guess of two strings. If they are equal, alphabetically greater guess is returned

public String findBetterGuess(String guess1, String guess2)
    { /* to be implemented in part (b) */ 
        if (scoreGuess(guess1) > scoreGuess(guess2)) {
            return guess1;
        } else if (scoreGuess(guess2) > scoreGuess(guess1)) {
            return guess2;
        } else if (guess1.compareTo(guess2) > 0) {
                return guess1;
        } else {
                return guess2;
            }
    }

The first two if statements are purely for returning the higher score guess. However, if they are equal, then the compareTo metod finds the alphabetically greater guess.

Google Form

6/7