Skip to main content

Homework 4: Recursion

In this assignment you will be working with methods, Strings, and Arrays. You will be implementing the following methods recursively. For some of these methods, it might be easier to create another method that is called recursively. For example, recall how in lecture 9 we created a recursive method called public static int sumArray(String[] list, int index) to sum all of the values in an array.

Requirements:

For these methods, you are not allowed to use a loop since we did not cover loops yet. WARNING:Using a loop in a method will result in getting 0 points for that method!

Note, in the examples, * indicates user input.

Update 02/11: 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.

Update 02/12: 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.

1. Numbers

Write a program called NumbersRecursion.java that implements the following methods. Make sure to test the methods in the file’s main method.

1.1 Previous even

Write a method called previousEven that takes in an integer and prints out all the previous even numbers.

For example, previousEven(6) should print out 6 4 2, previousEven(9) should print out 8 6 4 2 . Make sure to put a space between the numbers and print all the numbers on one line.

It is ok if there is a space after the 2.

1.2 productOfPreviousOdd

Write a method called productOfPreviousOdd that takes in a number and returns the product of the previous odd numbers.

For example, productOfPreviousOdd(9) would return 945 because it would multiply 9 by 7 by 5 by 3 by 1.

1.3 sumOfPreviousN

Write a method called sumOfPreviousN that takes two integers and returns the sum of the first number minus multiples of the second number. For example, sumOfPreviousN(9, 4) would return 6 because it would add 5 + 1. sumOfPreviousN(20, 6) would return 24 because it would add 14 + 8 + 2.

2. Playing with Words!

Write a program called WordsRecursion.java that implements the following methods. Make sure to test the methods in the file’s main method.

Hint: You will want to use some of the String methods covered in Lab02 and Lab03.

2.1 removeLetter

Write a method called removeLetter that takes in a String and a letter and returns the String but will all instances of that letter removed.

For example, removeLetter("asdfghsassaaaae", 'a') should return sdfghssse.

2.2 removeLetters (extra credit)

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 removeLetter from 2.1

Update: 02/22: 8:30pm: Even if you dont implement this method, make sure to include a method stub. The method stub should be:

public static String removeLetters(String haystack, char[] needlesToRemove) {
  return "";
}

2.3 abecedarian

A word is said to be “abecedarian” if the letters in the word appear in alphabetical order. For example, the following are all six-letter English abecedarian words:

abdest, acknow, acorsy, adempt, adipsy, agnosy, befist, behint, beknow, bijoux, biopsy, cestuy, chintz, deflux, dehors, dehort, deinos, diluvy, dimpsy

Write a method called isAbecedarian that takes a String and returns a boolean indicating whether the word is abecedarian.

Your method work for strings of any length. If a string is empty or contains one letter, then it is sorted alphabetically

2.4 generateRandWord

Write a method called generateRandWord that takes in an integer and returns a word of that length of random letters from a-z. Each character should be chosen at random.

2.5 Isopsephy

Isopsephy is the practice of adding up the number values of the letters in a word to form a single number, in Hebrew we often refer to this as Gematria.

2.5.1 isopsephy

Write a method called isopsephy that takes in a word and returns the isopsephy value of the word.

2.5.2 avgIsopsephy

Write a method called avgIsopsephy that takes in an array of words and returns the average isopsephy value of the words.

2.6 Reverse String

Write a method called reverseString that takes in a string and returns the reverse string.

Hint: String concatenation might be helpful here.

3. Arrays

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

3.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.

3.1.2 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.

Updated 02/17 4:40pm we changed the default be true for this and 3.1.3

3.1.3 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.

3.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).

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 HW03 on Gradescope:

  1. NumbersRecursion.java
  2. WordsRecursion.java
  3. ArraysRecursion.java
  4. 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.