Yajat FRQ Key Learnings - yajatyadav/intellijs GitHub Wiki
Unit 2
public class LightSequence{
private String sequence;
private String oldSeq;
private String segment;
public LightSequence(String seq){
sequence = seq;
}
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 distance(double a, double b){
System.out.println(Math.sqrt(a*a + b*b));
}
public void changeSequence(String seq){
sequence = seq;
}
public void display(){
System.out.println(sequence);
}
}
- I learned how to manipulate Strings in this FRQ, such as finding a substring using an index, in order to both insert and remove specific segments at a defined location
- I also wrote a mutator method
- Used the Math class's methods to perform computations
Unit 3
public Unit_3_1(boolean rsvp, int selection){
this.rsvp = rsvp;
this.selection = selection;
}
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");
}
}
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.";
}
}
- In these FRQs, I practiced implementing if/else if/else statements
- I also practiced concatenating different data types to form a message
- I also used the this keyword in a constructor to set the instance variables
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;
}
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);
int area = len * len;
System.out.println(String.format("Side length = %d, area = %d", len, area));
}
- In the second FRQ, I practiced calling a method inside a method to accomplish a particular task
- I also used some string formatting to print a custom message
- I also had compound boolean expressions to deal with the different restrictions (the square had to fit inside the grid)
Unit 4
public static void longestStreak(String str){
String previous = str.substring(0,1);
String longest = "";
String current = "";
for (int x = 0; x < str.length(); x++){
if (!str.substring(x,x+1).equals(previous)){
current = "";
}
current += str.substring(x,x+1);
if (current.length() > longest.length() ){
longest = current;
}
previous = str.substring(x,x+1);
}
System.out.println(longest.substring(0,1) + " " + longest.length());
}
- In this FRQ, I learned how to use a for loop with specific conditions to write an algorithm
- I used different if statements as well as variables to track the longest streak of letters while traversing down the String
public int getPlayer2Move(int round){
int result = 0;
if (round%3 == 0){result = 3;}
if (round%3 != 0 && round%2 == 0) {result = 2;}
if (round%3 != 0 && round%2 != 0) {result = 1;}
return result;
}
while (round <= maxRounds){
if(player1coins < 3 || player2coins <3){break;}
int player1spent = getPlayer1Move();
player1coins -= player1spent;
int player2spent = getPlayer2Move(round);
player2coins -= player2spent;
if(player1spent == player2spent){player2coins++;}
else if (Math.abs(player1spent - player2spent) == 1){player2coins++;}
else{player1coins = player1coins+2;}
round++;
}
- In this FRQ, I learned how to use compound boolean expressions to ensure multiple criteria were met
- I also practiced writing a while loop that goes until maxrounds are reached or if any player's coins fall below 3
- I also used conditionals in the loop to update the player's coin values correctly
Unit 5
public class Invitation{
private String hostName;
private String address;
public Invitation(String n, String a){
hostName = n;
address = a;
}
// part a
public String getName(){
return hostName;
}
// part b
public void updateaddress(String s){
address = s;
}
// part c
public String invite(String person){
return "Dear " + person + ", please attend my event at " + address + ". See you then, " + hostName + ".";
}
// part d
public Invitation(String a){
hostName = "Host";
address = a;
}
}
- In this FRQ, I learnt about the anatomy of a class, including instance variables and methods
- I created overloaded constructors requiring different number of parameters
- I also practiced writing accessor methods, mutator methods, as well as concatenation
private int suffix_length;
private String prefix = "A.";
private static int counter;
public String pwGen(){
for (int i = 1; i<=suffix_length; i++){
prefix += Integer.toString((int) (Math.random()*10));
}
counter+=1;
return prefix;
}
public int pwCount(){
return counter;
}
- In this FRQ, I again practiced using overloaded constructors to generate passwords based on the parameters the user provided
- I practiced using a for loop to generate different passwords
- I also learned how to use the Math.random() function with casting and multiplication to get a custom range of random numbers
- Finally, I learnt how to use a static variable to count how many times the class was instantiated
Unit 6
public static void filter(String[] words){
// enhanced for loop through words
for(String s: words){
if (s.substring(Math.abs(s.length()-3),s.length()).equals("ing")){
System.out.println(s);
}
}
}
- In this FRQ, I learned how to use an enhanced for loop to go through an array
- I also learned how to use the substring function in a conditional to check for the end of the word
// unit 6, question 2
public class Payroll {
private int[] itemsSold = {48, 50, 37, 62, 38, 70, 55, 37, 64, 60}; // number of items sold by each employee
private double[] wages;
public Payroll(int[] x){
itemsSold = x;
wages = new double[itemsSold.length];
}
public void printwages(){
for (int i = 0; i < wages.length ; i++){
System.out.println(wages[i]);
}
}
public double computeBonusThreshold(){
int temp;
double average = 0;
for (int i : itemsSold){
average += i;
}
// loop through and sort array to find max and min
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;
}
}
}
// returning average of items excluding min and max
return ((average - itemsSold[0] - itemsSold[itemsSold.length-1]) / ((itemsSold.length)-2));
}
public void computeWages(double fixedWage, double perItemWage){
double threshold = computeBonusThreshold();
double bonus_rate = 1.1;
// loop through array, calculating the pay. then multiply by 1.1 (if 10%) if higher than bonus threshold
for (int x = 0; x < wages.length; x++){
wages[x] = fixedWage + (perItemWage * itemsSold[x]);
if(itemsSold[x] - threshold > 0){
wages[x] = wages[x] * bonus_rate;
}
}
}
public static void main(String[] args){
int[] array1 = {48, 50, 37, 62, 38, 70, 55, 37, 64, 60};
Payroll pay = new Payroll(array1);
System.out.println(pay.computeBonusThreshold());
pay.computeWages(10.0, 1.5);
pay.printwages();
}
}
- In this class, I created a constructor to define two arrays
- I also wrote a method to iterate through an array and print the contents
- Used an enhanced for loop to find the average items sold
- I used nested for loops and a temp variable to go through the int array and sort it, allowing me to find the min and max in the array
- Used a for loop and conditionals to set the values in an array
- I also used a tester method where I created an instance of the class and tested all of the methods
Unit 7
public class UserName
{
private ArrayList<String> possibleNames;
public UserName(String firstName, String lastName){
for(int x = 1; x <= firstName.length(); x++){
possibleNames.add(lastName + firstName.substring(0,x));
}
}
/** Returns true if arr contains name, and false otherwise. */
public boolean isUsed(String name, String[] arr){
for (String s : arr){
if(s.equals(name)){
return true;
}
}
return false;
}
/** Removes strings from possibleNames that are found in usedNames as described in part (b).
*/
public void setAvailableUserNames(String[] usedNames){
for(int s = 0; s < possibleNames.size(); s++){
if(isUsed(possibleNames.get(s), usedNames)){
possibleNames.remove(s);
s--;
}
}
}
}
- I learned how to use a for loop to traverse an ArrayList
- different ArrayList methods (add, remove) to check and compare specific values
- used an enhanced for loop with String.equals()
- learned how to manage the index to ensure that each item was check in an ArrayList if an item was deleted
Unit 8
public Plot getHighestYield(String c){
int highestYield = 0;
Plot bestplot = farmPlots[0][0];
for(int row = 0; row < farmPlots.length; row++){
for(int col = 0; col < farmPlots[1].length; col++){
if(farmPlots[row][col].getCropType().equals(c) && farmPlots[row][col].getCropYield() > highestYield){
highestYield = farmPlots[row][col].getCropYield();
bestplot = farmPlots[row][col];
}
}
}
return bestplot;
}
- In this FRQ, I practiced using two for loops to traverse a 2d array
- I learned how a 2D array is an array of arrays, and how to index row and columns using that knowledge
- Used compound boolean expressions to check multiple conditions
- Checked and modified values in a 2d array by using row and col indeces
public boolean sameCrop(int col){
String type = farmPlots[0][col].getCropType();
for(int row = 0; row < farmPlots.length; row++){
if(!(farmPlots[row][col].equals(type))){
return false;
}
}
return true;
}
}
- Here, I wrote a method that could traverse down a specific column of a 2d array and check the values
- practiced Boolean algebra and String methods
Unit 9
class PictureBook extends Book{
private String illustrator;
public PictureBook(String t, String a, String illustrator){
super(t, a);
this.illustrator = illustrator;
}
public void printBookInfo(){
super.printBookInfo();
System.out.println(" and illustrated by " + illustrator);
}
// part b
public static void main(String[] args){
ArrayList<Book> myLibrary = new ArrayList<Book>();
Book book1 = new Book("Frankenstein", "Mary Shelley");
Book book2 = new PictureBook("The Wonderful Wizard of Oz", "L. Frank Baum", "W.W. Denslow");
myLibrary.add(book1);
myLibrary.add(book2);
}
}
- In this FRQ, I practiced class inheritance
- used the extends keyword to create a child class
- created a subclass constructor that contains a reference to the super constructor to initialize the variables inherited from the parent class
- used the this keyword in the constructor
- Wrote a print method that overrrid superclass's method implementation, and called the super method inside it as well (using super keyword)
- practiced creating an ArrayList of Objects, and adding both parent and subclass objects to this arraylist, by having the declared type be different from the constructor (late binding)
// book listing class for unit 9, frq 1, part c
class BookListing{
private Book book;
private double price;
public BookListing(Book b, double d){
book = b;
price = d;
}
public void printDescription(){
book.printBookInfo();
System.out.println(", " + price);
}
public static void main(String[] args){
Book book1 = new Book("Frankenstein", "Mary Shelley");
Book book2 = new PictureBook("The Wonderful Wizard of Oz", "L. Frank Baum", "W.W. Denslow");
BookListing listing1 = new BookListing(book1, 10.99);
listing1.printDescription();
BookListing listing2 = new BookListing(book2, 12.99);
listing2.printDescription();
}
}
- In this part, I implemented the subclasses' methods in a new class to have different print messages be displayed, based on which printBookInfo method was called upon runtime (parent class method or subclass overridden method)
Unit 9
public class Animal {
private String diet;
private String species;
private String name;
public Animal(String d, String s, String n){
diet = d;
species = s;
name = n;
}
public String toString(){
return(name + " the " + species + " is a " + diet);
}
}
- In this part, I revised the anatomy of a class, creating a constructor and writing a method based on the instance variables
public class Herbivore extends Animal{
public Herbivore(String species, String name){
super("herbivore", species, name);
}
}
- Here, I practiced Inheritance in Java
- Specifically, I wrote the child class constructor to call the super constructor with specific parameters unique to the child class's implementation
public class Elephant extends Herbivore{
private double tusk_length;
public Elephant(String name, double tusk){
super("elephant", name);
tusk_length = tusk;
}
public String toString(){
return super.toString() + " with tusks " + tusk_length + " meters long";
}
}
- Continued to practice inheritance, created a child class of a child class, and created a constructor referring to the parent class's constructor
- also wrote a method that called on the parent class's toString method (using super keyword), which it inherited from its parent class (Animal class)
Unit 10
public class NumberSystem {
public static int gcf(int a, int b){
if(a%b == 0){
return b;
}
else{
return(gcf(b,a%b));
}
}
public static void reduceFraction(int numerator, int denominator){
if (numerator%denominator == 0){
System.out.println(numerator + "/" + denominator + " reduces to " + numerator/denominator);
}
else{
int new_numerator = numerator/gcf(numerator, denominator);
int new_denominator = denominator/gcf(numerator, denominator);
System.out.println(numerator + "/" + denominator + " reduces to " + new_numerator + "/" + new_denominator);
}
}
public static void main(String[] args){
NumberSystem.reduceFraction(500, 36);
}
}
- In this unit, I practiced using recursion to create an algorithm
- using the given math knowledge, I wrote the GCF function to find the greatest common factor b/w 2 numbers
- In this function, I used if else statements, with the if containing the base case (remainder = 0) and the else containing the recursive call with new parameters
- using the GCF function, I wrote a fraction reducing method
- again, the if had the base case that fraction is already reduced (remainder b/w num and denom is 0) to return the original fraction
- the else contained a call to GCF with the numerator and denominator, which would keep recursing until it found the GCF, and then use this info to reduce the fraction and then print it
- Used a main method to test the function with different fractions