Sunteți pe pagina 1din 4

Software Design I (CS 120), Fall 2011 Assignment 05 (20 points)

Due Friday, 28 October

Instructions: For this assignment, you will be using a customized version of the ThreeButtons class provided to create an interactive program. You will be writing the code for two classes, the usual Driver and a supplier class, Scrambler, that the Driver will use. The program will allow the user to enter a message in a text-box. Then, it will randomly encode the message, one letter at a time, and decode it as well. The point of this assignment is to learn to manipulate String input and output, and to convert between such objects and actual numbers, like integers; you will also get practice in writing helper classes and methods to go along with your main Driver class. The Driver code you download, when run, will produce an empty window, with buttons that do not do anything yet. That class also contains a Scrambler variable, but that variable is not used for anything, and the Scrambler class itself does nothing yet. You will add code to both these classes. The precise requirements are: (a) (3 points) You will add code to both classes so that when the Driver() constructor is run, it instantiates the Scrambler object; the Scrambler will then ll in the window with the various boxes and text labels, so that it looks like the example above (minus the text). Initially, the various boxes will be blank. The top box will allow the user to enter text by typing in it. (The other boxes only display text, and wont allow typing.) (b) (3 points) Each time the Start button is pressed, the program will copy the input text from the top input box into the second box. Initially, the two boxes will contain the same text; the number appearing in the bottom box will be set to zero (0). The code for doing this should be written in the Scrambler class. The Driver should do little more than call a method from that supplier class.

(c) (7 points) Each time the Encode button is pressed, the text is encoded as follows: (a) One character from the text string is chosen at random. (b) That character is changed by being increased either by 1 spot or 2 spots in the ASCII character ordering (where a is followed by b, 1 by 2, z by {, etc.). The decision whether to increase by 1 or 2 spots is also made at random. (c) The newly encoded string appears in the second box. (d) The change you made is recorded as a pair of numbers that can later be used to decode the text. This pair of numbers will appear on the left-hand side of the third box (with all the numbers that are already in that box appearing to the right). (e) The counter value in the bottom box increases by 1. For example, if you press Encode twice and the program chooses randomly to encode the 7th character by increasing it 1 position, and then the 5th character by increasing it 2 positions, the third box will contain the number series: 5 2 7 1, and the bottom box will contain 2, meaning that it will now take two steps to decode the message. (d) (7 points) Each time the Decode button is pressed, the text is decoded as follows: (a) The program will take the rst pair of numbers (x, y) from the key sequence that appears in the third box, and change the character in position x in the coded message so that it decreases by y places in the ASCII text representation. (b) The partially (or completely) decoded text appears in the second box. (c) The pair of numbers used to decode is removed from the left-hand side of the key in the third box. (d) The counter value in the bottom box decreases by 1. For example, if we encode our message as just described, so the key sequence is 5 2 7 1, hitting Decode once will change the 5th character of the message back to what it was before, after which the third box will only contain the numbers 7 1, and the bottom box will contain 1, meaning that there is one more step of decoding left to do. (e) (Bonus: 4 points) When writing this code, it is possible that you will get it to work as described above, but that your code will create null pointer exception errors when you try, for example, to press Encode before actually entering any input. This is acceptable, as is a runtime error when trying to Decode a message that is already decoded. However, if you want some bonus points, you can set your code so that no such errors appear, and all the buttons work without any runtime errors.

To help you make sure your code works properly, the download folder contains an executable program, Encryptor Sample.jar, that implements a full solution for comparison purposes. On most systems, you can double-click to run this program like a normal application. If this does not work on your system, try right-clicking and looking for an option to run it. If all else fails, Google how to run executable jar [YOUR OPERATING SYSTEM] and see what you can come up with. 2

Coding conventions: Each step of the program has a points total, given above. For full points, you should complete all those components, and observe the basic coding conventions as follows: Comments should appear at the start of any code-le you write or change, with your name, the date of your work, and a short explanation of what the code does. Each method should be preceded by a blank line, followed by at least one line of comments explaining what the method does. Methods should be private if possible, and public only if necessary. Class variables should be local to methods if possible, and should only appear as global variables if they appear in more than one method of the class. Unless absolutely necessary, global instance variables should be private. Code can be broken up by blank lines, but keep this to a low level. There should be no more than a single blank line separating pieces of code. Within a line of code, white space should not be too wide, for clarity. Code should be properly indented and separated into lines.

Notes about the String class: Most of what you need to deal with the String class is detailed in the text. There are two other methods that may be helpful to you, however. These are as follows: public String substring(int start) This method returns the substring of the starting String that begins at input position start. That is, if you have this code: String s = "Hello"; String sub = s.substring(2); then sub will be "llo", since String characters always start at position 0.

public String substring(int start, int end) This method returns the substring of the starting String that begins at input position start and ends at position (end 1), with length (end start). That is, if you have this code: String s = "Hello"; String sub = s.substring(1, 4); then sub will be "ell", since String characters always start at position 0.

Notes about the Scanner class: You will likely nd this class very useful, because it can take a String and turn it into various numbers. For instance if you run the following: String s = "2 5 15"; Scanner scan = new Scanner(s); int i = scan.nextInt(); int j = scan.nextInt(); int k = scan.nextInt(); then the integers will be i = 2, j = 5 and k = 15. It is important to note that this will not change the original String s. It is also important to note that if you run nextInt() again in the above code, you will get a runtime error, since once the Scanner object has returned all the values from the String, it is empty and cant use any of its nextWhatever() methods anymore. Along with the numeric methods covered in class, Scanner has one more that you may nd useful: public String nextLine() This method returns all of the remaining input String from the Scanner (i.e., anything that has not already been returned by some other method). That is, if you have this code: String s = "5 3 17 8" Scanner scan = new Scanner(s); System.out.println( scan.nextInt() ); System.out.println( scan.nextLine() ); then the printout you get will be as follows: 5 3 17 8 Note again that if you have run the nextLine() method as above, the Scanner is now empty and trying anything like nextInt() or nextLine() again after that point causes a runtime error.

S-ar putea să vă placă și