





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Various questions related to recursion and file handling in java. It includes questions on recursive methods for finding the greatest common divisor, bubble sort recursive, finding the nth largest distinct value in an array, creating exception classes, summing digits in a string, reversing words in a string, copying selections to a file, and creating classes with properties, constructors, and interfaces. The document also includes a question on creating a campus class with an array of building objects and a clone method.
Typology: Exams
1 / 9
This page cannot be seen from the preview
Don't miss anything!






public static int method( int num1, int num2 ) { int result = num1; if (num2 > 0) { result = method(num2, num1 % num2); } return result; }
base case: num2 <= 0 recursive case: num2 > 0 recursive step: result = method(num2, num1 % num2);
result = method(66, 24) Remark: num1 = 24, num2 = 18 result = method(24, 18) 66%24 = 18 num1 = 18, num2 = 6 result = method(18, 6) 24%18 = 6 num1 = 6, num2 = 0 result = method(6, 0) num2 == 0 so it reaches the base case. result = num1 = 6 return result = 6 since 18%6 = 0, i.e. num2==0 & num1= return result = 6 return result = 6 return result = 6 return result = 6
public static void bubbleSortRecursive( Comparator list[] ) { bubbleSortRecursiveOuterLoop( list, list.length1 ); } private static void bubbleSortRecursiveOuterLoop( Comparator list[], int i ) { if ( i > 0 ) { bubbleSortRecursiveInnerLoop( list, 1, i ); bubbleSortRecursiveOuterLoop( list, i1 ); } } private static void bubbleSortRecursiveInnerLoop( Comparator list[], int i, int iUntil ) { int prev = i1; if ( i <= iUntil ) { if ( list[i].compare( list[prev], list[i] ) > 0 ) { // Swap if list[i] is greater list[next] Comparator temp = list[i]; list[i] = list[prev]; list[prev] = temp; } bubbleSortRecursiveInnerLoop( list, i+1, iUntil ); }
class NonNumericCharacterException extends Exception { private String errString; private char errChar; public NonNumericCharacterException() { super(); } public NonNumericCharacterException( String msg ) { super( msg ); } public NonNumericCharacterException( String eS, char eC ) { super(); errString = eS; errChar = eC; } }
public static int sumDigits( String value ) throws NonNumericCharacterException { int sum = 0; for ( int i = 0; i < value.length(); ++i ) { if ( Character.isDigit( value.charAt( i ) ) ) { sum += Integer.parseInt( value.substring( i, i+1 ) ); } else { //throw new NonNumericCharacterException(); //throw new NonNumericCharacterException( value + // " contains at least one nondigit character. " ); throw new NonNumericCharacterException( value, value.charAt( i ) ); } } return sum;
public static String reverseWords( String s ) { return reverseWordsRecursively( s, s.length()2, s.length()1 ); } private static String reverseWordsRecursively( String s, int leftIndex, int rightIndex ) { // A Base Case if ( rightIndex < 0 ) { return ""; } // A Base Case if ( leftIndex < 0 && s.charAt( rightIndex ) != ' ' ) { return s.substring( 0, rightIndex+1 ); } // A Recursive Case else if ( s.charAt( rightIndex ) == ' ' ) { return s.substring( rightIndex, rightIndex+1 ) + reverseWordsRecursively( s, rightIndex2, rightIndex1 ); } // A Base Case else if ( leftIndex == 0 ) { return s.substring( leftIndex, rightIndex+1 ); } // A Recursive Case else if ( s.charAt( leftIndex ) == ' ' ) { return s.substring( leftIndex+1, rightIndex+1 ) + reverseWordsRecursively( s, leftIndex1, leftIndex ); } // A Recursive Case else { return reverseWordsRecursively( s, leftIndex1, rightIndex ); } }
The base cases and recursive cases are shown in the code above. The recursive steps are inside the recursive cases. The program traverses the string from the highest index to the lowest index and extract a word from the string whenever it encounters a white space. It stops and extracts the last word when it has traversed to the start of the string. Each extracted word is added to the end of the return
public static void copySelectionsToFile( String inFile, String outFile, String value ) { BufferedReader in = null; BufferedWriter out = null; try { // Open Files in = new BufferedReader( new FileReader( inFile ) ); out = new BufferedWriter( new FileWriter( outFile ) ); // Copy Values from Read File to Write File String line = null; while ( (line = in.readLine()) != null ) { boolean isFound = false; for ( int i = 0; i < line.length() value.length() + 1; ++i ) { if ( !isFound ) { for ( int j = 0; j < value.length(); ++j ) { if ( line.charAt( i+j ) != value.charAt( j ) ) { j = value.length(); // break from the inner loop } else if ( j == value.length() 1 ) { isFound = true; } } } } if ( isFound ) { out.write( line ); out.newLine(); } } // Close Files in.close(); out.close(); } catch ( IOException e ) { System.out.println( "IOException Error(s)!" ); }
class Building implements Comparable, Comparator, Cloneable { // Properties private String name; private int stories; // Constructors public Building() { name = new String( "unknown" ); stories = 1; } public Building( String _name, int _stories ) { name = _name; stories = _stories; } // Clone Method public Object clone() { return new Building( new String( name ), stories ); } // Method from Comparable public int compareTo( Object obj ) { // comparisons based upon the stories return stories ((Building)obj).stories; } // Methods from Comparator public int compare( Object obj1, Object obj2 ) { // comparisons based upon the name return ((Building)obj1).name.compareTo( ((Building)obj2).name ); } public boolean equals( Object obj1, Object obj2 ) { // comparisons based upon the name return ((Building)obj1).name.compareTo( ((Building)obj2).name ) == 0; }