## 第一大题

### 第一问

```public boolean simulate()
{
int position = 0;
for (int count = 0; count < maxHops; count++)
{
position += hopDistance();
if (position >= goalDistance)
{
return true;
}
else if (position < 0)
{
return false;
}
}
return false;
}```

### 第二问

```public double runSimulations(int num)
{
int countSuccess = 0;

for (int count = 0; count < num; count++)
{
if(simulate())
{
countSuccess++;
}
}
return (double)countSuccess / num;
} ```

## 第二大题

### 第一问

```public WordPairList(String[] words)
{
allPairs = new ArrayList<WordPair>();
for (int i = 0; i < words.length-1; i++)
{
for (int j = i+1; j < words.length; j++)
{
}
}
}```

### 第二问

`String``equals`一直是重要考点之一。

```public int numMatches()
{
int count = 0;
for (WordPair pair: allPairs)
{
if (pair.getFirst().equals(pair.getSecond()))
{
count++;
}
}
return count;
}```

## 第三大题

`interface`概念、基本的constructor写法以及布尔表达式。

```public class CodeWordChecker implements StringChecker
{
private int minLength;
private int maxLength;
private String notAllowed;

public CodeWordChecker(int minLen, int maxLen, String symbol)
{
minLength = minLen;
maxLength = maxLen;
notAllowed = symbol;
}

public CodeWordChecker(String symbol)
{
minLength = 6;
maxLength = 20;
notAllowed = symbol;
}

public boolean isValid(String str)
{
return str.length() >= minLength && str.length() <= maxLength &&
str.indexOf(notAllowed) == -1;
}
}```

## 第四大题

### 第一问

```public static int[] getColumn(int[][] arr2D, int c)
{
int[] result = new int[arr2D.length];
for (int r = 0; r < arr2D.length; r++)
{
result[r] = arr2D[r][c];
}
return result;
}```

### 第二问

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