CSCI 201: Intro to Programming (Java)

Assignment 4.    Due: Tue, Oct 17

Please put your work in directory cs201 on your Linux account before 11:59pm of the day above.

  1. (12+13 points)   A palindrome is a string that is spelled the same way forward and backward. Examples of palindromes include "radar", "able was ere saw elba" (if blanks are ignored), and "a man a plan a canal panama". Write a Java application containing the following two methods:
    1. A non-recursive method testPalindrome1 that returns boolean value true is the string stored in a class variable s is a palindrome and false otherwise. All blanks in a string must be ignored for checking a string for being a palindrome, however the case of characters does matter.
    2. A recursive method testPalindrome2 with the same functionality as above.

    Put both methods into one class. Do not remove blanks from the input string in your code. The test string should be entered by the user. The result of checking it for being palindrome should be output to the screen.

    Name the project Hw4_1
    Solution

  2. (12+13 points)   Write a Java application that involves the following functions:
    1. A non-recursive method intPower1(int n, int pow). The first argument is a number that is to be raised to a (non-negative) power, and the second argument is the power. Do not use any methods of class Math.
    2. A recursive method intPower2(int n, int pow) with the same functionality as above.

    Put both methods into one class. The program should ask the user to input the numbers n and pow and output the results.

    Name the project Hw4_2
    Solution

  3. (25 points)   Design Java application for recursive computation of square roots by using the bisection method whose non-recursive pseudocode is outlined as follows:
    1. Initialize
       lower_bound = 0
       upper_bound = x + 1
       midpoint = upper_bound/2.0;
    
    2. do {
         if (midpoint2 < x)
           lower_bound = midpoint
         else
           upper_bound = midpoint
    
         midpoint = (lower_bound + upper_bound)/2.0;
      } while ( upper_bound - lower_bound > e )
    
    3. Return midpoint as the approx. value of the square root

    Your task is to redesign this method to be recursive. Your project has to have 2 methods: main() and sqrt(). The main() method should ask the user to enter a positive number x, then call your recursive sqrt() method and print the square root of x computed by that method. The sqrt() method should be called as sqrt(lower_bound, upper_bound). The desired precision e must be fixed and equal 0.000001. Declare it as a class variable. You can use non-recursive project designed in class as a reference.

    Name the project Hw4_3
    Solution

  4. (25 points)   Design a Java project with 2 methods: main() and recursive method printString(). The main() method should ask the user to input a string and then call printString() method. The printString() method should output the string forwards and backwards and put a space in between. For example, if the input string s="abc", then invocation of your printString(s) method should produce the following output: "abc cba". The method printString() must be inplemented as recursive and called only once from main() to print both: the original string and its reverse.
    Do not modify the user input string prior passing it to your printString() method.

    In your implementation of the printString() method you are not allowed to use any methods of Java API class String besides of chatAt() and length(). The output of the string and its reverse must be done on character-by-characted basis.

    Name the project Hw4_4
    Solution