public class StringManipulator
extends Object
Modifier and Type | Field and Description |
---|---|
static String |
OPTIONS
An contains the String "Cipher,Encode,Compress" for use in other classes
|
static String |
VOWELS
Contains the String "aeiouAEIOU" for use in stringCompress
|
Constructor and Description |
---|
StringManipulator() |
Modifier and Type | Method and Description |
---|---|
static String |
encrypt(String msg,
int shift)
Performs a simple Caesar Cipher on msg and returns the result.
|
static void |
main(String[] args)
By itself, this main simply tests the various methods in StringManipulator.java.
|
static String |
manipulate(String msg,
String type)
Called to determines which type of String manipulation to perform, and then returns the
manipulated String.
|
static String |
runLengthEncoding(String msg)
This method takes sequences of the same character in a
String and represents that sequence as the number of same characters in a row
followed by that character.
|
static String |
stringCompress(String msg)
Remove all
VOWELS from another String, unless the vowel starts a word. |
public static final String OPTIONS
public static final String VOWELS
public static String encrypt(String msg, int shift)
--- Implementation Hintsencrypt("A", 2)
return "C"encrypt("Ada", 3)
returns "Dgd"
(char)(myChar+shift)
(notice parens - they matter)msg
- the string to manipulateshift
- the amount of Caesar shift to do (can be in either direction)public static void main(String[] args)
Students! You must implement this main with some test methods, as this is a good practice to develop, that will greatly help later projects. In this case, we are going to be specific on the minimum things you should implement, but it will not hurt your grade doing additional tests (which you should!)
The output (System.out.println) for your test methods should contain at a minimum the following lines. We will test each group separately, so please add your own tests freely.Testing encrypt TEST: encrypt("A", 2) returns C - true TEST: encrypt("Ada", 3) returns Dgd - true Testing stringCompress TEST: stringCompress("You can be a good programmer, if you can read this.") returns Y cn b a gd prgrmmr, if y cn rd ths. - true TEST: stringCompress("HA AH HA AH") returns H AH H AH - true Testing runLengthEncoding TEST: runLengthEncoding("AAA") returns 3A - true TEST: runLengthEncoding("AAA BBB") returns 3A1 3B - trueHow to write a test method, here are a couple samples we wrote
System.out.println("\tTEST: encrypt(\"A\", 2) returns C - " + encrypt("A", 2).equals("C"));
System.out.println("\tTEST: runLengthEncoding(\"AAA\") returns 3A - " + runLengthEncoding("AAA").equals("3A"));
Suggestion: write one method, completely test it before moving onto the nextargs
- not used, but needed for correctness.public static String manipulate(String msg, String type)
cipher:N
Runs the encrypt(String, int)
method, using msg, and a shift defined by
any value after the colon (:). For example:
"cipher:2" would call encrypt(msg, 2).
encode
Runs the runLengthEncoding(String)
method using msg.
Compress
Runs the lossy compression method stringCompress(String)
method using msg.
--
Implementation suggestions include:msg
- the string to manipulatetype
- the type of manipulation to performpublic static String runLengthEncoding(String msg)
"BBBWWWWWB", becomes "3B5W1B". "AAAAA BBB", becomes "5A1 3B". (notice the space)This is a simple implementation, and does not encode any number characters. (read: you can ignore those cases, we won't test strings with numbers in them.
--- Implementation Suggestions:
This is a CHALLENGE goal. We will not grade the correctness of this method, just that you attempted it (so make sure it compiles).
msg
- A String representing the String to encode.public static String stringCompress(String msg)
VOWELS
from another String, unless the vowel starts a word.
Systematically removes all VOWELs from a line, and returns a new string.
This method does not ignore case and does not remove a character if it is the
'start' of a word after a space.
Example:
You can be a good programmer, if you can read this.Gets converted to
Y cn b a gd prgrmmr, if y cn rd ths.Implementation hints:
String.valueOf(char)
can be useful to convert a character to a string for containsVOWELS.contains(String)
Remember, contains requires Strings not characters to work. Character.isWhitespace(char)
Able to check any spacing.msg
- A String representing the String to check for matching characters.