Published using Google Docs
Discussion 5: APTs and More, Compsci 201, Spring 2021
Updated automatically every 5 minutes

Discussion 5: APTs and More

Discussion Reflect

Groups, Say Hello (10 minutes)

PairDown APT (15 minutes)

SetAside (20 minutes)

MaxDonor APT (20 minutes)

Useful Methods and Classes (15 minutes)

Discussion Reflect

Complete the reflection form found here: D5 Reflect Form. Each of the parts of Discussion below has its own section in the form; you can complete them as you go.

Groups, Say Hello (10 minutes)

Today, your UTAs will assign you to your pre-assigned breakout rooms from last week. The students in your breakout room will be a more permanent group of peers for you to work with. You will likely work with this group in every discussion and during lecture.

Your TA will give you a link to the Microsoft Teams channel for your group, where you can communicate about the course and work on peer instructions together during lecture. You all already have access to the course Team, so this will just be making sure that you join the correct channel. Make sure those in your discussion group know each other's names and your section UTAs' names.

PairDown APT (15 minutes)

Read the PairDown APT writeup.

Discuss in your groups the following ideas.

  1. In terms of list.length, what is one expression that gives the size of the returned array no matter whether list.length is odd or even? You can create a table to help answer this question.
  2. Suppose you loop over the even indexes for the array list, e.g.,

    for(int k=0; k < list.length; k += 2) {...

Explain how to index, in terms of k, the two values from list that will be added together and stored in the returned array.

  1. What are some ideas to avoid indexing off the end of list when it contains an odd number of entries, e.g., if it's three the returned array will contain list[0]+list[1] and list[2]?
  2. If you use the for loop shown in a previous step, what expression in terms of k is the index in the returned array at which a value is stored as the loop iterates?

Based on these ideas, what is the group consensus on how long it will take to use these ideas and get an all green solution: D5 Reflect Form.

SetAside (20 minutes)

This part is about the SetAside APT.

Some background information:

  1. For two Set<String> objects a and b, the value of a.retainAll(b) is the set intersection of sets a and b, that is the elements in common to the two sets. Note that this operation alters the set a, so it is the intersection of the two. Conceptually this is like writing a = a ∩ b.
  2. The expression String.join(" ",set) returns a single string with spaces between each element of the set, which are strings in this problem.
  3. Using a TreeSet instead of a HashSet will result in a for loop returning the values of the set in alphabetical order. In particular, if used in a String.join expression the resulting strings will be in alphabetical order.

Explore in your group this idea: Start with a set common containing the Strings in the first element of the array list. Then loop over each of the other strings in list making common the intersection of itself and each set created from the string.

  1. How will you create a set from the String "one two three" ?
  2. How will you loop over all the Strings in list after the first one, which was used to initialize common?
  3. How will you use String.join to create the return String?
  4. In your group, someone should open and share an IntelliJ window, and either in an existing APT project, or a new one, sketch out in code these ideas. You don't need to test the code, you should think about it and discuss in your group. Copy paste the code you write into the reflect document. Share the code with each other.

Answer questions in D5 Reflect Form.

MaxDonor APT (20 minutes)

This is about the MaxDonor APT.

This problem was from a previous APT quiz. To start thinking about these quizzes, we'd like you, in your group, to read the problem description on your own for 3-5 minutes and then discuss what questions you have and what high-level ideas you have to solve the problem.

  1. How could you use a map as part of solving this problem? What will the keys and values be? How will you update the keys and values as part of solving the problem?
  2. If you use a map, how will you determine which key/value pair to return? What map methods will you use?
  3. In your group, someone should open and share an IntelliJ window, and either in an existing APT project, or a new one, sketch out in code these ideas. You don't need to test the code, you should think about it and discuss in your group. Copy paste the code you write into the reflect document.

Answer questions in D5 Reflect Form.

Useful Methods and Classes (15 minutes)

See the jshell session below to understand conversion between arrays and ArrayList/List objects. Answer these questions

  1. What does Arrays.asList() return? Type and conceptual
  2. Does Java correctly determine (conceptually) if two arrays are equal? What about two ArrayLists?
  3. Why can String[] and ArrayList<String> be converted back and forth but int[] and ArrayList<Integer> cannot be? (e.g., with .toArray and Arrays.asList)

Answer questions in D5 Reflect Form.

|  Welcome to JShell -- Version 10.0.1

|  For an introduction type: /help intro

jshell> String[] a = {"zoo", "yak", "ant", "cat"};

a ==> String[4] { "zoo", "yak", "ant", "cat" }

jshell> List<String> list = Arrays.asList(a);

list ==> [zoo, yak, ant, cat]

jshell> String[] b = list.toArray(new String[0])

b ==> String[4] { "zoo", "yak", "ant", "cat" }

jshell> a == b

$4 ==> false

jshell> a.equals(b)

$5 ==> false

jshell> a[0] == b[0]

$6 ==> true

jshell> a[1] == b[1]

$7 ==> true

jshell> a[2] == b[2]

$8 ==> true

jshell> a[3] == b[3]

$9 ==> true

jshell> List<String> list2 = Arrays.asList(b)

list2 ==> [zoo, yak, ant, cat]

jshell> list == list2

$11 ==> false

jshell> list.equals(list2)

$12 ==> true

jshell> int[] a = {1,2,3,4};

a ==> int[4] { 1, 2, 3, 4 }

jshell> List<Integer> xyz = Arrays.asList(a)

|  Error:

|  incompatible types: inference variable T has incompatible bounds

|      equality constraints: java.lang.Integer

|      lower bounds: int[]

|  List<Integer> xyz = Arrays.asList(a);

|                      ^--------------^

jshell> ArrayList<Integer> xyz = new ArrayList<>();

xyz ==> []

jshell> for(int i : a) xyz.add(i)

jshell> xyz

xyz ==> [1, 2, 3, 4]

jshell> String[] nums = {"12", "35", "78"}

nums ==> String[3] { "12", "35", "78" }

jshell> int[] anums = new int[nums.length]

anums ==> int[3] { 0, 0, 0 }

jshell> for(int k=0; k < nums.length; k += 1)

   ...>     anums[k] = Integer.parseInt(nums[k])

jshell> anums

anums ==> int[3] { 12, 35, 78 }

jshell> nums

nums ==> String[3] { "12", "35", "78" }

jshell>