Skip to main content

Homework 5: Recursion & Arrays

In this assignment you will be getting more experience with recursion. You will implement the following methods using recursion.

Requirements:

Note, in the examples, * indicates user input.

WARNING: In this assignment you are NOT ALLOWED to import any packages. For example, you cannot use the builtin Arrays java class to reverse an array. Doing so will result in earning ZERO POINTS for a method that uses it.

IF YOUR CODE DOES NOT COMPILE ON GRADESCOPE, YOU WILL NOT RECEIVE ANY POINTS FROM THE AUTOGRADER. When you submit, make sure to check if the autograder compiles and if you test some of the public test cases.

Arrays

In a file called ArraysRecursion.java, write the following methods.

1 isPalindrome

A palindrome is a word (or an ordered collection) that is the same backwords. For example, madam and "a man, a plan, a canal – Panama are both palindromes.

1.1 isPalindrome characters

Write a method called isPalindrome that takes in an array of characters and returns true if the array is a palindrome. If the array is empty, the method should return true since that technically is a palindrome.

Note, do not solve this by reversing the array and then comparing the two arrays.

1.2 isPalindrome integers

Write a method called isPalindrome that takes in an array of integers and returns true if the array is a palindrome. If the array is empty, the method should return true.

Note, do not solve this by reversing the array and then comparing the two arrays.

2. Sorted

Write a method caled isSorted that takes in an array of integers and a boolean and determines if the list of numbers is sorted. If the boolean parameter is true, then the method should check if the numbers are sorted in ascending order (1234), if the parameter is false, then the method should check if the numbers are sorted in descending order (4321).

3. removeLetters

Write a method called removeLetters that takes in a String and an array of letters and returns the String but with all instances of the letters removed.

For example, removeLetters("asdfghsassaaaae", ['a','s']) should return dfghe.

Your method can use removeLetter from the last assignment.

4. countOccurrences

Write a recursive method called countOccurrences that takes in an array of integers and a target integer, and returns how many times the target integer appears in the array.

For example, countOccurrences([1, 2, 3, 2, 4, 2], 2) should return 3.

5. power

Write a recursive method called power that takes in an array of double and an integer n and returns a new array where every double \(x\) in the original array is \(x^n\). You are not allowed to use the power from the Math library or the ** operator.

Hint: you will likely want to create a recursive method power that takes in a double x and a integer n and returns \(x^n\).

README.txt

In a text file called README.txt answer the following questions:

  1. How much time did you spend on the homework
  2. What did you learn from this homework
  3. optional: What did you struggle with during this homework
  4. optional: any other feedback you would like to share

Dont forget: make sure to fill in the header in all of your java files.

Submitting

Submit the following files to the assignment called HW06 on Gradescope:

  1. ArraysRecursion.java
  2. README.txt

Make sure to name these files exactly what we specify here and name the methods exactly what we specify too. Otherwise, our autograders might not work and we might have to take points off.