Skip to main content

Homework 4: Recursion

Due: 2024-10-07 23:59:00EDT

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.

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.

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.

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 an integer 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 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.3 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.4 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.4.1 isopsephy

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

2.4.2 avgIsopsephy

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

2.5 Reverse String

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

Hint: String concatenation might be helpful here.

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