Homework 6: Loops and Strings

Objectives

  • Write loops that contain nested if-else statements.
  • Build new strings from characters and substrings.

Note: CodingBat is a free site of live problems to build skill in Java and/or Python. It was created by Nick Parlante, who is Computer Science lecturer at Stanford. If you create an account, CodingBat will automatically save your progress online.

For this homework, you might find (on CodingBat's site) the Java String video 1 and video 2 useful.

Exercise 6.1   CodingBat String-1

The String-1 problems don't require any loops. They just use string methods like charAt, length, and substring. For this exercise, solve the following problems:

Complete these problems online first, until you get them 100% correct. Then download the BatString1.java source file and paste your code into the corresponding method stubs. Resolve any Checkstyle errors, and submit your final code to Autolab.

Exercise 6.2   CodingBat String-2

The String-2 problems are more a bit more challenging, because they each require iterating over a string (with one loop). For this exercise, solve the following problems:

Complete these problems online first, until you get them 100% correct. Then download the BatString2.java source file and paste your code into the corresponding method stubs. Resolve any Checkstyle errors, and submit your final code to Autolab.

Exercise 6.3   Magic 8-Ball

The Magic 8-Ball is a toy produced by Tyco Toys (now Mattel) that consists of a ball filled with a blue fluid. Suspended in the fluid is a 20-sided polyhedron with each side consisting of an equilateral triangle (an "icosahedron"). Each face has an answer to a yes/no question.

Download EightBall.java and read the main method to get a sense of what the program should do. Then implement the required helper methods:

  • public static boolean inputYesNo(Scanner in, String prompt)

    This method is simpler than the previous input methods you have written. Display the prompt (followed by "[yes/no]: "), read the next line of user input, and return true if it's "y" or "yes" ignoring case.

  • public static String getQuestion(Scanner in, String prompt)

    This method should keep prompting the user to enter a question until they enter a valid one. Question strings must be between 1 and 60 characters, and they must end with a question mark. If the question is invalid, display the applicable error message:

    • "Your question is blank"
    • "Your question is too long"
    • "Your question must end with a ?"

After completing the helper methods, be sure to test the program multiple times with different inputs. Here is an example of what it will look like:

Magic 8-Ball

Do you want to ask a question? [yes/no]: yes
What is your question? Will I win the lottery
Your question must end with a ?
What is your question? Will I win the lottery?

Question: Will I win the lottery?
  Answer: Signs point to yes.

Do you want to ask a question? [yes/no]: no
Goodbye!

Exercise 6.4   Nifty Spelling

Create a class NiftySpell.java that contains two methods described in Part A and B.

Part A

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

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

Write a method named abecedarian that takes a string and returns a boolean indicating whether the word is abecedarian. You may assume that the string will not be empty ("").

Part B

A palindrome is a word that reads the same both forward and backward, like "otto" and "palindromeemordnilap". Write a method named palindromethat takes a string and returns a boolean indicating whether the word is a palindrome. You may assume that the string will not be empty (""), and it will not have any spaces.

Exercise 6.5   Scrabble Words

Create a class Scrabble.java that contains two methods described in Part A + Part B

Part A

In Scrabble (a game by Hasbro), each player has a set of tiles with letters on them. The object of the game is to use those letters to spell words. The scoring system is complex, but longer words are usually worth more than shorter words.

Imagine you are given a set of tiles as a string, like "QUIJIBO", and you are given another string to test, like "JIB". Write a method called canSpellthat takes two strings (tiles and word) and checks whether the set of tiles can spell the word (return true if it can, false otherwise). You might have more than one tile with the same letter, but you can only use each tile once. Both strings will be in ALL CAPS.

Hint: Each time you "use" a letter, remove it from the tiles string. There is no method for removing letters, but you can use the substring method (twice) to copy the tiles before and after the letter.

Part B

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, bil- abial, boob, Caucasus, coco, Dada, deed, Emmett, Hannah, horse- shoer, intestines, Isis, mama, Mimi, murmur, noon, Otto, papa, peep, reappear, redder, sees, Shanghaiings, Toto

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

Hint: One way to solve this problem is to remove pairs of letters from the string (see hint for Part A). Another way is to use nested loops: for each letter, count the number of matching letters.

Notes

Your solution (for both methods) must NOT use arrays (Chapter 7) or collections (Chapter 13). Autolab will automatically reject submissions containing words like import, new, and [].

 

Submissions for all problems for HW6 should be made to https://autolab.cs.jmu.edu 

Back to Top