No presentation

  • personal quiz 7/7 on using java objects
  • 5-7.5% of the test

Declaration − A variable declaration with a variable name with an object type.

Instantiation − The 'new' keyword is used to create the object.

Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.

MATH CLASS - abs creates an absolute value around the number, and random returns an int between 0 and 1

  • part of java package, uses static methods that belong to the class and not object
  • math random returns random int or double in the given range
import java.lang;
public class Math{
    public static void main(String[] args){
        System.out.println(Math.abs(-20));
        System.out.println(Math.abs(20)); //math absolute makes it all positive

        System.out.println(Math.random()); //math random returns random in range
    }
}
Math.main(null);

Concatentation

  • adding or joining data types together
  • String concatenation is a type where strings can added to each other to form full sentences
  • primitive concatenation

Standard Methods

  • the toString() method used to get a String object representing a data type or element converted to a String
  • equals() is used to compare 2 strings and returns if the content of the strings are equal
  • hashCode() method computes the hash values of given input objects

Wrapper Class

  • Each primitive has a corresponding wrapper class: boolean, byte, short, char, int, long, float, double
  • used for methods that support objects like conversion
  • converts from other data types to object

1a

Write the WordMatch method scoreGuess. To determine the score to be returned, scoreGuess finds the number of times that guess occurs as a substring of secret and then multiplies that number by the square of the length of guess. Occurrences of guess may overlap within secret. Assume that the length of guess is less than or equal to the length of secret and that guess is not an empty string. The following examples show declarations of a WordMatch object. The tables show the outcomes of some possible calls to the scoreGuess method. WordMatch game = new WordMatch("mississippi");

public class WordMatch
{

public int scoreGuess(String guess) //answer to 1a
{   
    int count = 0; //starts off the count of substring occurrences at 0
    
    for (int i = 0; i <= secret.length(); i++) { //assuming that length of guess is less than or equal to secret.length
        if (secret.substring(i, guess.length().equals(guess)); //checks if guess is within substring of secret
        count++;
    } //then increases the count
}

return count * guess.length() * guess.length(); //numbers of times guess is found within substring and multiplies it by square of guess 
}

1b

Write the WordMatch method findBetterGuess, which returns the better guess of its two String parameters, guess1 and guess2. If the scoreGuess method returns different values for guess1 and guess2, then the guess with the higher score is returned. If the scoreGuess method returns the same value for guess1 and guess2, then the alphabetically greater guess is returned. The following example shows a declaration of a WordMatch object and the outcomes of some possible calls to the scoreGuess and findBetterGuess methods.

public class WordMatch
{
public String findBetterGuess(String guess1, String guess2)
{
    if (scoreGuess(guess1)>scoreGuess(guess2)) //if guess for guess1 is greater then guess1 is returned
    {
        return guess1;
    }
    if (scoreGuess(guess2)>scoreGuess(guess1)) //otherwise guess2 is returned
    {
        return guess2; 
    }
}

I couldn't figure out how to get it to return the greater alphanumeric guess if the values for both were the same, so I searched it up and found out that you need to use an if(guess1.comparedTo(guess2)>0), then you return guess1, otherwise you return guess 2.