assign10-p3

# assign10-p3 - Name Thomas Keating a Let n = length of...

This preview shows pages 1–2. Sign up to view the full content.

Sheet1 Page 1 Name: Thomas Keating a) Let n = length of String s. Let m = length of String s up to an error (if there is one). The Integer.parseInt(s) runs until either it reaches the end of the string or until it reaches an error. So if there are no errors it runs n times and then it prints, which has O(1) running time. Worst case: (runs to completion with no errors) O(n) + O(1) = O(n) If there is an error, then Integer.parseInt(s) runs m times. It then goes to catch, where it prints, which has running time O(1). Best Case:(error on first digit scanned, so m = 1) O(m) + O(1) =O(1) + O(1) = O(1). b) Let n = length of String s. The int assignment has O(1). The for loop runs n times, regardless of whether or not it reaches an error in the try method. s.length runs in constant time and has O(1) running time. (((Comment: won't there be an error in this method each time, since the code is: (((String c = s.substring(i, i+1) (((int digit = Integer.parseInt(c) (((sum = sum + c (((In the final line, sum, an integer, is added with c, which is a string. (((Shouldn't it be sum = sum + digit? For my analysis, I'm going to assume the program is correct.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/03/2008 for the course CS 111 taught by Professor Hari during the Spring '08 term at Rutgers.

### Page1 / 2

assign10-p3 - Name Thomas Keating a Let n = length of...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online