In this assignment you will be getting more experience with recursion. You will implement the following methods using recursion.
Requirements:
- Every method you created should be tested in the class’s main method.
- Every java file should have a header. However, DO NOT PUT YOUR NAME IN YOUR HEADERS. On gradescope we grade your assignments anonymously so including your name in your Java file will defeat that purpose. However, please include your name in your README.txt
- Dont forget to add
public static
to every method signature. If a method does not have these, the autograder will fail and you wont receive any points for that method.
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.
In a file called ArraysRecursion.java
, write the following methods.
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.
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.
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.
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).
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.
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.
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\).
In a text file called README.txt
answer the following questions:
Dont forget: make sure to fill in the header in all of your java files.
Submit the following files to the assignment called HW06
on Gradescope:
ArraysRecursion.java
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.