• Most of these were done with the help of Marks Education Tutorials, and Vallandi Allulis on youtube.

2014

  • Consider the following partial declaration for a WordScrambler class. The constructor for theWordScrambler class takes an even-length array of String objects and initializes the instance variable scrambledWords.

  • Takes a word a scrambles it.

  • What we want to do is iterate through the characters in word. If we have an A followed by not A, then we swap those two characters. Then we return the word.

public static String scrambleWord (String word) {
    for(int i = 1; i < word.length(); i++)  // set index to 1 and iterate through the string
        {
            if (word.substring(i - 1, i).equals("A") && // letter prior to index
            !word.substring(i, i + 1).equals("A")) // Continues if not equal
            {
                word = word.substring(0, i - 1 ) + word.substring(i, i + 1) // from the 0 of index to the letter prior to the A we are trying to swap. Then capture character at index.
                    + word.substring(i - 1, i) + word.substring(i + 1); // Passes parameter to the end of the string
                i++;
            }
        }
    
        return word;

        
    }

2020

  • Methods and Control Structures
  • Help detect if an error has occured when a number is entered or transmitted electoronically.
public static boolean isValid(int numWithCheckDigit) {

    int lastDigit = numWithCheckDigit % 10;
    int num = numWithCheckDigit / 10;

    if (getCheck(num) == lastDigit) {
        return true;
    }
    else{
        return false;
    }
}

In order to count how many times the isValid method returned false, a new integer counter field would need to be created for the CheckDigit class. Every time, either in the else statement in the isValid method, or whenever the isValid method returns false within another class from method, this field should be incremented by 1.

2015

  • This question involves reasoning about one-dimensional and two-dimensional arrays of integers. You will write three static methods, all of which are in a single enclosing class, named DiverseArray (not shown). The first method returns the sum of the values of a one-dimensional array; the second method returns an array that represents the sums of the rows of a two-dimensional array; and the third method analyzes row sums.
//part A
public static int arraySum(int[] arr)
{
   int sum = 0;

   for(int n : arr)
     sum += n;

   return sum;
}

//part B
public static int[] rowSums(int[][] arr2D)
{
   int[] sums = new int[arr2D.length];

   for(int i = 0; i < sums.length; i++)
   {
      sums[i] = arraySum(arr2D[i]);
   }

   return sums;
}

//part C
public static boolean isDiverse(int[][] arr2D)
{
   int[] sums = rowSums(arr2D);

   for(int i = 0; i < sums.length; i++)
      for(int j = i+1; j < sums.length; j++)
         if(sums[i] == sums[j])
            return false;

   return true;
}

2D array FRQ

public static boolean isNonZeroRow(int[][] array2D, int r)
{
    for(int v : array2D[r])
        if(v == 0)
            return false;

    return true;
}

Arrays ArrayLists FRQ

Question 1

public void addMembers(String[] names, int gradYear)
{
    for(String name : names)
        memberList.add(new MemberInfo(name, gradYear, true));
}
public ArrayList<MemberInfo> removeMembers(int year)
{
    ArrayList<MemberInfo> goodStanding = new ArrayList<MemberInfo>();	

    int i = 0;
    while(i < memberList.size())
    {
        if(memberList.get(i).getGradYear() <= year)
        {
            MemberInfo removed = memberList.remove(i);
            if(removed.inGoodStanding())
                goodStanding.add(removed);
        }
        else
            i++;
    }
            
    return goodStanding;
}

Question 3

public static int[] getCubeTosses(NumberCube cube, int numTosses)
{
    int[] tosses = new int[numTosses];

    for (int i = 0; i < tosses.length; i++)
        tosses[i] = cube.toss();

    return tosses;
}
public static int getLongestRun(int[] values)
{
    int lengthOfLongestRun = 1;
    int startOfLongestRun = -1;

    int lengthOfCurrentRun = 1;

    for (int i = values.length - 2; i >= 0; i--)
    {
        if (values[i] == values[i + 1])
        {
            lengthOfCurrentRun++;
            if(lengthOfCurrentRun > lengthOfLongestRun)
            {
                lengthOfLongestRun = lengthOfCurrentRun;
                startOfLongestRun = i;
            }
        }
        else
            lengthOfCurrentRun = 0;
    }

    return startOfLongestRun;
}

Class FRQ

Question 3

public class MultPractice implements StudyPractice
{
    private final int first;
    private int second;

    public MultPractice(int f, int s)
    {
        first = f;
        second = s;
    }

    public String getProblem()
    {
        return first + " TIMES " + second;
    }

    public void nextProblem()
    {
        second++;
    }
}

Combined Table FRQ

public class CombinedTable
{
    private SingleTable t1, t2;

    public CombinedTable(SingleTable t1, SingleTable t2)
    {
        this.t1 = t1;
        this.t2 = t2;
    }
        
    public boolean canSeat(int people)
    {
        int seats = t1.getNumSeats() + t2.getNumSeats() - 2;
        return seats <= people;
    }
        
    public double getDesirability()
    {
        double averageView = (t1.getViewQuality() + t2.getViewQuality()) / 2;

        if(t1.getHeight() == t2.getHeight())
            return averageView;
        else
            return averageView - 10;
    }
    
}