CS 1073 Introductory Programming
for Scientific Applications
Final Exam, Fall 2004


Directions: Fill in answers on the pages below. Don't spend too much time on any one problem. Even if you have forgotten some things, fake it as well as you can.
Questions about simple loops.
  1. Write a loop that will print the following 8 numbers, where the first is 1, and each subsequent number is half the previous one. For full credit they should look exactly as shown, with a comma and a space after each number except the last. You must use a loop. You don't need to write the main function.


  2. Finish writing a program that will start with an integer n and will print a triangle with n rows and n columns that looks as follows, with selected values of n. For full credit you must use the function chars that is provided, and you must use a loop. Your program should also print out the first line, that looks like n = ?, where ? is the value of n.
    
    n = 10
    **********     
     *********
      ********
       *******
        ******
         *****
          ****
           ***
            **
             *
    
    n = 4
    ****       
     ***
      **
       *
    
    n = 12
    ************     
     ***********
      **********
       *********
        ********
         *******
          ******
           *****
            ****
             ***
              **
               *
    
    n = 1     
    *


Questions about arrays.
  1. Consider the array declaration:

    Write a simple code segment (with a loop) that will add up the square of each array entry. When you are done, print your answer. Thus your program should calculate 22 + 32 + 52 + 72 + 112 + 132 + 172, and it should do this by accessing the array elements. (Your program should not directly use the number 7, the number of elements in the array, but should work unchanged if one added 19 to the end of the above array.)


Question about functions.
  1. The problem is going to produce a Fahrenheit-to-Centigrade Conversion Table. Consider the following incomplete program that produces the output shown when completed:

    1. In the program above, give in the definition for a function ftoc that will convert Fahrenheit temperature to Centigrade and will work properly with the rest of the code. The formula is c = (5/9)(f - 32).

    2. What is the purpose of the following two lines in the program? Give the answer below them.

        
         if (f == 0) System.out.print("  ");
         else if (f > 0 && f < 100) System.out.print(" ");
                 
                 
        
        


Question about classes.
  1. Consider the following Student class (pretty much the same as was on the review for this final exam):

    Answer the following questions:
    1. Write code above for the constructor that has three parameters corresponding to the three data fields.
    2. Write code above for the getGPA method that will return the value of gpa.
    3. Write code above for the toString method that will return a String with the three data fields in it.
    4. Below, say what use a toString method might be put to. (What is it for?)

        
        
        
        
        

    5. Write code inside the main function of the separate Students class that will create Student classes with the following data for name, GPA, and collegeClass:
      • "Blow, Joe",     3.4, 2
      • "Bonkers, Bruce", 2.9, 3
      • "Nutcase, Nancy", 3.7, 2
    6. Write code inside the main function of the separate Students class that will invoke the getGPA method for the first student above (Joe Blow).


Question about arrays of Strings.

  1. This question is just a part of the "bottles of beer" assignment:

    Write a function (method) toEnglish that will convert its int input parameter (in the range from 0 to 99 inclusive) to English. [Hints: You must use the arrays of Strings given below. First handle an input parameter value n < 20 with the teens array. Remember, if the input parameter is n = 47 then n/10 will equal 4 and you can use the tens array. Similarly n%10 will equal 7 and you can use the ones array. Then concatenate.]


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