Unit 1 HW

  • Grade Calculator
import java.util.Scanner;
 
    Scanner input;
    boolean separateCategory;
    float currentGrade;
    float desiredGrade; 
    float percentOfFinal; 
    
    input = new Scanner(System.in);
    System.out.print("Do you want separate categories? ");
    separateCategory = input.nextBoolean();
    System.out.println(separateCategory);

    if(separateCategory == true) {
        System.out.print("Enter current grade? ");
        currentGrade = input.nextInt();
        System.out.println(currentGrade);

        System.out.print("Enter desired grade? ");
        desiredGrade = input.nextInt();
        System.out.println(desiredGrade);

        System.out.print("Enter percent of grade that is final? ");
        percentOfFinal = input.nextFloat();
        System.out.println(percentOfFinal);

        input.close();
        
        float gradeNeeded = (desiredGrade - currentGrade * (1-percentOfFinal))/percentOfFinal;
        System.out.println(gradeNeeded);
    }
Do you want separate categories? true
Enter current grade? 91.0
Enter desired grade? 95.0
Enter percent of grade that is final? 20.0
91.2

Unit 2 HW

  • 2021 FRQ

Problem 1

a) 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.

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 */
}
/** Returns a score for guess, as described in part (a).
* Precondition: 0 < guess.length() <= secret.length()
*/
public int scoreGuess(String guess)
{
    int result = 0;

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

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

b) 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. WordMatch game = new WordMatch("concatenation");

// public int scoreGuess(String guess)
// { /* to be implemented in part (a) */ }
/** Returns the better of two guesses, as determined by scoreGuess and the rules for a
* tie-breaker that are described in part (b).
* Precondition: guess1 and guess2 contain all lowercase letters.
* guess1 is not the same as guess2.
*/
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; 
}

Scoring Guidelines

Part A

  • compare guess to substring of secret
  • Uses of substring secret with correct length for comparison with guess
  • Loops through all necessary substrings of secret
  • Counts number of identified occurences of guess within secret
  • Calculate and returns correct final score (algorithm)

  • Notes: We believe we would earn a 5/5, full credit, for this part because we completed all of the elements of the point guidlines. At first, we forgot the return statement, but we were able to problem solve and fix this. It was also hard to get the correct syntax down while we were working with new elements in Java

Part B

  • Calls scoreGuess to get scores for guess1 and guess2
  • Compares the scores
  • Determines which guess1 and guess2 is alphabetically greater
  • Returns the identified guess1 or guess2 (algorithm)

  • Notes: After some problem solving we believe we earned full credit. We found this part to be a little easier since it was simple if statements that we had learned previously.

Unit 4 HW

  • Caesar Cipher Program Try to write a caesar cipher program that shifts each letter in a message 3 letters forward. Use any of the methods you learned today. Use it to decode the 3 messages we've given you!
public class CaesarCipher {

    public static void main(String[] args) {

        String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
        String message1 = "Kfzb gly!";
        String message2 = "zlab zlab zlab";
        String message3 = "prmbozxifcoxdfifpqfzbumfxifalzflrp";

        int shiftBy = 3 ; // showing how much to shift by

        CaesarCipher caesarCipher = new CaesarCipher(); // instantiating CaesarCipher class

        System.out.println("Decrypted Message1 : " + caesarCipher.decryptMessage(message1, shiftBy));
        System.out.println("Decrypted Message2 : " + caesarCipher.decryptMessage(message2, shiftBy));
        System.out.println("Decrypted Message3 : " + caesarCipher.decryptMessage(message3, shiftBy));
    }

    // method for decrypting given message by the shift int value 
    String decryptMessage(String input, int shiftBy){ 

        int asciia =(int) 'a'; // assigning ascii values for a 

        String decryptMessage = "";
        for(int i=0 ; i < input.length(); i++){
                char c = input.charAt(i) ; 
                char newChar = c ;
                // does decryption only for letters (not the exclamation mark, not spaces, etc.)
                if( Character.isLetter(c) ){
                    int intValue = (int) c ; // finds the ascii value for given letter
                    int newValue = (intValue - asciia + shiftBy ) % 26 ; // assiging deciphered ascii value by subtracting the ascii value and adding the shift value
                    newValue = newValue + asciia ;
                    newChar = (char) newValue; // converting ascii value to new character
                }
                decryptMessage += newChar ;
        }
        return decryptMessage;
    }

}
CaesarCipher.main(null);
Decrypted Message1 : Nice job!
Decrypted Message2 : code code code
Decrypted Message3 : supercalifragilisticexpialidocious

Unit 5 HW

  • 2019 FRQ Q2

his question involves the implementation of a fitness tracking system that is represented by the StepTracker class. A StepTracker object is created with a parameter that defines the minimum number of steps that must be taken for a day to be considered active.

The StepTracker class provides a constructor and the following methods.

  • addDailySteps, which accumulates information about steps, in readings taken once per day
  • activeDays, which returns the number of active days averageSteps, which returns the average number of steps per day, calculated by dividing the total number of steps taken by the number of days tracked
  • Write the complete StepTracker class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.
public class StepTracker {

    // accessing and showing our private instance variables
     private int totalSteps;
     private int minimumSteps;
     private int daysActive;
     private int days;
     
     // constructor with the parameter 
     public StepTracker(int least){
         minimumSteps = least;
         totalSteps = 0; // values to initialize variables
         daysActive = 0;
         days = 0;
     }
 
     //added the dailySteps method as the "AddDailySteps"
     public void AddDailySteps(int steps){
         totalSteps += steps; //shows active days and the incremental counting
         days++;
         if (steps >= minSteps){
             daysActive++; // updates the other instance variables
         }
     }
 
     //the activeDays method
     public int getdaysActive(){ // declared and implemented into program
         return days;
     }
 
     public double avgSteps(){
         if (days == 0){
             return 0.0;
         } 
         else{
 
             //returns the calculated double of the average number of steps walked
             return (double) totalSteps / days; 
         }
     }
 
 }

Unit 6