CS 1401, Quiz #5

Date: Tuesday, February 15, 2005
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1. Write down a method that computes the cotangent cot(x) as the ratio of cos(x) and sin(x). This method should have the following header:

public static double cotangent(double x){

Answer:

public static double cotangent(double x){
  return Math.cos(x)/Math.sin(x);
}

2. To attract more tourists, the city of El Paso decided to officially change the name to Our Beautiful City of El Paso. Because of this change, we must change all the city documents. Describe the code that, given a string document in which the word "El Paso" appears exactly once, replaces the word "El Paso" in this string with the sequence "Our Beautiful City of El Paso". For example, the string
Cars parked illegally in El Paso will be towed away.
should be replaced with a string
Cars parked illegally in Our Beautiful City of El Paso will be towed away.
Hints: The length of a string s can be found as s.length(); a substring can be found as s.substring(first_in,first_out).
Answer: 

int location = document.indexOf("El Paso"); 
document = document.substring(0,location) + 
  "Our Beautiful City of El Paso" + 
  document.substring(location + "El Paso".length(),document.length()); 

3. What is a Boolean variable? What values can it take? Why do we need Boolean variables?
Answer:

A Boolean variable is a variable that can take only two values: "true" 
and "false". Boolean variables are needed to describe conditional
programs, programs in which the order of the operations depends on the 
data.