Computer Science Homework Solutions

Visualizing Recursion - Java

It is interesting to watch recursion "in action." Modify the factorial method in Fig. 15.3 to print its local variable and recursive-call parameter. For each recursive call, display the outputs on a separate line, and add a level of indentation. Do your utmost to make the outputs clear, interesting and meaningful. Your goal here ...continues

Java - What does the following program do? MysteryClass

// Exercise 15.12 Solution: MysteryClass.java public class MysteryClass { public int mystery( int array2[], int size ) { if ( size == 1 ) return array2[ 0 ]; else return array2[ size - 1 ] + mystery( array2, size - 1 ); } // end method mystery } // end cl ...continues

Java - What does the following program do? - SomeClass.java

// Exercise 15.13 Solution: SomeClass.java public class SomeClass { public String someMethod( int array2[], int x, String output ) { if ( x < array2.length ) return String.format( "%s%d ", someMethod( array2, x + 1 ), array2[ x ] ); else ...continues

Palindromes - Blanks Ignored - Java

A palindrome is a string that will spell the same way forward and backward. Some examples of palindromes are “radar”, “able was I ere I saw elba” and (if blanks are ignored) “a man a plan a canal panama”. Write a recursive method testPalindrome that returns boolean value true if the string stored in the array is a palindrome ...continues

Recursive Binary Search Java

Modify Fig. 16.4 to use recursive method recursiveBinarySearch to perform a binary search of the array. The method should receive the search key, starting index and ending index as arguments. If the search key is found, returns its index in the array. If the search key is not found, return -1.

Formatted Output - Java

Find the error(s) in each of the following program segments. Show the corrected statement. a) System.out.printf( "%sn", 'Happy Birthday' ); b) System.out.printf( "%cn", "Hello" ); c) System.out.printf( "%cn", "This is a string" ); d) The following statement should print "Bon Voyage" with the double quotes: System.out. ...continues

Printing Dates and Times - Java

Write a program that prints dates and times in the following forms: GMT-05:00 04/30/04 09:55:09 AM GMT-05:00 April 30 2004 09:55:09 2004-04-30 day-of-the-month:30 2004-04-30 day-of-the-year:121 Fri Apr 30 09:55:09 GMT-05:00 2004

Rounding Numbers - Java

Write a program that prints the value 100.453627 rounded to the nearest digit, tenth, hundredth, thousandth and ten thousandth.

Random-number generation - Java

Write an application that uses random-number generation to create sentences. Use four arrays of strings called article, noun, verb and preposition. Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is picked, concatenate it to ...continues

Java - KeyboardWord

Write a program that inputs a word from the keyboard and determines the length of the word. Print the word using twice the length as the field width.

Browse