Class OpenMessage
Object
OpenMessage
- All Implemented Interfaces:
Message
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionDecrypts a String using a Key.Encrypts a String using a Key.getBody()Gets the body of the messagegetFrom()Gets the "from" portion of the messageGets the subject of the messagegetTo()Gets the "to" portion of the messagestatic voidbooleanSearches for a particular term in a part of the message.booleansearchFrom(String term) Searches for a message by "from" portion of the message.booleansearchSubject(String term) Searches for a message by subject.booleanSearches for a message by "to" portion of the message.toString()Creates and returns a String that better represents our OpenMessage object.
-
Field Details
-
to
-
from
-
subject
-
body
-
-
Constructor Details
-
OpenMessage
-
-
Method Details
-
getBody
Gets the body of the message -
getTo
Gets the "to" portion of the message -
getSubject
Gets the subject of the message- Specified by:
getSubjectin interfaceMessage- Returns:
- String subject
-
getFrom
Gets the "from" portion of the message -
search
Searches for a particular term in a part of the message. Implemented in Lab 10. -
searchSubject
Searches for a message by subject. Implemented in Lab 10.- Specified by:
searchSubjectin interfaceMessage- Parameters:
term- term used to see if contained in subject- Returns:
- boolean result if term was in the subject
-
searchTo
Searches for a message by "to" portion of the message. Implemented in Lab 10. -
searchFrom
Searches for a message by "from" portion of the message. Implemented in Lab 10.- Specified by:
searchFromin interfaceMessage- Parameters:
term- term used to see if contained in "from" portion- Returns:
- boolean result if term was in the "from" portion
-
toString
Creates and returns a String that better represents our OpenMessage object. In Lab 10, you created the messageToString() method in MessageView that printed the "TO:", "FROM:", "SUBJECT:", and "BODY:" information when called in MessageApp. This works, but since you've now learned about classes and their toString() methods let's get rid of the messageToString() method in Message View and copy the contents here, in OpenMessage's toString(). Notice that you can now call getTo(), etc. directly, and don't need an object to call it on because we are inside that object! For example, instead of adding "TO: " + m.getTo() + "\n" to your String or StringBuilder, you can add "TO: " + getTo() + "\n". Everything else will remain the same. Keep in mind that to print this information, you will be calling the toString() method on an OpenMessage object, not on the MessageView object, view in MessageApp. -
encrypt
Encrypts a String using a Key. Lab 12's encryption will use the Caesar Cipher.Example output:
OpenMessage message1 = new OpenMessage("Cheshire Cat", "Quick Rabbit", "Important Meeting", "Don't be late for our very important date!"); Key key = new Key(1, 5); Key key2 = new Key(2, 4); System.out.println(message1.encrypt(message1.toString(), key)); System.out.println(message1.encrypt(message1.toString(), key2));- First line prints YT?%Hmjxmnwj%HfyKWTR?%Vznhp%WfggnyXZGOJHY?%Nrutwyfsy%RjjynslGTI^?%Its,y%gj%qfyj%ktw%tzw%{jw~%nrutwyfsy%ifyj&
- Second line prints XS>$Gliwlmvi$GexJVSQ>$Uymgo$VeffmxWYFNIGX>$Mqtsvxerx$QiixmrkFSH]>$Hsr+x$fi$pexi$jsv$syv$ziv}$mqtsvxerx$hexi%
- Parameters:
message- String message to encryptkey- Key used to encrypt the String- Returns:
- encrypted String
-
decrypt
Decrypts a String using a Key. Lab 12's decryption will reverse the Caesar Cipher.Example output:
OpenMessage message1 = new OpenMessage("Cheshire Cat", "Quick Rabbit", "Important Meeting", "Don't be late for our very important date!"); Key key1 = new Key(1, 4); //encrypting message String encryptedMessage = message1.encrypt(message1.toString(), key1); System.out.println(encryptedMessage); //decrypting message System.out.println(message1.decrypt(encryptedMessage, key1)); /* Prints: TO: Cheshire Cat FROM: Quick Rabbit SUBJECT: Important Meeting BODY: Don't be late for our very important date!Encrypted message prints XS>$Gliwlmvi$GexJVSQ>$Uymgo$VeffmxWYFNIGX>$Mqtsvxerx$QiixmrkFSH]>$Hsr+x$fi$pexi$jsv$syv$ziv}$mqtsvxerx$hexi%. You can re-write your encrypt() code backwards, or you can call encrypt() but change the parameters to stay DRY!- Parameters:
message- String message to decryptkey- Key used to decrypt the String- Returns:
- decrypted String
-
main
-