HOME WORK 7 STRING PARSING; FILES. 1.JUNO DESIGN CHANGES IN GOING FROM HW6 TO HW7 One of the design changes in Juno is a change of Shell() method by class Shell. Class Shell in the hw7 made the Juno prompt to simulates the real Unix Shell prompt. The way class Shell works is , when the Juno.login() invoked it creates a new shell object with the user name and a current directory. Because of this change in the hw7 we are able to implment a cd command which simulated the real Unix cd commad to change the current directory. The other change in Juno is the relocation of the dispatch table. The dispatch table in the hw6 was implmented in the Juno.shell() method. But now in the hw7, the dispatch table is relocated in the class ShellCommand.interpreter() method. The interpreter method does a wonderful job in this hw 7. It separets command and arguments from comments. And command form argument. And finaly if dispatch the command. 2.APPLY AND PASSWORD: In order to improve the apply method so that it accepts only passwords that are at least six characters long and contain at least one character not in the alphabet. I used the String.length() method to test the length of the character, and if the password.length is less than 6 it throw an exception and prints an error message. The other change I made to the apply method is to test if the password has at least one charcter not in the alphabet in order to do that I used a Character.isLetter() method from the class Character and I tested every password character one by one. And if the password has at least one none character which is not in the alphabet it excute the rest of the the apply method. If it does not have al least one none character it throws an excepton. APPLY AND PASSWORD TEST RUN. Test run using: java Juno -e < padpw.script > mypass Welcome to juno help, apply, , exit juno login: apply username: eb password: short null juno login: apply username: eb password: onlyletters null juno login: apply username: eb password: use-a-dash juno login: eb password: use-a-dash juno:/users/eb> logout goodbye juno login: exit 3. CD SHELL COMMAND AND SHELL PROMPT: In order to use the " cd " command in the Juno. I wrote class CdCommand which extends the ShellCommand.java. And wirit the new CdCommand in the ShellCommand.fillTable() to create the cd object. The cd command wtih no arguments it return to user's home direcory. with argument it gose one leve up it the argument is " .. " and if the argument is " a directory " it gose to sub directory. In order to this solve this problem using the class CdCommand. I wrote the CdCommand so that it accept argument. And I tesed the argnents kind and according to the arguments I called the appropriate methods in the CdCommand.doIt method. For instance if there is no argument I called the sh.whoami.getHome() by casting to the Directory. Then I passed the argument in the sh.setDot() parameter and this changes the prompt to the users home directory. If the argument were a ".." I wrote a code wich calls the users home directory getHome() and I passed it to the sh.setDot() parameter to go one level up to the parent. If the argument were a directory; I wrote a code which check if the directory is in the ffilesystem if it is not it thorw an exception if it is a dirctory the sh.setDot() goes to the sub directory. The CdCommand.java thrwos JunoExcetion for handling errors. It has a try and catch statment implmented to its doItmethod for a diffent kind of exceptiosns. ForInstance NoSuchElementFoundException, NullPointerException and ClasCastException. TEST RUN FOR CDCOMMAND AND SHELL PROMPT. Test run using: java Juno -e < cd.script > mycd Welcome to juno help, apply, , exit juno login: apply username: eb password: good-password juno login: eb password: good-password juno:/users/eb> mkdir cs110 juno:/users/eb> newfile greeting hello juno:/users/eb> ls Directory listing for /users/eb/ Owner: eb, 2 FFiles greeting cs110/ juno:/users/eb> cd cs110 juno:/users/eb/cs110> mkdir hw7 juno:/users/eb/cs110> cd hw7 juno:/users/eb/cs110/hw7> cd juno:/users/eb> cd juno:/users/eb> cd no-such-place director does not exist juno:/users/eb> cd greeting not a directory juno:/users/eb> cd .. juno:/users> cd .. juno:> cd .. juno:> logout goodbye juno login: root password: shazam juno:> cd users juno:/users> cd eb juno:/users/eb> logout goodbye juno login: exit 4. DC SHELL COMMAND: The class DcCommand simulate the real Unix dc command for desk calculator with a diffrent sintax. The dc command does addition and multiplication only. The dc command needs three arguments in order to do its function. Inorder to write the dc command for the Juno I wrote the DcCommand.java which extends the ShellCommand.java. And I also wrote a new DcCommand() in the ShellCommand.fillTeble() to create the dc object. The first thing I impmented in the doIt method is a code which counts the number of arguments. And the code thorws as exception if the number of arguments sre not equals to three. the first argument for the 1st operand, the second one for operator and the third one for the 2nd operand. And I wote a code for the two operands which reads a tokens of strings and converts it the the Integer and assigne it to the int operand1 and int operand2. The code is implemented as follows. int operand = integer.pparseInt(args.nextToken()). But for the operator I assigned the character with out converting it. The doIt method test for the type of the poertor. I used if condition statment to check if the operator is equals to the opretor passed as argment and if the argument matches with the hard wired argument it excute the method of the of its match and prits out the answer as an out put. The doIt method throws as exception if the operator is not "+" or "*". It also handle an exceptions like NoSuchElementException, NumberOfFormatException(if it is not an int). TEST RUN FOR THE DCCOMMAND. Test run using: java Juno -e < dc.script > mydc Welcome to juno help, apply, , exit juno login: root password: shazam juno:> dc 3 + 4 7 juno:> dc 3 * 4 12 juno:> dc 3 wrong number of arguments juno:> dc 3 + wrong number of arguments juno:> dc 3 x 4 wrong operator:x use + or * juno:> dc x + 4 not an integer juno:> dc 3 ++ 4 wrong operator:++ use + or * juno:> logout goodbye juno login: exit 5.PUTFFILE AND GETFFILE The class putFFileCommand and The GetFFileCommand extends the ShellCommand.java. In oreder to use these commands I created a objects of PutFFileCommand and GetFFileCommand in the ShellCommand.fillTable(). The putFFile command opens the file Unixfile, and writes the contents of the TextFFile textffile to it. It also overwite the unix file if it is already there. In order to accomplish this task I wrote a code that take two arguments. If the number of arguments is less than three it throws an exception. if there are two tokens it assign the token to the ffile and the unix file. And set up a FileOutputStream for the named unix file. Then I retrive the named ffile from the hashtable, if it is not found in the hashtable I wrote it to throw an exception. If the named ffile is there it gets the contents field of the named ffile object. Then I used the for loop to change the bytes to character. Then the FileOutPutStream object write every character one by one. The GetFFileCommand opens the named unix file and copies its content into (as contents of) the TextFFile textffile, overwriting if it is already exist. Inoreder to accomplish this task I wrote a code in the doIt method that counts the number of arguments. If the number of arguments is less than two or greater than two it throws an exception. But if there are two tokens it assigns the tokens to unixfile and to the textffile. Then I created the FlleInPutStream object for the unixfile, the problem here the FileInputStrean returns bytes which is(ints) which are not suitable to form string. In order to solve this problem I used the String.valueOf()method from the String class to change the bytes(ints) to the string. There is also another approch to solve this problem. " to wrape it" BufferredReader inf = new BufferreadReader(new FileReader(unixfile)). Afterthat it writes the contents of the textfile to the unixfile. The doIt method handles a diffrent kind of exceptions. ForInstance FileNotFoundException, NullPointerException and IoException for in put and out files. TEST RUN FOR PUTFFILE AND GETFFILE. The putget.script tests the programe normal but when I try to get an out put it kept giving me ClassCastException. ars:/users/dgebru/cs110/hw7[2:08pm]> java Juno -e < putget.script > myputget something bad happened java.lang.ClassCastException: Directory mars:/users/dgebru/cs110/hw7[2:08pm]> Because of that I wrotenew script for the test run of put & getcommands. myscript is a new script for the putFFile and getFFile commads.The content of the myscript is as follows. root shazam getFFile read-only unixfile ls cat unixfile putFFile unixfile x-file getFFile x-file thisfile cat thisfile logout exit Using the script (above) I got this out put to test the getFFile and putFFile commands. Welcome to juno help, apply, , exit juno login: root password: shazam juno:> getFFile read-only unixfile juno:> ls Directory listing for / Owner: root, 2 FFiles users/ unixfile juno:> cat unixfile hello, world juno:> putFFile unixfile x-file juno:> getFFile x-file thisfile juno:> cat thisfile hello, world juno:> logout goodbye juno login: exit 6. HELP COMMAND: The help command did not accept any arguments before. It used to excute only the command to print the whole usage of ShellCommands. In order to make the help command prints the optiojal single help command I had to improve the doIt method so as it can accept the argument and invoke the helpstring of the argument. To accomplish this task in the help command I used a conditional staments. If there is no argument to print the whole helpstrings if there is argument to print the only command help string. I also wrote a code to throw juno exception if there is more than one token. What is does is it assign the first token to the String cmd, then if there are any left tokens it throw an exception. To print the optional single helpstring, I used the first token to retrive it from the hashtable of the shellcommand.lookup(). If the argument is a ShellCommand object I wrote a print statment to print the command name and its usage by calling the getHelp() and getArgstring() methods. And It works just fine. TEST RUN FOR HELP COMMAND. I got this out put using: java Juno -e < help.script> myhelp Welcome to juno help, apply, , exit juno login: root password: shazam juno:> help shell commands logout: log out usage: logout ls: list contents of current directory usage: ls putFFile: put a ffile into a unix ffile usage: putFFile FFile-name Unixfile-name dc: desk calculator usage: dc operand1- operator- operand2 cat: display contents of a TextFFile usage: cat ffilename newcmd: create a new Executable ffile usage: newcmd ffilename command-to-execute mkdir: create a subdirectory of the current directory usage: mkdir directory-name getFFile: put a unix-file into a FFile usage: getFFile Unixfile - textffile cd: change directory usage: cd directory-name help: display ShellCommands usage: help run: run an Executable ffile usage: run ffilename newfile: create a new TextFFile usage: newfile ffilename contents juno:> help help help:display ShellCommands usage: help juno:> help no-such-command ShellCommand no-such-command not found juno:> logout goodbye juno login: exit 8. JUNO.SCRIPT It test for all functionality of the ShellCommands i added in this home work. I wrote this script according to the changes I made in this homework. It test for all commands I implmented it also shows some exception handling. But the full exception handling are shown in each command test runs. the new juno.script gives this out put. Welcome to juno help, apply, , exit juno login: apply username: dawit password: xxx3xxx juno login: apply username: bad password: ssss # short password thorws exception null juno login: apply username: bad password: sssssss # password only characters it throw an exception null juno login: dawit password: xxx3xxx juno:/users/dawit> mkdir cs110 juno:/users/dawit> ls Directory listing for /users/dawit/ Owner: dawit, 1 FFile cs110/ juno:/users/dawit> cd cs110 juno:/users/dawit/cs110> mkdir hw7 juno:/users/dawit/cs110> cd hw7 juno:/users/dawit/cs110/hw7> cd .. juno:/users/dawit/cs110> cd juno:/users/dawit> dc 4 + 4 8 juno:/users/dawit> dc 8 * 8 64 juno:/users/dawit> dc 3 * # throws an exception wrong number of arguments juno:/users/dawit> newfile greeting helo, world I am in love with java juno:/users/dawit> cat greeting helo, world I am in love with java juno:/users/dawit> putFFile greeting unixfile juno:/users/dawit> getFFile unixfile crazyfile juno:/users/dawit> cat crazyfile helo, world I am in love with java juno:/users/dawit> help shell commands logout: log out usage: logout ls: list contents of current directory usage: ls putFFile: put a ffile into a unix ffile usage: putFFile FFile-name Unixfile-name dc: desk calculator usage: dc operand1- operator- operand2 cat: display contents of a TextFFile usage: cat ffilename newcmd: create a new Executable ffile usage: newcmd ffilename command-to-execute mkdir: create a subdirectory of the current directory usage: mkdir directory-name getFFile: put a unix-file into a FFile usage: getFFile Unixfile - textffile cd: change directory usage: cd directory-name help: display ShellCommands usage: help run: run an Executable ffile usage: run ffilename newfile: create a new TextFFile usage: newfile ffilename contents juno:/users/dawit> help cat cat:display contents of a TextFFile usage: cat ffilename juno:/users/dawit> logout goodbye juno login: exit 9.DC { DISCUSION HOW TO DETECT INTEGER OVERFLOW} doIt method detect the integer overflow by counting the tokens and if it is more than it throws ans exception. it also thwor an exception if it is not an integer. Welcome to juno help, apply, , exit juno login: root password: shazam juno:> dc 3 + 4 7 juno:> dc 3 * 4 12 juno:> dc 3 wrong number of arguments juno:> dc 3 + wrong number of arguments juno:> dc 3 x 4 wrong operator:x use + or * juno:> dc x + 4 not an integer juno:> dc 3 ++ 4 wrong operator:++ use + or * juno:> logout goodbye juno login: exit