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:
- 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
- Every method should contain a javdoc that briefly describes the method, lists the params, and the return type/value. Use the notation that we used in lectures.
- 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.
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.
Write a program called NumbersRecursion.java
that implements the following methods. Make sure to test the methods in the file’s main method.
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
.
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
.
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
.
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.
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
.
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
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.
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.
Write a method called isopsephy
that takes in a word and returns the isopsephy value of the word.
Write a method called avgIsopsephy
that takes in an array of words and returns the average isopsephy value of the words.
Write a method called reverseString
that takes in a string and returns the reverse string.
Hint: String concatenation might be helpful here.
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 HW03
on Gradescope:
NumbersRecursion.java
WordsRecursion.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.