2022 #1a

First attempt:

public int getScore() {
    Level goal = new Level();
    Level points = new Level(); 
    int pointTotal = 0; 
    if (goal.goalReached()) {
        pointTotal = points.getPoints();
    }
}

I got stuck at the point above, so I looked at part of the answers. Here is my attempt afterwards:

What the code does:

A variable pointTotal is created that records the amount of points that the player earns. Since the points from level 2 and level 3 are earned only if the previous level is completed, I created if loops within the if loop.

After the points from the levels are totaled, the code then tests the ifBonus method. This is a boolean if statement where if ifBonus is true, the total score would triple.

public int getScore() {
    int pointTotal = 0; 
    if (levelOne.goalReached()) {
        pointTotal += levelOne.getPoints();

        if (levelTwo.goalReached()) {
            pointTotal += levelTwo.getPoints();

            if (levelThree.goalReached()) {
                pointTotal += levelThree.getPoints();
            }
        }
    }

    if (isBonus()) {
        pointTotal *= 3;
    }

    return pointTotal;
}

Questions:

  • Why are objects not defined to call goalReached and getPoints?

After some researching, I learned from this video that if methods are in the same class (isBonus and getScore are in the same class), an object does not need to be created to call the method.

#1b

public int playManyTimes(int num) {
    int scores[];
    int maxScore = 0;
    for (int i = 0; i < num; i++) {
        play(); 
        scores[i] = getScore();
    }

    for (int i = 0; i < num; i++) {
        if (scores[i] > maxScore) {
            maxScore = scores[i];
        }
    }

    return maxScore;
}

An array wasn't needed. All you had to do was compare the scores within the for loop like this:

public int playManyTimes(int num) {
    int maxScore = 0;
    int score = 0;
    for (int i = 0; i < num; i++) {
        play(); 
        score = getScore();
        if (score > maxScore) {
            maxScore = score;
        }
    }

    return maxScore;
}


2016 #1a

Initial thoughts:

When I first read the question, I did not know where to start, so I looked at part of the answers to guide me on what to learn.

Looking at this,

I was confused as to what List<String> was, so I googled it.

I learned that List<String> is used to make an object that can store the array list. For instance, List<String> list = new ArrayList<String>();

I also saw how an array list can also be created with ArrayList<String>. The difference between ArrayList<String> and List<String> is that with List<String>, you can typecast the array list into a different type of list; however, you can't do that with ArrayList<String>.

The code below is what I got started with before I became stuck again.

public class RandomStringChooser {
    private List<String> words;
    public RandomStringChooser(String[] wordArray) {
        words = new ArrayList<String>();
    }
}

Once again not knowing how to proceed, I took a look at the answers and came upon this:

Because I had no idea what the syntax meant, I hopped onto Google and did some searching.

So, for (String singleWord : wordArray) is an example of a for-each loop. A for-each loop allows you to loop through the elements in an array list.

Below is a simple example of a for-each loop:

public class ForEachDemo {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<String>(Arrays.asList("apple", "pineapple", "mango"));

        for (String i : fruits) {
            System.out.println(i);
        }
    }
}
ForEachDemo.main(null);
apple
pineapple
mango

This essentially creates is the same as using a regular for loop, and printing out each index in the array list using .get. .get returns the element specified in the index number.

public class GetDemo {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<String>(Arrays.asList("apple", "pineapple", "mango"));

        // same as: 
        // for (String i : fruits) {
            // System.out.println(i);
        // }
        for (int i = 0; i < fruits.size(); i++) {
            System.out.println(fruits.get(i));
        }
    }
}
GetDemo.main(null);
apple
pineapple
mango

The code below is my continued attempt at answering 1a, after learning about the for-each loop.

public class RandomStringChooser {
    private List<String> words; // creating an object words
    public RandomStringChooser(String[] wordArray) {
        words = new ArrayList<String>();  // assigning words to a new array list

        for (String singleWord : wordArray) {
            // add all of the elements in wordArray to words
            words.add(singleWord); 
        }

        public String getNext() {
            return words.remove((int)(Math.random() * words.size())); 
        }



    }
}

This was close, but I forgot that NONE should be the output once all of the elements in the array list have been used. The correct answer is:

public class RandomStringChooser {
    private List<String> words; // creating an object words
    public RandomStringChooser(String[] wordArray) {
        words = new ArrayList<String>();  // assigning words to a new array list

        for (String singleWord : wordArray) {
            // add all of the elements in wordArray to words
            words.add(singleWord); 
        }

        public String getNext() {
            // forgot to write an if statement
            if (words.size() > 0) {
                return words.remove((int)(Math.random() * words.size())); 
            }
            return "NONE";
        }



    }
}

Concluding thoughts

This question was hard mainly because I had no idea what an array list was prior to doing this question. Although I looked at the answer key a few times, I researched all of the code that I did not understand to help me learn what an array list was, how to create it, and how to use iteration with array lists.


1b

Explanation:

If I'm understanding correctly, when the object letterChooser is created, the RandomLetterChooser constructor will run. Since we want to create an array of strings, we would use the super keyword to call the getSingleLetters method, which will split the word into an array of strings. Then, System.out.print(letterChooser.getNext()); is ran, which will call the superclass's constructor and then run the getNext method. This will randomly output the characters.

The code below matched the answer key.

However, one thing that I wondered as I was examining the answer was, why was there no super() in RandomLetterChooser, which would call the superclass (RandomStringChooser). The answer, after some googling, told me that even if you don't type super(), the superclass will still implicitly be called.

public RandomLetterChooser(String str) {
    super(getSingleLetters(str));
}