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:
- 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.
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.
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 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
.
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
.
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 "";
}
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 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.
Updated 02/17 4:40pm we changed the default be true
for this and 3.1.3
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).
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
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.