# Week7

请背诵四道FRQ（见PDF）的solution，并回答关于代码的相应问题（下面有例子）。理解代码很重要。

{% file src="https://1730460162-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fwc27wmKfnoDuqA5F9f2t%2Fuploads%2FpvibdNokDjnH9Q4rmSMg%2FTB_Homework_640aef32dcd694.640aef33ad0aa8.03754644%20(1).pdf?alt=media&token=31ca7a92-cf36-4152-a9f7-48d1430b55ea" %}

{% file src="https://1730460162-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fwc27wmKfnoDuqA5F9f2t%2Fuploads%2F4GpRwyCTEI9O7khBEmwU%2FTB_2DArrayHW_6414235d45fd28.6414235e1fe6a0.91745043%20(1).pdf?alt=media&token=ee7516da-4a7a-4e73-adb8-fdd9b5485473" %}

### Digits

```java
public Digits(int num) {
    digitList = new ArrayList<Integer>();
    if (num == 0) {
        digitList.add(0);
    }
    else {
        int n = num;
        while (n > 0) {
            digitList.add(0, n%10);
            n /= 10;
        }
    }
}
```

```java
public boolean isStrictlyIncreasing() {
    for (int i = 0; i < digitList.size() - 1; i++) {
        if (digitList.get(i) >= digitList.get(i + 1)) {
            return false;
        }
    }
    return true;
```

{% hint style="info" %}
可能会问到的问题包括但不仅限于：

1. 第二问中i的取值范围为什么是digitList.size()-1?
2. 第一问中为什么要检查num==0？不检查可以吗？

下面题目的问题类似。
{% endhint %}

### MemberInfo

```java
public void addMembers(String[] names, int gradYear) {
  for(String name: names) {
    MemberInfo newMem = new MemberInfo(name, gradYear, true);
    memberList.add(newMem);
}
```

```java
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;
}
```

### ArrayResizer

```java
public static boolean isNonZeroRow(int[][] array2D, int r)
{
  for(int c = 0; c < array2D[0].length; c++) {
    if(array2D[r][c] == 0)
      return false;
  }
  return true;
}
```

```java
public static int[][] resize(int[][] array2D)
{
  int[][] newArray = new int[numNonZeroRows(array2D)][array2D[0].length];
  int newR = 0;
  for(int oldR = 0; oldR < array2D.length; oldR++)
  {
    if(isNonZeroRow(array2D, oldR))
    {
      // could copy elements within row instead
      newArray[newR] = array2D[oldR];
      newR++;
    }
  } 
 return newArray;
}
```

### ArrayTester

```java
public static int[] getColumn(int[][] arr2D, int c)
{
  int[] column = new int[arr2D.length];

  for(int i = 0; i < column.length; i++)
    column[i] = arr2D[i][c];

  return column;
}

```

```java
public static boolean isLatin(int[][] square)
{
  if(containsDuplicates(square[0]))
    return false;

  for(int r = 1; r < square.length; r++)
    if(!hasAllValues(square[0], square[r]) )
      return false;

  for(int c = 0; c < square[0].length; c++)
    if(!hasAllValues(square[0], getColumn(square, c)))
      return false;

  return true;
}
```
