// CIS3023 Fall 2007 Exam II import java.util.StringTokenizer; import java.io.*; import javax.swing.JOptionPane; class ExamIISolns { public static void main( String [] args ) { // Test Q3 int [] testQ3 = new int[]{9,0,1,9,5,9,5,8,9,0,1,8,1}; System.out.println( "Q3 sample answer: " + fewestOccurrences( testQ3 ) ); System.out.println(); // Test Q4 String testQ4 = "exam 2 is the second exam in the course"; // This string will cause the method to throw the exception //String testQ4 = "exam 2 is an exam"; String [] resultQ4 = stringBreak( testQ4, "the" ); System.out.println( "Q4 sample answer:" ); for ( int i = 0; i < resultQ4.length; ++i ) { System.out.println( resultQ4[i] ); } System.out.println(); // Test Q5 String testQ5 = "exam 2 is the second exam in the course"; // This string will cause the method to throw the exception //String testQ5 = "exam 2 is an exam"; String [] resultQ5 = stringBreakRecursive( testQ5, "the" ); System.out.println( "Q5 sample answer:" ); for ( int i = 0; i < resultQ5.length; ++i ) { System.out.println( resultQ5[i] ); } System.out.println(); // Test Q8 String readFile = JOptionPane.showInputDialog( "Enter input file" ); String appendFile = JOptionPane.showInputDialog( "Enter appended file" ); appendFile( appendFile, readFile ); } // Q3 public static int fewestOccurrences( int a[] ) { int [] occ = new int[]{ 0,0,0,0,0, 0,0,0,0,0 }; for ( int i = 0; i < a.length; ++i ) ++occ[a[i]]; int few = a.length; int ans = 0; for ( int i = 0; i < occ.length; ++i ) { // System.out.println( "" + i + ": " + occ[i] ); // DEBUG if ( occ[i] > 0 ) { if ( few > occ[i] ) { few = occ[i]; ans = i; } } } return ans; } // Q5 // Note the solution uses only charAt and length methods. // To make a very simple solution, indexOf method can be used. public static String [] stringBreak( String s, String value ) { String [] tokens = new String[s.length()]; int count = 0; int indices[] = new int[]{0,0,0}; // start, middle, end // Loop until the end of the string length while ( indices[0] < s.length() ) { // Call helper method to find start, middle, and end indices indices = findIndices( indices[0], s, value ); // Keep strings (between start & middle and middle & end) for ( int i = 0; i < 2; ++i ) { if ( indices[i] < indices[i+1] ) { tokens[count] = ""; for ( int c = indices[i]; c < indices[i+1]; ++c ) { tokens[count] += s.charAt(c); } if ( i == 1 ) tokens[count] += s.charAt(indices[i+1]); if ( indices[1] == indices[2] ) tokens[count] += s.charAt(indices[1]); ++count; } } indices[0] = indices[2]+1; } // Throw exception if ( count < 1 ) { throw new StringNotFoundException(); } if ( count == 1 ) { if ( !tokens[0].equals( value ) ) { throw new StringNotFoundException(); } } // Return string array with correct size String [] ret = new String[count]; for ( int i = 0; i < count; ++i ) { ret[i] = tokens[i]; } return ret; } private static int [] findIndices( int start, String s, String value ) { int [] ret = new int[]{start,start,start}; for ( int i = start; i < s.length(); ++i ) { ret[1] = i; for ( int j = 0; j < value.length(); ++j ) { if ( i >= s.length() ) { ret[1] = start; ret[2] = i-1; return ret; } if ( s.charAt(i) == value.charAt(j) ) { if ( j == value.length()-1 ) { ret[2] = i; return ret; } else ++i; } else j = value.length(); } } ret[2] = s.length()-1; return ret; } // Q6 // Since StringTokenizer uses each char of string as a delimiter, // it does not use the whole string as a delimiter. // So the correct solution will be much harder. // Acceptable solution public static String [] stringBreakRecursive( String s, String value ) { if ( s.indexOf( value ) == -1 ) throw new StringNotFoundException(); StringTokenizer str = new StringTokenizer( s, value ); String [] tokens = new String[s.length()]; int count = stringBreakRecursive( str, tokens, 0 ); String [] ret = new String[count]; for ( int i = 0; i < count; ++i ) { ret[i] = tokens[i]; } return ret; } private static int stringBreakRecursive( StringTokenizer str, String [] tokens, int count ) { // Recursive case if ( str.hasMoreTokens() ) { // Recursive step tokens[count++] = str.nextToken(); return stringBreakRecursive( str, tokens, count ); } // Base case else { return count; } } // Q7 // Base case: no more tokens // Recursive case: has more tokens // Recursive step: stringBreakRecursive( str, tokens, count+1 ); // // Each call of stringBreakRecursive method will decrease number of // tokens by one. When no more tokens is true, then the base case is reached. // Q8 // with RandomAccessFile //* public static void appendFile( String filename1, String filename2 ) { try { // Open the files RandomAccessFile rd = new RandomAccessFile( filename2, "r" ); RandomAccessFile wr = new RandomAccessFile( filename1, "rw" ); // Move to the end of the file to be appended while ( wr.read() != -1 ) { wr.read(); } // Append the file with the data from the read file int v = rd.read(); while ( v != -1 ) { wr.write( v ); v = rd.read(); } // Close the files rd.close(); wr.close(); } catch ( IOException e ) {} } //*/ // Q8 /* // with BufferedReader and BufferedWriter // REMARK: FILE SHOULD NOT BE OPENED FOR READ AND WRITE AT THE SAME TIME! public static void appendFile( String filename1, String filename2 ) { // Some method calls may throw exception, // so try/catch block is used to catch the exceptions that may occurred. try { // Keep the data from the file to be appended BufferedReader rd = new BufferedReader( new FileReader(filename1) ); String str = ""; String s = rd.readLine(); while ( s != null ) { str += s + "\n"; s = rd.readLine(); } rd.close(); // Open file for write and write the read data to it BufferedWriter wr = new BufferedWriter( new FileWriter(filename1) ); wr.write( str, 0, str.length() ); // Open input file, read, and write its data to the file for write rd = new BufferedReader( new FileReader(filename2) ); int v = rd.read(); while ( v != -1 ) { wr.write( v ); v = rd.read(); } // Close the files rd.close(); wr.close(); } catch ( IOException e ) {} } //*/ } // Q4 class StringNotFoundException extends RuntimeException { // Default ctor public StringNotFoundException() { super(); } // Nondefault ctor public StringNotFoundException( String msg ) { super(msg); } }