|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectSpy
public class Spy
The Spy
class contains methods to encrypt and decrypt messages,
as well as information about the secret code names of Spies that are already operating.
This class should have ONE instance variable (you must not add any other instance variables):
int
).
String
).String
has zero or more items stored
in this form: number:number:etc.
where number is the secret code number of a Spy. Each item is separated by a colon (with
no space before or after the colon). Also, there should not be a
colon at the beginning or end of the String
.
All the numbers in this String
must be unique (i.e., no duplicates). Also, the numbers
must be ordered (from left to right) in decreasing order: the highest code numbers come first.
Here is an example:
"9076:9075:1234"
If there are no Spies, then the String
is empty ("").
Constructor Summary | |
---|---|
Spy(int n)
Create a new Spy with code number n; Update the record of all existing code numbers according to the description above; If the specified code number already exists in the record, print a warning message to the user (hint: use System.out.println); You can assume that the user's input will be a 4-digit integer. |
Method Summary | |
---|---|
static String |
charToBinary(char c)
Return the binary representation of character c; You must do this by first converting the character into an integer ASCII value, and then converting that integer into an 8-bit binary value; Note that the return type must be a String of length 8; This means that the ASCII value of the given character
cannot be more than 255 -- if the character's ASCII value is greater than 255,
then return the empty String (""). |
static String |
decrypt(String encrypted,
int key)
Return the decrypted version of the encrypted String, using the given key; You should assume that the String was encrypted using Spy's encrypt method;
If the encrypted String is not valid (i.e., it is not composed of 8-bit binary Strings), then return
"cannot decrypt". |
static String |
encrypt(String msg,
int key)
Return the encrypted version of the msg, using the given key, according to this encryption algorithm. |
static int |
exclusiveOr(int i,
int j)
Return the result of applying the "exclusive-or" (XOR) operator to integers i and j; You can assume that the input integers will be either 1 or 0, representing true and false, respectively; This means that this method must return either 1 or 0, depending on the result of "i XOR j" . |
static String |
getAllCodeNumbers()
Return the code numbers of all existing Spies. |
int |
getCodeNumber()
Return the secret code number of this Spy. |
static boolean |
removeCodeNumber(int c)
Remove the specified code number c from the record of all existing code numbers; Return true once this is done;
if the specified code number does not exist in the record, return false . |
static String |
stretch(String binKey,
int length)
Return a new key that is the result of stretching the given binKey to be AT LEAST as long as the given length; The given binKey must be a String of bits! The algorithm for this "stretching" is as follows: divide the binary key into two even halves (you can assume that the key has an even number of bits), then XOR the corresponding bits of each half to produce a new String of bits, and append this new String of bits to the end of the key; If the resulting key is still not as long as the given length, then repeat the process until the key is at least as long as the given length. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Spy(int n)
Method Detail |
---|
public int getCodeNumber()
public static String getAllCodeNumbers()
public static boolean removeCodeNumber(int c)
true
once this is done;
if the specified code number does not exist in the record, return false
.
public static String charToBinary(char c)
String
of length 8; This means that the ASCII value of the given character
cannot be more than 255 -- if the character's ASCII value is greater than 255,
then return the empty String ("").
public static int exclusiveOr(int i, int j)
"i XOR j"
.
public static String stretch(String binKey, int length)
public static String encrypt(String msg, int key)
public static String decrypt(String encrypted, int key)
encrypt
method;
If the encrypted String is not valid (i.e., it is not composed of 8-bit binary Strings), then return
"cannot decrypt".
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |