Kyle FRQ Key Learnings - yajatyadav/intellijs GitHub Wiki

Unit 2

This FRQ involved creating a Light Sequence object and then performing various changes such as inserting segments, removing segments, and replacing the value of the string.

  public String insertSegment(String segment, int ind){
    sequence = sequence.substring(0, ind) + segment + sequence.substring(ind, sequence.length()); 
    return sequence;
  }

  public String remover(String oldseq, String segment){
    this.oldSeq = oldseq;
    this.segment = segment;
    int start = oldSeq.indexOf(segment);
    String newSeq = oldSeq.substring(0,start) + oldSeq.substring (start+segment.length());
    return newSeq;
  }

  public void changeSequence(String seq){ 
    sequence = seq;
  }

Though not the main focus, I also got practice with creating classes like with the constructor below, taking in a user input from earlier.

      Scanner sc8 = new Scanner(System.in);
      System.out.println("Enter a light sequence (0s/1s)");
      String user_sequence = sc8.nextLine();
      LightSequence ls = new LightSequence(user_sequence);

  public LightSequence(String seq){ 
    sequence = seq;
  }

Finally, I was able to execute code with a menu to take in user input -

Unit 3

The first question in this FRQ involved taking in information about an event including if the user would rsvp and what their food selection would be

boolean rsvp;
  int selection;
  String option1;
  String option2;

  public event(boolean rsvp, int selection){
    this.rsvp = rsvp;
    this.selection = selection;
  }


  public void attending(){
    if(rsvp == true){
      System.out.println("attending");
    }else{
      System.out.println("not attending");
    }
  }

  public void selection(){
    if(selection == 1){
      System.out.println("beef");
    }else if(selection == 2){
      System.out.println("chicken");
    }else if(selection == 3){
      System.out.println("pasta");
    }else{
      System.out.println("fish");
    }
  }

The main learning from this was the use of if, else if, and else statements as seen in the selection method.

After taking in all of the information by user input, it would return a string putting it all together, in which I used concatentation.

  public void message(){
  if (rsvp == true){
    option1 = "Thanks for attending. You will be served ";
    if (selection == 1){
      option1 = option1 + "beef.";
    }else if(selection == 2){
      option1 = option1 + "chicken.";
    }else if (selection == 3){
      option1 = option1 + "pasta.";
    }else{option1 = option1 + "fish.";
  }
  }else{
    option1 = "Sorry you can't make it.";
  }

The second question asked us to draw a square using preset methods;

public static void drawLine(int x1, int y1, int x2, int y2){
  }

  public static void drawSquare(int x, int y, int len){
    if(x + len > 10 && y -len < 0){
      len = Math.min(10-x, y);
    }else if (x + len > 10){
      len = 10-x; 
    }else if(y - len < 0){
      len = y;
    }

In it, I used if/else statements as well as Math.min to set restrictions for the dimensions of the square, so that it would fit within the given area.

I then called the functions to draw the square

  drawLine(x, y, x + len, y);
  drawLine(x, y, x, y - len);
  drawLine(x, y - len, x + len, y - len);
  drawLine(x + len, y - len, x + len, y);

and finally returned a string, formatted to include certain variables

  System.out.println(String.format("Side Length = %d, Square Area = %d", len, area));

Unit 4

The first question in unit 4 asked us to create functions to take in a string and find the longest continuous streak of the same character.

To achieve this, I used a loop to go through the string, checking if each character was the same as the previous one, and then keeping track of the most in a row.

for (int i = 0; i < str.length(); i++){

        // Clear streak when reaching a new character
        currentChar = str.charAt(i);
        // Makes currentChar case insensitive; comment this line to make sensitive
        currentChar = Character.toLowerCase(currentChar);

        if (currentChar != previousChar){
            currentStreak = "";
        }

        currentStreak += str.charAt(i);

        // Set largest streak if it's longer than the previous one
        if (currentStreak.length() > largestStreak.length()){
            largestStreak = currentStreak;
        }

        // Set new previousChar
        previousChar = str.charAt(i);

I also added string methods so that it would be case-insensitive - the line could be removed if I wanted it to be case sensitive

 previousChar = Character.toLowerCase(previousChar);
        }

The second question of unit 4 was about a coin game, in which 2 players would bet coins and then gain or lose them based on preset rules.

I used an overarching while loop to keep the game going until one player won

while (round <= maxRounds){
      if(player1coins < 3 || player2coins < 3){
        break;
      }

      int player1spent = getPlayer1Move();
      player1coins -= player1spent;

      int player2spent = getPlayer2Move(round);
      player2coins -= player2spent;

      if(Math.abs(player1spent - player2spent) == 2){
        player1coins = player1coins + 2;
      }else{
        player2coins++;
      }

      round++;
    }

Including method calls to get the move from each person and a math function to determine who gained coins. The round would then increase after each turn.

The player moves were determined in different ways - for player 1 I used Math.random and for player 2, there were specific conditions given by the question

  public int getPlayer1Move(){
    return (int)(Math.random() * 3) + 1;
  }

  public int getPlayer2Move(int round){
    int spent = 0;

    if(round%3 == 0){
      spent = 3;
    }
    if(round%3 != 0 && round%2 == 0){
      spent = 2;
    }
    if(round%3 != 0 && round%2 != 0){
      spent = 1;
    }

Finally, a win condition would be evaluated and we would return the winner of the game.

  if(player1coins == player2coins){
    System.out.println("tie game");
  }else if(player1coins > player2coins){
    System.out.println("player 1 wins");
  }else{
    System.out.println("player 2 wins");
  }

Unit 5

This FRQ was fairly simple, taking in user input for a host name and address, for which I set up a basic class

  private String hostName;
  private String address;
  
  public Invitation(String n, String a){
    hostName = n;
    address = a;
  }

  public String getName(){
    return hostName;
  }

  public void updateAddress(String s){
    address = s;
  }

including an overload constructor for a different set of arguments

  public Invitation(String a){
    hostName = "Host";
    address = a;
  }

and then returned a string based on the information given

  public String invite(String person){
    return "Dear " + person + ", please attend my event at " + address + ". See you then, " + hostName + ".";
  }

The second question was more complicated, asking us to create a password generator, randomly creating a password for someone to use.

I started by taking in various user inputs such as the length and an (optional) custom prefix

      Scanner sc5 = new Scanner(System.in);
      System.out.println("Enter number of digits");
      int digit_response = Integer.parseInt(sc5.nextLine());
      System.out.println("Custom prefix? y/n");
      String prefix_response = sc5.nextLine();
      if(prefix_response.equals("y")){
        System.out.println("Enter custom prefix:");
        String user_prefix = sc5.nextLine();

and then creating a class with that information

public static class passwordGenerator{

  private int suffix_length;
  private String prefix = "A.";
  private static int counter;

  public passwordGenerator(int s, String p){
    prefix = p + ".";
    suffix_length = s;
  }

  public passwordGenerator(int s){
    suffix_length = s;
  }

For the creation of the password, I randomly chose a digit with Math.random for each digit that the user wanted past their prefix

  public String pwGen(){
    for (int i = 1; i<=suffix_length; i++){
      prefix += Integer.toString((int) (Math.random()*10));
    }
    counter+=1;
    return prefix;
  }

and then finally returned the password concatenated into a string

 System.out.println("Generated password: " + pwd.pwGen() + " , total passwords generated: " + pwd.pwCount());

Unit 6

The first question in unit 6 was about searching a list of words for a certain substring and then counting the amount that included it.

For this, I used a for loop to go through the list

    for(String s: words){
      if (s.substring(Math.abs(s.length()-3),s.length()).equals("ing")){
        System.out.println(s);

and then checked the last 3 characters in each entry by substring, seeing if they were "ing" using .equals

The second question was focused on evaluating employee wages based on the amount of items they sold.

I started by taking in user input for the base wage and wage per item

      System.out.println("Enter base wage");
      double fixed_wage  = Integer.parseInt(sc3.nextLine());

      System.out.println("Enter wage per item");
      double per_item = Integer.parseInt(sc3.nextLine());

and then computed the threshold for a bonus 10%

for(int i = 0; i< itemsSold.length; i++){
            for(int x = i + 1; x < itemsSold.length; x++){
                if (itemsSold[x] < itemsSold[i]){
                    temp = itemsSold[i];
                    itemsSold[i] = itemsSold[x];
                    itemsSold[x] = temp;
                }
            }
        }

        return ((int)(totalSales - itemsSold[0] - itemsSold[itemsSold.length-1]) / ((itemsSold.length)-2)) + 0.5;

Knowing that, I was able to find the final wage for every employee -

        double threshold = computeBonusThreshold();
        double bonus = 1.1;

        for (int n = 0; n < wages.length; n++){
            wages[n] = fixedWage + (perItem * itemsSold[n]);
            wages[n] = (int)wages[n];
            if(itemsSold[n] - threshold > 0){
                wages[n] = wages[n] * bonus;

and then return it to the user

      pr.computeWages(fixed_wage, per_item);
      System.out.println("Employee wages:");
      pr.printwages();

Unit 7

This unit focused on using arraylists and arrays, including traversing them to create a set of usernames.

I started by creating an arraylist to store the names

private ArrayList<String> possibleNames = new ArrayList<>();

I then traversed it when changing the list of names

public void setAvailableUserNames(String[] usedNames){
  for(int s = 0; s < possibleNames.size(); s++){

And with an enhanced for loop to check if a name was used

public boolean isUsed(String name, String[] arr){
  // Enhanced for loop
  for (String s : arr){

Unit 8

This FRQ used 2d arrays in creating a farm plot, and we traversed it to access information stored in it. Nested for loops were very useful for this as well.

I began by creating the plot.java file, with a constructor and getters to be used for the experimental plot.

public Plot(String crop, int yield) {
        this.cropType = crop;
        this.cropYield = yield;
    }

    public String getCropType() {
        return cropType;
    }

    public int getCropYield() {
        return cropYield;
    }

I then created a method to traverse the arrays to find the highest yield plot

public Plot getHighestYield(String c){
        int m = farmPlots[0].length;
        int max_yield = 0;
        Plot p = null;
        for (Plot[] farmPlot : farmPlots){
            for (int j = 0; j < m; j++) {
                if (farmPlot[j].getCropType().equals(c) && farmPlot[j].getCropYield() > max_yield) {
                    max_yield = farmPlot[j].getCropYield();
                    p = farmPlot[j];
                }
            }
        }
        return p;
    }

and to check if crops were the same

    public boolean sameCrop(int col){
        int n = farmPlots.length;
        for (int i = 1; i < n; i++){
            if (farmPlots[i][col] != farmPlots[i][0]){
                return false;
            }
        }
        return true;
    }

Unit 9

This unit was primarily about inheritance involving overrides, references, and constructors.

⚠️ **GitHub.com Fallback** ⚠️