CS 1401, Exam #2

Date: Thursday, October 6, 2005
Name (please type legibly, ideally in block letters): ______________________________________________________________________

Exactly 10 years ago, on October 6, 1995, astronomers discovered the first planet outside the Solar system. This planet rotates around a star named 51 Pegasi in the constellation of Pegasus, 47.9 light-years away from Earth.

1. A light-year is the distance that requires 1 year for a light to travel. Write down a method named yearsToDays that, given the time in years, returns the corresponding amount of time in days. Remember that 1 year = 365.25 days. Trace your method, on the example of 2 years, and show that your method correctly computes the value 730.50.










































2. If the variable distance has the initial value 47.9, and we run the following sequence of statements, what will be the resulting values of the variables distance, far, and close:
int far = distance * 10.0;
int close = ((int) distance)/10; 
close++;
--close;
What is the advantage of using close++ instead of close = close + 1?


















































3. The original story in El Paso Times about the planet discovery had a typo: instead of "Pegasi" it erroneously had "Centauri". Write a method that, given a string story in which the word "Centauri" appears exactly once, returns a corrected string, in which the word "Centauri" is replaced with "Pegasi".
























































4. By now, several planets have already been discovered. Let us assume that you have discovered two planets, at distances distance1 and distance2 from Earth. Write a piece of code that will sort these values in increasing order, i.e., if distance1 is larger than distance2 (e.g., 15 > 10), it will swap the values of the two variables.
























































5. Astronomers have discovered three planets, at distances distance1, distance2, and distance3. Write down a method record1 that, given these three distances, returns the value "true" or "false" depending on whether the planet 1 is the farthest from Earth.
























































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


























































7. Let us assume that the distances to different planets in light-years are stored, line by line, in a file planets.dat. Write down a piece of code that, given this file, produces a new file days.dat in which the same distances are listed in light-days.
























































8. Define a class Planet with three fields name, distance, and radius, constructor method, and methods which return the planet's area and volume. Use constant(s) and methods from the Math package.

Reminder: