CS 1401, Exam #2

Date: Thursday, March 2, 2006

Class section (9:00–10:20 am or 10:30-11:50 am): ____________________

Name (Please type legibly, ideally in block letters):

 

_________________________________________________________________________

 

Athletic events of the Torino Winter Olympic games were all held indoors. However, weather was a factor at the closing ceremonies, last Sunday at the Stadio Olimpico. According to USA Today, the average February daytime high in Torino is about 7.8°C, with nighttime lows dipping to near 1.1 °C below zero.

 

1. Write a method named celsiusToFahrenheit that converts temperature readings from Celsius to Fahrenheit degrees. Trace your method on the example of 100 oC and show that your method correctly computes the value of 212 oF.

Use the conversion equation:

 

Fahrenheit  =

_9_

Celsius  + 32

5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


2. If the variable freezePoint has the initial value 32, and we run the following sequence of statements, what will be the resulting values of the variables cool, reallyCold, and boiling?

 

int cool = freezePoint * (int) 1.2;

int reallyCold =  cool / 10;

double boiling = 7.0 * freezePoint;

reallyCold--;

 

What is the advantage of using reallyCold-- instead of  reallyCold = reallyCold - 1?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


3. The news about the next Olympic Games inaccurately cited Sweden as the host country. Write a method that, given a string media in which “Sweden” appears exactly once, returns a corrected string in which “Sweden” is replaced with “Canada”.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


4. Yevgeny Plushenko won the gold medal in the men’s figure skating competition at the Torino Olympic Games. Suppose that we stored the points obtained by two skaters in variables firstPlace and secondPlace. Write a code segment that will correctly sort these values, i.e., make sure that firstPlace has the most points by swapping the values of the variables if needed.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


5. The top three competitors of the women’s giant slalom in the Torino Olympic Games were Julia Mancuso from the U.S., Finland’s Tanja Poutinanen, and Sweden’s Anna Ottoson. Write a method usGold that, given the overall time for Julia, Tanja, and Anna, returns true or false depending on whether Julia obtained the fastest time.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


7. Italy’s Winter Olympics Committee commissioned a giant golden sign with the Olympic rings. Manufactures of the sign need to compute the amount of material to use for the rings. Define a class OlympicRing with fields innerRadius and outerRadius, constructor method based on the ring radii, and methods that return the area, the outer circumference, and the inner circumference of a ring. Use constant(s) and methods from the Math package.

Remember that:

·         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).