CSSE 221: Fundamentals of Software Development Honors

Key

Programming assignment

Finish BigRational, as stated in the specification, being sure you have checked in your final, fully-documented copy to the repository.

Written homework

As usual, please bring hardcopy for written answers to class to hand in on Monday.
  1. Be sure you are keeping up with the reading.
  2. I want to compare two BigRationals, x and y, to see if they have equal value (intuitively, x == y).
    1. Write an expression for this that uses the compareTo() method. (5 Points)
    2. Write an expression for this that uses the equals() method. (5 Points)
    3. What is compared if I write (x == y)? (Hint, you saw this in section 2.10. This is important to understand!) (10 Points)
  3. For the sake of simplicity, on this question, you may ignore effects such as operating system interrupts that may change the runtime slightly. (Focus on the meaning of big-Oh only.)
    1. A block of code is characterized as O(n) and runs in 23 milliseconds. If n is increased by a factor of 8, what is the new runtime of the code? (10 Points)
    2. A block of code is characterized as O(n2) and runs in 400 milliseconds. If n is increased by a factor of 3, what is the new runtime of the code? (10 Points)
  4. What combination of control structure(s) gives code that is O(n3)? (10 Points)
  5. How many times is the sum modified (in terms of n):
    for (int i = 0; i < n; i++) {
    
    	for (int j = 0; j < n; j++) {
    		sum++;
    	}
    	for (int j = 0; j < n; j++) {
    		sum++;
    	}
    }
    
    Exact answer = ______, giving an asymptotic answer of O( ______ ). (5 Points Each)
  6. Write the code for a method that does the following. (Note, you may choose to type this into Eclipse to test it, but you aren't required to do so. If you do type it in, please still print it out.)

    Pass into the method a BigRational object. Evaluate the object to determine if it is equal to its own absolute value. If it is, print the message "Good going", and terminate the program (or at least exit the method). If it is not, print the message "Not good going", and terminate the program. Regardless of the outcome of this test, print the message "Done" before terminating.

    Write this code twice: once using if-then-else block(s) to evaluate the data and handle a possible error, and once using a try-catch block to evaluate the data and handle a possible error. When using a try-catch block, use a "throw" statement to throw an exception for each case and handle that case within the catch block; you are allowed to use an if statement within the try-catch, but you should not print within the if statement. (20 Points Each)

    Note: This code would never be written using a try-catch block in a production environment, nor do we typically both throw and catch exceptions in the same method; we do it here to make sure you understand important structures.