HW2 Development Guide

 

You should use this guide to help you to complete your assignment. You should follow it step by step, resolving errors before going on to the next step. Once you can follow this guide without errors, you should come up with more test cases to check your code. Being able to follow this development guide does not guarantee full credit.

    Copying Over Your Homework 1 Code

  1. Copy the code from the methods baseout, decout, hexout, newline, and writeline, which you wrote in hw1.java, into the corresponding methods in hw2.java. It is up to you to resolve any errors in your Homework 1 code that arise as a result of this copy. DO NOT change the method definitions of any methods in the Homework 2 code.
  2. Make sure the programs compile with make. The programs should not run correctly, but they should compile without errors at this point.

  3. A note on Debug Messages

  4. Throughout this assignment and all future assignments, we will be adding debug messages to the methods you write. Take a look at the main method to see the code that handles command line processing for the debug option -x. main uses getopt to check if the -x option was selected. If -x is found, debug_on is set to true. This then communicates to the rest of the program that debug messages should be printed. An example of some debug output is as follows:


    [*DEBUG: The length of the string displayed is 25]
    Please enter a string: Hello World!
    [*DEBUG: The length of the string just entered is 12]


    [*DEBUG: The length of the string displayed is 16]

    [*DEBUG: The length of the string displayed is 12]
    The string is: Hello World!

    [*DEBUG: The length of the string displayed is 15]
    Its length is 12

    OVERLOOK REMAINING OUTPUT

    Notice the extra output printed to the terminal when you run with the -x option as compared with running the executable without the -x. These are debug messages that give the programmer extra information while a program is running to help them diagnose any problems with the program's code. For this assignment, the extra output indicates how many characters were printed in writeline, and how many characters were read in getaline. From now on in CSE 12, all assignments will include the command line option -x, which will give useful debugging information. In this assignment, -x will help you determine if your program is printing or reading in the correct number of characters.

    We recommend writing the debug output early on so that you can use the information for debugging.

    In hw2.java, you will add debug output to methods writeline and getaline. You should make use of the pre-written debug messages located at the top of both files. Remember that fprintf to System.err.print are used to print debug messages. Note: there is no "correct" debug message order, as output will vary based on implementation For example, the 0x in hexout can be printed using fputc calls or a call to writeline. (Printing the 0x with writeline would cause a debug message to appear, while printing with fputc would not).


  5. Writing a Simple getaline

  6. Now that you've gotten all of your Homework 1 code situated, focus on the core logic behind the getaline method. Start with hw2.java's getaline method. You will need a simple loop reading characters one at a time from the standard input using fgetc until you read '\n', storing them in sequence in your message buffer so that they can be accessed once the method completes. Don't worry about special cases like EOF or buffer overflows yet, just get the simplest functionality working. Don't forget to null-terminate your message (store '\0' in your array after the last user entered character)!
  7. Test your getaline method by entering a short string and verifying that it is printed correctly back to you. We will use jdb to test your program. Since we are dealing with user inputs in this homework, we will need to open two different terminals. In the first terminal, type the following:

    $ jdb hw2

    This runs your program and creates a VM that we can connect to using jdb.

  8. Let's test your getaline with a simple input such as "abc". To do this, first open a second terminal. Then, paste the command that was given to you from the previous step. Make sure to replace the xx with the correct numbers.

    $ \jdb -attach 80[xx]
    ...
    main[1] stop in hw2.getaline
    ...
    main[1] run

  9. Now, switch to your first terminal, and enter "abc" where it prompts "Please enter a string: ". Switch back to your second terminal, and just type "next" to iterate through your getaline code. Verify that your loop only executes 3 times. Verify that you see the following messages in your first terminal.

    The string is: abc
    Its length is 3

    If you see a different output, debug your getaline and re-verify using jdb.

  10. Enhancing getaline, Implementing clrbuf

  11. Now enhance your getaline method by checking for the other termination conditions and adding a debug message displaying the number of characters read. Remember that when EOF is reached, getaline should stop immediately and return EOF. This will let us terminate the program properly with ^D (ctrl-d).

    Your final termination condition should check if the message buffer is full. You can do this using the maxlength parameter. Using the length member of the message array may be larger than the desired maximum storage requested, and therefore, using the length field is not ideal. Best is to check if the message length is smaller than maxlength, and if so, reset the maxlength parameter to message's length. If you do reach maxlength characters entered, getaline will need to clean up the standard input before it terminates using the clrbuf method. Implement that next.

  12. In hw2.java, write the clrbuf method. The clrbuf method's job is to remove all of the extra characters from standard input until a newline is reached. You should have a loop of reading characters from fgetc checking each one until a newline is found. Please note the possibility that all standing input may already be empty when calling clrbuf. If the parameter to clrbuf is newline, then stardard input is already clear and clrbuf has nothing more to do.

    Try inputting a string which is longer than MAXLENGTH characters to test the new code you added to getaline. To avoid having to type >80 characters each time you test the buffer overflow, you can change the MAXLENGTH constant to be 5 characters at the top of the file. Run your program using jdb to verify that your loop only executes 5 times (since we set the MAXLENGTH to 5). At the 5th iteration, your loop should stop. Remember, to use jdb you need to open two terminals, one where the program runs and one for jdb. After you press next enough times in jdb, you should see the following in your terminal that is running the program (not the one where you are using jdb):

    $ jdb hw2

    Please enter a string: awesome

    The string is: awes
    Its length is
    OVERLOOK REMAINING OUTPUT

  13. Enhancing writeline

  14. Now add code to writeline to print a debug message displaying the length of the string. If your writeline doesn't already, modify the method to return the length of the string displayed. Also, you should NOT print null character in your writeline. Verify your debug messages print using jdb. In the first terminal type the following:

    $ jdb hw2 -x

  15. Now, open up a new terminal and follow the procedure before, this time setting a breakpoint on writeline. To enable the debug option, enter the following:

    $ \jdb -attach 80[xx]
    ...
    main[1] stop in hw2.writeline
    ...
    main[1] run
    ...
    main[1] cont

    Note that "-x" enables the debug option. This will continue the process until writeline is reached again, printing you a message on the first terminal one at a time. Enter "Hola Mundo!" as the input.
    Your Java program should now display the following debug messages:

    $ jdb hw2 -x

    Please enter a string:
    [*DEBUG: The length of the string displayed is 25]
    Hola Mundo!
    [*DEBUG: The length of the string just entered is 4]

    The string is:
    [*DEBUG: The length of the string displayed is 16]
    Hola
    [*DEBUG: The length of the string displayed is 4]

    Its length is
    [*DEBUG: The length of the string displayed is 15]
    4


  16. Implementing Decin

  17. In hw2.java, write the decin method using the algorithm in class. Remember that you'll be obtaining a single character from System.in at a time. So you'll have to convert the character from ASCII and then store that value in a variable. For every subsequent character you'll have to convert it from ASCII then, add it to the previous value, multiplied by ten in order to shift the place the digits are in. Write the simple algorithm which deals with normal input (all digits, no overflow). As you did before, create your two screen setup with the same commands (use the -x option). Then, type following in the terminal you are using jdb:

    $ \jdb -attach 80[xx]
    main[1] stop in hw2.decin
    ...
    main[1] run
    ...
    main[1] next

    DO 'next' AS MANY TIMES AS NEEDED

    Verify that your loop only executes as many times as the number of digits in your input. (i.e. if your input was '123', your loop should execute 3 times.
  18. Add a check for non-digit input, and call the digiterror method if a non-digit character is found. You'll have to check for non-digit input by making sure it's within the values for ASCII code that digits are found in. Then add a check for overflow. This can be done by comparing the value of the current the sum stored, divided by 10, compared to the last sum stored.
  19. Now we are ready to test your program as a whole! If you verified everything using jdb, you should see the following once you run driver:

    $ ./driver

    Please enter a string: Hola Mundo!

    The string is: Hola
    Its length is 4

    Please enter a decimal number: 9876543211234567890
    digit 0 caused overflow!!!

    Please reenter number: 123cse
    digit c out of range!!!

    Please reenter number: 4

    Please enter a decimal base: 10

    OVERLOOK REMAINING OUTPUT


  20. Final Tests and Final Words

  21. At this point, you should test your assignment extensively by coming up with your own test cases.
  22. Remember to change the MAXLENGTH constant back to its original value (80)

  23. Compile and confirm that you can execute this simple test correctly. Test your solution extensively.

    $ make new
    $ ./driver

    Please enter a string: Hello world!

    The string is: Hello world!
    Its length is 12

    Please enter a decimal number: 123

    Please enter a decimal base: 10

    Number entered in base 10 is: 123
    And is decimal is: 123
    And in hexidecimal is: 0x000000000000007B
    Number entered multiplied by 8 is: 984
    And in hexidecimal is: 0x00000000000003D8

    OVERLOOK REMAINING OUTPUT


  24. Make sure that you have fully commented your program and added method and file headers. Make sure your style follows the Style Outline for CSE 12. And again, make sure your debug statements do not print out when the -x option is not used. Then turn in your program by following The Turnin Procedure.

    Note that, in order for your homework to be collected correctly, you must name your file hw2.java and the file must be located in a folder called hw2 in your home directory.