CS 1713-001 Introduction to Computer Science
Spring 2001 -- Exam 1
Directions: Use your own paper for answers. When you are done you may keep this exam sheet and should pick up an answer sheet. (For problem 2 below, you should not copy the supplied code, but should just show what code you would supply.)

  1. Assume your program is reading a sequence of positive integers ("scores") with a -1 to mark the end. Assume GetNext.getNextInt() will read and return each new integer. Stop reading when you read the final -1. You must use a loop of some kind, and you should not use an array.
    1. Store the sum of the numbers (but not the final -1) in a variable sumScores.
    2. Count the numbers as you read them (but not the final -1) and store the count in a variable numScores.
    3. Store the largest integer read in a variable maxScores.
    4. If the number of scores read is even, print Even and otherwise print Odd. (You do not need to print anything else.)
    [For example, if scores 80 70 90 60 -1 are read, sumScores in 300, numScores is 4, maxScores is 90, and the program should print Even.]
  2. Consider the following partially completed class HealthRecord:
    // HealthRecord.java: store health information
    public class HealthRecord 
    { 
       public static final double FEVER = 100.4;
       private String name; 
       private double age; 
       private double temperature; 
    
       public HealthRecord(String n, double a, double t) { 
          // fill in for part a.
       } 
    
       public String getName() { 
          return name; 
       } 
    
       public double getAge() { 
          return age;
       }
     
       public double getTemperature() { 
          return temperature; 
       } 
    
       public boolean hasFever() { 
          // fill in for part b.
       } 
    
       public boolean needsBloodWorkup() { 
          // fill in for part c.
       } 
    } 
    
    // HealthRecordDemo.java: test HealthRecord class
    public class HealthRecordDemo {
       public static void main (String[] args) {
          // fill in code for parts d, e, and f.
       }
    } 
    
    
    1. Fill in the code for the constructor of the HealthRecord class.
    2. By definition, a patient has a fever if his temperature is greater than or equal to 100.4. Fill in the code for the method hasFever(). (Be sure to use the final constant FEVER.)
    3. A patient with a fever under 1/2 year old needs a blood workup. Fill in the code for the method needsBloodWorkup.
    4. In the main function of HealthRecordDemo class, create two instances of the HealthRecord class, one with identifier patient1 named "Joe Jones" of age 2.0 with temperature 101.5, and the second with identifier patient2 named "John Junior" of age 0.3 with temperature 102.1.
    5. In the main function of the HealthRecordDemo class, write a statement that will check if paitent1 has a fever and will print his name along with " has a fever" in case he does.
    6. In the main function of the HealthRecordDemo class, write a statement that will check if paitent2 needs a blood workup and will print his name along with " needs a blood workup" in case he does.

  3. The Math class has a method random that is described in your text as follows:
      public static double random()
        Returns a random number between 0.0 and 1.0.

    Similarly the Random class has a method nextDouble that is described in your text as follows:
      public double nextDouble()
        Returns a random number between 0.0 and 1.0.

    Using either method, give an expression that will return a random integer between 1 and 6, inclusive, to use in throwing dice, say. (Your answer should just be a single expression.)


  4. Write a Java program segment (no need for the enclosing main function) that uses whatever value is in the integer variable n to print the following pattern of stars: n rows, where the first row has n stars, and each subsequent row has one fewer star, and the rows are lined up at the right (so there are initial blanks). Here is an example with n equal 6
    
    ******
     *****
      ****
       ***
        **
         *
    

    [Hint: Use a for loop to let row i go from 0 to n - 1. Then use for loops inside the main loop so that on row i, you print i blanks followed by n - i stars.]


    Points for each problem: 1-30, 2-40, 3-10, 4-20.