Methods

  • A method is a block of code which only runs when it is called
  • Parameters - data passed through the method
  • Methods used to perform certain actions - also known as functions.
  • In the FRQ below it focuses on ensuring that you know when to call methods and how to call them properly to ensure that it is functional

Control Structures

  • Programming blocks that can change the path we take through those instructions.
  • Conditional branches
    • if/else/else if
    • switch
  • Loops
    • for
    • while
    • do while
  • In the FRQ below it focuses on control structures in part a which requires a for loop

FRQ

public class WordMatch {

    private String secret; 

    public WordMatch (String secret) { 
        this.secret = secret;
    }

    public void setSecret(String secret) { 
        this.secret = secret;
    }

    public int scoreGuess(String guess) { 

      

        int count = 0; 

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

        return count * guess.length() * guess.length(); 
    }

    public String findBetterGuess (String guess1, String guess2) { 
        
        // this is using the method of scoreGuess above and is inputing guess1 and guess2 into scoreGuess to check which has the higher 
        // this is methods and control structures because the frq is testing that you know when to use and how to call methods to help advance your code and make it function properly

        if (scoreGuess(guess1) > scoreGuess(guess2)) { // if the score of guess1 is greater than score of guess2 
            return guess1; // then guess 1 is the better guess
        }
        if (scoreGuess(guess1) < scoreGuess(guess2)) { // if the score of guess1 is less than score of guess2 
            return guess2; // then guess 2 is the better guess
        }
        if (guess2.compareTo(guess1) > 0) { // compare guess1 and guess2 and the alphabetically greater one is returned
            return guess2;
        }
        return guess1;
    }

    public static void main(String args[]) {  

        Scanner sc = new Scanner(System.in);

        // System.out.println("Enter a guess: ");
        String guess = sc.nextLine();

        String secret = "mississippi";
        WordMatch wordMatch = new WordMatch(secret);

        int score = wordMatch.scoreGuess(guess);
        System.out.println("Score for " + guess + " is " + score);

        // System.out.println("Enter another guess: " +);
        String guess1 = sc.nextLine();

        // System.out.println("Enter another guess: " +);
        String guess2 = sc.nextLine();

        String compare = wordMatch.findBetterGuess(guess1, guess2);
        System.out.println("The better guess is between " + guess1 + " and " + guess2 + " is: " + compare);

        sc.close();
    }
}

WordMatch.main(null)
Score for i is 4
The better guess is between iss and issipp is: issipp