Skip to main content

Homework 3: Methods, Conditionals, Strings, and Arrays

In this assignment you will be working with methods, Strings, and Arrays.

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. Playing with Words!

Write a program called WordsWordsWords.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.

1.1 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 should only work for words that have 6 letters. If a word does not have 6 letters, the method should return false.

The method should work for non-alpha numeric characters. For example, the method should return true if the string is 456789.

1.2 doubloon

A word is said to be a “doubloon” if every letter that appears in the word appears exactly twice. Here are some example doubloons found in the dictionary:

Abba, Anna, appall, appearer, appeases, arraigning, beriberi, bilabial, boob, Caucasus, coco, Dada, deed, Emmett, Hannah, horseshoer, intestines, Isis, mama, Mimi, murmur, noon, Otto, papa, peep, reappear, redder, sees, Shanghaiings, Toto

Write a method called isDoubloon that takes a string and checks whether it is a doubloon. To ignore case, invoke the toLowerCase method before checking.

Your method should only work for words that have 6 letters. If a word does not have 6 letters, the method should return false. If the words contains a character that is not an English letter, i.e. a character that is not a-z or A-Z, the method should return false.

Hint: it might be helpful to create an array to keep track of how many times each character appears.

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

Hint: you might find it helpful to create a method that returns the isopsephy value of a word. When often refer to these types of methods as helper methods since they implement some functionality that you use over and over again.

1.3.1 avgIsopsephy

Write a method called avgIsopsephy that takes in 5 words and returns the average isopsephy value of the words. If any of the words do not contain 4 letters, the method should return -1.

1.3.2 isopsephySumEqual

Write a method called isopsephySumEqual that takes in 2 words and determines if the sums of the isopsephy of each word are equal to each other. The method should return a boolean. Again, if any of the words do not contain 4 letters, the method should return false.

1.4 Reverse String

Write a method called reverseString that takes in a string and returns the reverse string. If the word does not contain 10 letters, return an empty string.

Hint: String concatenation might be helpful here.

2. Arrays

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

2.1 abecedarian

2.1.1 Characters

Write a method called isAbecedarian that takes an array of characters and returns a boolean indicating whether the characters appear in alphabetical order.

Your method should only work for arrays that have 6 characters. If a word does not have 6 characters, the method should return false.

2.1.2 Integers

Write a method called isAbecedarian that takes an array of integers and returns a boolean indicating whether the integers are sorted in ascending order. Note: 1234 is an example of ascending order while 4321 is descending order.

Your method should only work for arrays that have 6 integers. If a word does not have 6 integers, the method should return false.

2.1.3 Doubles

Write a method called isAbecedarian that takes an array of double and returns a boolean indicating whether the doubles are sorted in descending order. Note: 1234 is an example of ascending order while 4321 is descending order.

Your method should only work for arrays that have 6 doubles. If a word does not have 6 doubles, the method should return false.

2.2 doubloon

Write a method called isDoubloon that takes an array of characters and checks whether each character in the array appears twice.

Your method should only work for arrays that have 6 letters. If a array does not have 6 characters, the method should return false. Your method should also work for only lower case letter (a-z). If the array contains a character that is not a lower case letter, the method should return false.

2.3 Isopsephy

2.3.1 avgIsopsephy

Write a method called avgIsopsephy that takes in an array of 5 words and returns the average isopsephy value of the words. If any of the words do not contain 4 letters, the method should return -1.

2.3.2 maxIsopsephy

Write a method called maxIsopsephy that takes in an array of 5 words and returns the word that has the largest isopsephy value. If any of the words do not contain 4 letters, the method should return an empty String ("").

2.3.3 minIsopsephy

Write a method called minIsopsephy that takes in an array of 5 words and returns the word that has the smallest isopsephy value. If any of the words do not contain 4 letters, the method should return an empty String ("").

2.3.4 isopsephySumEqual

Write a method called isopsephySumEqual that takes in an array of 2 words and determines if the sums of the isopsephy of each word are equal to each other. The method should return a boolean. Again, if any of the words do not contain 4 letters, the method should return false.

2.4 Reverse Array

2.4.1 Integers

Write a method called reverse that takes in an array of integers and returns an array where the order of the integers is reversed. If the word does not contain 10 integers, return an empty array.

2.4.1 Characters

Write a method called reverse that takes in an array of characters and returns an array where the order of the characters is reversed. If the array does not contain 10 characters, return an empty array.

2.4.1 Strings

Write a method called reverse that takes in an array of Strings and returns an array where the order of the strings is reversed. If the array does not contain 10 strings and if each string does not contain 5 characters, return an empty array.

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. ArrayMethods.java
  2. WordsWordsWords.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.