## CS 1401, Exam #2

Date: Thursday, February 24, 2005
Class section (9:00-10:20 am or 10:30-11:50 am): ______________________
Name (please type legibly, ideally in block letters): ______________________________________________________________________

Exactly 25 years ago, on February 24, 1980, the US Olympic Ice Hockey team has won its gold medal. This event was described in a recent movie "Miracle".

1. Write down a method named anniversary that, given the year of the event and current year, computes how many years have passed since that event. Show, on the example of 1980 and 2005, that your method correctly computes the value 25.

```

```
2. If a variable years has the initial value 25, and we run the following sequence of statements, what will be the resulting values of the variables next, many, and few:
```int many = years * 10.0;
int few = (int) years/10.0;
next = years++;
next--;
```
What is the advantage of using next-- instead of next = next - 1?

```

```
3. After sending an email to your friends about the hockey anniversary, you realized that you accidentally got confused with the Miners miraculous 1966 victory and typed basketball instead of hockey. Write a code that, given a string email in which the term "basketball" appears exactly once, replaces this term with "hockey".

```

```
4. In hockey, winning is easy to describe:
• if the first team scored more, it wins, so it gets 2 points and the second team gets none;
• if the second team scored more, then the second team wins, it gets 2 points and the first team gets none;
• if both teams scored the same, they get 1 point each.
Suppose that we know the scores score1 and score2 of the two teams. Write down a piece of code that assigns the corresponding values to the variables points1 and points2 (these variables represent the number of points assigned to each team).

```

```
5. In the competition between the three teams 1, 2, and 3, a team becomes a champion if it has earned more points than every other team. Write down a method champion1 that, given the overall number of points points1, points2, and points3 earned by each team, returns the value "true" or "false" depending on whether Team 1 is a champion.

```

```
6. What are the reasonable test cases for the method you developed for Problem 5? Explain your answer.

```

```
7. Let us assume that the points earned by the team in different games are stored, line by line, in a file points.dat. Write down a piece of code that, given this file, computes the overall number of points earned by the team.

```

```
8. To celebrate their victory, the hockey players usually skate in circles. To describe this celebration, define an object circle with the only field radius, constructor method, and methods that return the circle's diameter, circumference, and area. Use constant(s) and methods from the Math package.

Reminder:

• the diameter of circle is equal to 2 times its radius;
• the circumference of a circle is 2*PI times its radius;
• the area of a circle is PI times the radius squared (use the corresponding method from the Math package).