CS111 Programming Assignment 4


All problems in this assignment MUST be solved recursively

Instructor: Trish Cornez


Program 1

    Prompt the user for a sentence.
    Compute recursively the number of times the word "like" (lowercase only) appears in the sentence. Do not count "like" if the character '!' appears immedately before it.

    Executions:
    1. Input:    "I like apples and I like grapes."
      Output:2
    2. Input:    "I like apples and I like !grapes."
      Output:2
    3. Input:     "I !like apples and I !like grapes."
      Output:0



Program 2

    Prompt the user for a string.
    Compute recursively a new string where all letters of the alphabet are separated by a dash character.

    Executions:
    1. Input:    "grape 21"
      Output: "g-r-a-p-e 21"
    2. Input:    "ice cream"
      Output: "i-c-e c-r-e-a-m"
    3. Input:     "apple.""
      Output: "a-p-p-l-e."



Program 3

    Prompt the user for a string.
    Recursively count the number of pairs separated by a character. For example, The string "x2x" contains a pair of x's separated by the number 2.

    Executions:
    1. Input:    "x2x2
      Output:2
    2. Input:    "x2y2"
      Output:1
    3. Input:     "x2x2x2x2"
      Output:6



Program 4

    Declare an array containing a collection of random integers.
    Use recursion to determe if the array contains an even value immediately followed by an odd value.
    NOTE: Your recursive method should contain an index parameter that allows it to consider only the segment of the array that begins at this index.
    Executions:
    1. Input:    {1, 2, 3, 4, 5, 6, 7}
      Output: true
    2. Input:    {43, 55, 33, 11, 77, 1, 2}
      Output: false
    3. Input:     {2, 4, -2, -4, 5}
      Output: true



Program 5

    Declare an array containing a collection of random integers.
    Compute recursively the number of times an even integer appears in the array.
    NOTE: Your recursive method should contain an index parameter that allows it to consider only the segment of the array that begins at this index.
    Executions:
    1. Input:    {1, 2, 3, 4, 5, 6, 7}
      Output: 3
    2. Input:    {43, 55, 33, 11, 77, 1, 2}
      Output: 1
    3. Input:     {2, 4, -2, -4, 5}
      Output: 4



Program 6

    Prompt the user for a string.
    Compute recursively a new string in which all the lowercase vowels have been replaced by the letter 'z'.

    Executions:
    1. Input:    "Where are the flowers?"
      Output: "Whzrz zrz thz flzwzrs?"
    2. Input:    "http"
      Output: "http"
    3. Input:     "razor
      Output: "rzzzr"



Program 7

    Prompt the user for a sentence.
    Recursively clean the string of double spaces. After being cleaned, the sentence should have a single space separating each work.

    Executions:
    1. Input:    "I       love    pizza   !""
      Output: "I love pizza!
    2. Input:    Cupcakes       are    too       sweet.""
      Output: "Cupcakes are too sweet."
    3. Input:     The code          is       good.
      Output: "The code is good."



Program 8

    Prompt the user for a string containing the opening and closing HTML tags for bold, <b> and </b>.
    Compute recursively the length of the bold tag element, including the opening and closing bold tag.

    Executions:
    1. Input: <b>Welcome</b>
      Output:14
    2. Input: This is a <b>go</b> zone.
      Output:9
    3. Input: <b>Enter</b> here.
      Output:12



Program 9

      The following table shows the decimal values (base 10) and their equivalent binary (base 2), octal (base 8) and hexadecimal (base 16) values.
      This programming exercise requires that you compute a new value in a given base for a decimal value.
      You MUST NOT use a method provided by an existing Java class.
      Decimal
      Base 10
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      Binary
      Base 2
      00001
      00010
      00011
      00100
      00101
      00110
      00111
      01000
      01001
      01010
      01011
      01100
      01101
      01110
      01111
      10000
      10001
      10010
      10011
      10100
      Octal
      Base 8
      1
      2
      3
      4
      5
      6
      7
      10
      11
      12
      13
      14
      15
      16
      17
      20
      21
      22
      23
      24
      Hexadecimal
      Base 16
      1
      2
      3
      4
      5
      6
      7
      8
      9
      A
      B
      C
      D
      E
      F
      10
      11
      12
      13
      14
      Write a recursive method decimalConversion() that receives a decimal value and a base value (2, 8, or 16) and recursively convert the decimal number to the specified base value in String format.

      Write a test application to perform the following test conversions:
      1. decimalConversion(42, 2]) → 101010
      2. decimalConversion(42, 16]) → 2A
      3. decimalConversion(42, 8]) → 52
      4. decimalConversion(341, 2]) → 101010101
      5. decimalConversion(341, 8]) → 525
      6. decimalConversion(1, 8]) → 1
      7. decimalConversion(207, 16]) → CF
      8. decimalConversion(207, 2]) → 11001111