## 第一大题

### 第一问

```public int getDuration()
{
if (flights.size() == 0)
{
return 0;
}

Time depart = flights.get(0).getDepartureTime();
Time arrive = flights.get(flights.size() - 1).getArrivalTime();
return depart.minutesUntil(arrive);
}```

### 第二问

```public int getShortestLayover()
{
if (flights.size() < 2)
{
return -1;
}

int shortest = getDuration();
for (int k = 1; k < flights.size(); k++)
{
Flight flight1 = flights.get(k - 1);
Flight flight2 = flights.get(k);
Time arrive = flight1.getArrivalTime();
Time depart = flight2.getDepartureTime();
int layover = arrive.minutesUntil(depart);
if (layover < shortest)
{
shortest = layover;
}
}

return shortest;
}```

## 第二大题

### 第一问

```public static String apcsReplaceAll(String str,
String oldStr,
String newStr)
{
String firstPart = "";
String lastPart = str;
int pos = lastPart.indexOf(oldStr);
while (pos >= 0)
{
firstPart += lastPart.substring(0, pos);
firstPart += newStr;
lastPart = lastPart.substring(pos + oldStr.length());
pos = lastPart.indexOf(oldStr);
}
return firstPart + lastPart;
}```

### 第二问

```public static String replaceNameNickname(String str,
List people)
{
for (Person p : people)
{
str = apcsReplaceAll(str, p.getFirstName(), p.getNickname());
}
return str;
}```

## 第三大题

### 第一问

```public class Cat extends Pet
{
public Cat(String petName)
{  super(petName);  }

public String speak()
{
return "meow";
}
}```

### 第二问

```public class LoudDog extends Dog{
public LoudDog(String petName)
{  super(petName);  }

public String speak()
{
return super.speak() + super.speak();
}
}```

### 第三问

```public void allSpeak()
{
for (Pet a : petList)
{
System.out.println(a.getName() + a.speak());
}
}```

## 第四大题

### 第一问

```public static void shiftArray(int[] arr, int num)
{
for (int k = arr.length − 1; k > 0; k−−)
{
arr[k] = arr[k - 1];
}

arr[0] = num;
}```

### 第二问

```public void shiftMatrix(int num)
{
for (int[] row: matrix)
{
int temp = row[row.length − 1];
ArrayUtil.shiftArray(row, num);
num = temp;
}
}```

### 第三问

```public void rotateMatrix(){
shiftMatrix(matrix[matrix.length − 1][matrix[0].length − 1]);
}```