Question: 1. Write a method named sqrt that takes a double parameter and returns its square root with a margin of error 0.01. There should be
1. Write a method named sqrt that takes a double parameter and returns its square root with a margin of error 0.01. There should be no calls to other methods inside sqrt. Do not worry about the class containing sqrt, just work out the method itself.
2. What is the approximate running time of your sqrt method from Question 1 on an input n? What is the order of magnitude of the running time, using -notation? What is a lesser order of magnitude? What is a greater order of magnitude?
3. Find ten errors (there are more than ten) in the following Java class. The errors are both syntactic (compiler errors) and logical (bugs). For each error, identify the line number and briefly explain how to fix it.
1 import java.io.* ; 2 3 /* A class that counts the letter frequency in a text 4 * input stream. */ 5 6 public class Count { 7 8 /* Counts the letter frequency in a character 9 * input stream. */ 10 public static count(String fileName) { 11 /* initialize the frequency counts */ 12 int[] alpha = new int[] ; 13 for( int i=0 ; i <= alpha.length ; i++ ) 14 alpha[i] = 0 15 16 /* Keep track of line numbers for error 17 * reporting. */ 18 try { 19 LineNumberReader in2 = new LineNumberReader( 20 new FileReader( fileName ) ) ; 21 22 /* Read through to end of file. */ 23 while( ( ch = in2.read() ) != -1 ) 24 char c2 = (char) ch ; 25 26 /* Update character counts. */ 27 if( Character.isLetter( c2 ) ) 28 alpha[c2-a]++ ; 29 } catch( Exception e ) { 30 System.err.println("Could not count file.") ; 31 } catch( IOException e ) { 32 /* Handle input error. */ 33 System.err.println("Error reading line " 34 in2.getLineNumber()) ; 35 System.err.println(e.getMessage()) ; 36 } finally { 37 /* Make sure the stream is closed. */ 38 try { in2.close() ; } catch( Exception e ) { } 39 } 40 41 /* Print out results. */ 42 for( int i=0 ; i < alpha.length ; ) 43 System.out.println(""+(char)(a+i)+": " 44 +alpha[i]) ; 45 } 46 47 /* Takes a list of files to process. */ 48 public static void main(string[] args) { 49 for( int i=0 ; i < args.length ; i++ ) 50 count( args[i] ) ; 51 } 52 }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
