HW1 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. For this assignment, once you can follow this guide without errors, you are done with the coding aspect of the assignment. Remember, be sure to read ALL details on the main assignment page! Failure to do so on ANY assignment may result in major point deductions!

Make sure to fill in the Academic Integrity form here:

Academic Integrity Form

    Getting Familiar with Debugger

  1. Compile your program to start using JDB. JDB is a debugger for java programs. A debugger is a software or a program that is used to find bugs and errors in other programs in a programatic way. To use these debuggers at the command line you will need to compile your program first, so type

    % cd ~/hw1/java
    % make

    The make command uses the Makefile in your current directory to know what files to compile. In CSE 12, all programs are compiled with using make. The make commands for hw1 are:

    make - compiles program, does nothing if source files haven't changed since the last compile.
    make clean - removes .class and executable files from the current directory.
    make new - removes .class and executable files, then compiles all your source files.

  2. Note: The rest of this development guide assumes that you'll enter either make or make new when you are asked to compile your program.

  3. After resolving any compiler errors, compiling successfully causes a new executable files, driver, as well as a class file hw1.class to appear in the current directory. driver will run your Java program. To run the Java program with JDB, type

    % \jdb hw1

  4. Your output should match the following.

    Initializing jdb ...
    >

    There is a while loop and some arithmatic operations that occur in the loop. We will try to see what is the value of integer variable element in each iteration.

  5. So far we have only Initialized JDB and haven't run the program. To run the program we still need to perform other commands. So now in order for the execution of the program stop at a point of our choice in the program we need to tell JDB to do so. This will help us to print values of parameters and locals until that point. For us to follow along the while loop and check the variable element it is good to stop at main method. To do so type

    > stop in hw1.main

    This tells jdb to stop and wait for further commands once reaches the beginning of the main method

  6. Your output should match the following.

    Deferring breakpoint hw1.main.
    It will be set after the class is loaded.
    >

  7. Now we will be running the program using JDB. To do so type

    > run hw1

  8. Your output should match the following.

    run hw1
    Set uncaught java.lang.Throwable
    Set deferred uncaught java.lang.Throwable
    >
    VM Started: Set deferred breakpoint hw1.main

    Breakpoint hit: "thread=main", hw1.main(), line=104 bci=0
    104 int value = 0;

  9. Now in order to advance the execution and see each line of code you could type next and hit enter. This will execute the current line of code and show you the next line to be executed. You could also type list and hit enter to see few lines before and after the current line that is going to be executed by the program. Now continue execution of the program by typing next until you reach the first line in the while loop and stop. You should see the following.

    main[1] next
    >
    Step completed: "thread=main", hw1.main(), line=106 bci=4
    106 while (count < 3){

    main[1] next
    >
    Step completed: "thread=main", hw1.main(), line=108 bci=9
    108 element += ADD;

    Please note that the line with element += ADD has not been executed yet.

  10. Now type next and hit enter. Once you do this the value of variable element has been changed. To see what the new value is type the following in the command line

    > print element

    Note the value of element and repeat the steps to iterate the loop again. At each iteration note the new value of element. Do this unitl the loop exists. In order to exit from JDB type the following

    > exit

  11. For the remaining of the program you will be testing and running your program using JDB.

  12. Displaying Strings

  13. Following the algorithm discussed in class, write the code for the writeline method in your hw1.java file.
  14. After resolving any compiler errors, compiling successfully causes 2 new executable files, driver, to appear in the current directory. driver will run your Java program. To run the Java program in the debugger, type

    % \jdb hw1

  15. Now, verify that your output is correct using the debugger as described in the steps above. Set a breakpoint in your writeline method, and make sure you are looping through 3 characters only. You can verify this by typing the following in the debugger:

    > stop in hw1.writeline
    ...
    > run hw1
    ...
    main[1] next

    In each iteration of the loop, you should see the character getting printed. If your loop runs more than 3 times or prints some other characters than expected, make sure to debug your code and redo this step to verify.

  16. Write the code for the newline method in your hw1.java file. We will verify this method once we finish baseout and decout in the next section.
  17. Compile again with the make command.

  18. Displaying Numbers

  19. Write the baseout method in your hw1.java file. Concentrate on writing the algorithm given in class. For a few hints, refer to the method descriptions. To test baseout, write your decout method first. This method should delegate all the work to baseout. As an example, look at how the hexout method is implemented.
  20. Verify your baseout using jdb. Type the following and step through each iteration of the loop for '123'.

    % \jdb hw1
    ...
    > stop in hw1.baseout
    ...
    > run hw1
    ...
    main[1] next

    Verify that you iterate only 3 times through the loop. In each iteration, make sure you are printing the correct digit. If you have any errors in this step, debug your code and re-dop this step before moving to the C code.

  21. Now, handle the special case for the hexout method in baseout for both your hw1.java file. Any calls from hexout must result in a total of 16 digits after the 0x. Any unused digits should be padded with zeros. For example, if you look at the main method, you'll notice that the call to hexout only passes in 0xFEEDDAD. However, the desired output is 0x000000000FEEDDAD. This is a special case for the baseout method to handle. For hints on how to do this, look at the frequently asked questions: FAQ. Once you are done with this step, we are ready to test our program as a whole!
  22. Make sure your code is compiled with your latest code before progressing any further. If you are not sure, type:

    % make

  23. Now, first verify your Java program. If you followed each step correctly and verified using jdb, your output should match the following:

    % ./driver
    Hello World
    Ni Hao Shi Jie
    123
    0
    0x000000000FEEDDAD
    %

  24. Notice that "abc" was displayed before "def," even though the line of code from main that prints "abc" comes after the line that prints "def." This is because "abc" is displayed to System.err while "def" is displayed to System.out. This above demonstrates that System.err is not buffered, while System.out is. A buffer is like an array. Everything written to System.out gets put in this array and does not get displayed on the console until it is flushed. There are 5 situations for when the buffer gets flushed. Refer to your notes to recall these 5 situations. They may appear on your quiz.

  25. Run the hw1checkoff script by typing the following command in your own hw1 directory (~/../public/hw1/java/hw1checkoff). If the script shows no differences between your program and our public executable, your program's output is correct. In CSE 12, having correct output for the testcases is only worth part of your grade, called Correctness/Execution. You will also be graded on style and commenting. Make sure your code is commented throughout, and that the method headers are filled in completely. Also make sure not to use magic numbers and to have consistent indentation. You will be graded based on these standards.

    % ~/../public/hw1/java/hw1checkoff
    Compilation Passed!
    hw1.java has correct output to standard out!
    hw1.java has correct output to standard error!
    %


  26. Turnin Instructions

  27. Run the "turnin" command within your hw1/java directory.

    % turnin hw1

  28. Go to the lab to check-off your assignment with a tutor. You will need to demonstrate that your program works. The tutor will check your code, style and commenting. This is very important as it will help you resolve potential misunderstandings for future assignments! Do this if you value your grade!