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.


public int anniversary(int event, int current){
  return current - event;}


int age = anniversary(1980, 2005); 

this call will compute the value 2005 - 1980 =25 and assign it to the
variable age

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 = (int) years * 10.0; 
int few = (int) years/10.0; 
next = years++;
What is the advantage of using next-- instead of next = next - 1?


many = 250, 
few = 2,
next = 25 after the third statement, then 24

next-- is shorter than next = next - 1, so it is faster for a
programmer to write; besides, if you have to repeat the variable name
twice, you make a mistake and accidentally type in two different names

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".


int location = email.indexOf("basketball"); 
email = email.substring(0,location) + "hockey" + 
  email.substring(location + "basketball".length(),email.length()); 

4. In hockey, winning is easy to describe: 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).


if (score1 > score 2)
  {points1 = 2; points2 = 0;}
else if (score2 > score 1)
  {points1 = 0; points2 = 2;}
  {points1 = 1; points2 = 1;}

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.


public Boolean champion1(int points1, int points2, int points3){
  return (points1 > points2) && (points1 > points3);

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


* a case when the first team gets more points than others, e.g., 10, 8,
and 7; 

* a case when the first team in not a champion, e.g., 7, 8, and 10; 

* a borderline case, e.g., 10, 10, and 8

* a random case, when all three variables get values from a random
number generator 

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.


Buffered Reader in = new BufferedReader(new FileReader("points.dat")); 
String line; 
int overall = 0; 

while((line = in.readLine())!=null){
  overall = overall + Integer.parseInt(line);}


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.



import Math.*; 

public class Circle{
  private double radius; 
  public Circle(double aradius){
    radius = aradius;

  // this method is only needed if we define the field as private
  public double getRadius(){
    return radius;

  // this method is only needed if we define the field as private
  public void setRadius(int aradius){
    radius = aradius;

  public double diameter(){
    return 2.0*radius;

  public double circumference(){
    return 2.0*Math.PI*radius;

  public double area(){
    return math.Pi*power(radius,2.0);