CSCD 226                                                                   NAME_______________________

(Mini) Exam 3                                                              Section______

50 points possible

 

Recursion Section

 

  1.  (10 points)  Given the following recursive code, (a) show the recursive calls that will be made (a stack or tree is fine) and (b) show the final result for an initial call of System.out.println(superFact(4));

 

int superFact(int N)

{

if (N <= 1)

return 1; //base case

else

return N * superFact(N – 1) * superFact(N – 2); //recursive case

   }//end method

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. (15 points) Fibbonacci numbers are a famous series of numbers that represent, of all things, the potential amount of rabbits that will reproduce over a period of time.  A Fibbonacci number can be calculated as follows:

 

a)       The Fibbonacci value of 0 is defined as 0

b)       The Fibbonacci value of 1 is defined as 1

c)       All subsequent Fibbonacci values are calculated by adding the two previous Fibbonacci values together.  So the Fibbonacci value of 2 is the Fibbonacci value of 1 plus the Fibbonacci value of 0 (for a result of 1).  The Fibbonacci value of 3 is the Fibbonacci value of 2 plus the Fibbonaci value of 1 (for a result of 2).  This means that the Fibbonacci value of some number N (N >1) is calculated by adding the Fibbonacci value of N-1 to the Fibbonacci value of N-2.

 

Write a recursive method called Fib that will compute the Fibbonacci value of an integer N that is passed into it.  The method should return this value (which will also be an integer).  You are guaranteed that N will be positive.  As an example, an initial call to the method as follows: System.out.println(Fib(6)); should print a result of 8.  Use the back of this page if necessary.

 


Stream Section

public static void main(String [] args)

{

BufferedReader inFile;

 

try

{

 

inFile = openInputFile();     //problem 3

readContents(inFile);         //problem 4

 

inFile.close();

}//end try

 

catch (FileNotFoundException ex)

{

System.out.println(“Input file not found!  Program will terminate.”);

}//end catch

 

catch (IOException ex)

{

System.out.println(ex);

}//end catch

 

}//end main method

 

 

  1. (10 points) Write a method called openInputFile that does the following:

 

a)      gets the name of an input file from the user (3 points extra credit if you do NOT use the Keyboard class)

b)      opens the input file

c)      returns a BufferedReader object that is associated with the input file

d)      accounts for (but does not handle) any exceptions that may be thrown as the result of the above actions

 

Be sure and declare all variables necessary for the method to function correctly!

 


 

  1. (15 points) Write a method called readContents that is passed the BufferedReader object from problem 3 and does the following:

 

a)      reads lines of text (one at a time – you do NOT have to do this recursively!) from the BufferedReader object until the end of the file is reached

b)      parses each line read for a string that represents the day of the week followed by an integer that represents the high temperature for that day

c)      the above information (day and high temperature) should be written to the monitor, nicely formatted

d)      the method should account for and handle any exceptions that result from parsing

e)      the method should account for (but does NOT handle) any exceptions that result from reading operations from the BufferedReader object.

 

You are guaranteed each line in the input file will contain two entries per line.  You are guaranteed the first entry will be a day of the week (a string).  The second entry on the line (the temperature) may not be an integer.   The entries will be separated by a space.