In this lab you'll go through a short program, identify the bugs and fix them.
There is no reference sheet this week, but the slides are available HERE
This "magic" will be explained in one of the next lectures!
git clone https://github.com/PurdueCS190/lab5.git ~/cs190lab5
Copy the command above and run it within the terminal. It will create a folder named cs190lab5
located within your home directory.
DrJava on the lab machines defaults to Java version 8, and the debugger isn't compatible with java jdk > 7 To get around this, you can use the shell script we've given below
- Open DrJava using the shell script we supply.
~/cs190lab5/open_drjava Transformer.java
- Right click on a line, select 'Toggle Breakpoint'
- Under the 'Debugger' menu option, enable 'Debug Mode'
- Hit Run
Compile and run Transformer.java
. You do this in DrJava, or by using the command line utilities javac
to compile and java
to run it.
Terminal example command to compile and run:
javac Transformer.java && java Transformer
You'll see some output that will look like this:
Original Input is: cs190 is sooooooo awesome!!!
==================
Capitalized Version:
=====================
cS190 iS sOOOOOOO aWESOME!!!
==================
Reversed Version:
==================
cs190 is sooooooo awesome!!!
The Transformer class will take a string in the constructor and then it will perform various transformations on that string.
There are currently two transformations implemented, but neither works correctly.
- The
capitalize()
method should return a string with each the first letter of each word in upper case. - The
reverse()
method should return a string with the characters in reverse order.
As we can see from the output above, the capitalized version isn't capitalized in the way we want. Let's figure out what's wrong.
The capitalize method will basically split the string into words separated by spaces. It will then take each word and capitalize the first character of it.
As we can see from the output above, this isn't what the capitalize method currently does. It currently capitalizes a bunch of characters, but not the first character.
Find the bug in here using whatever strategies you'd like, the DrJava debugger and print statements are two excellent options. Then implement a fix.
After this, you should have a working capitalize method, so let's look at reverse()
This reverse method uses a pretty standard reversal algorithm. It will start at the outside of the string and work its way inwards.
This is a VERY common entry level interview question, so take note!
For an example, lets use 'watch'. On the first iteration, we'll swap 'w' and 'h' and end up with hatcw. We'll continue in this way until the string is reversed.
The current implementation is incorrect.
This bug is most easily found using the DrJava debugger and a break point on line 63.
You can use the continue
button to continue the execution of the program to the next breakpoint.
As discussed in lecture, if a breakpoint is in a loop, then it will break every time it hits that line, so every time through the loop.
After you've found the bug, fix it and compile and run your program again.
After you've fixed both bugs, you should see an output that looks like this.
Original Input is: cs190 is sooooooo awesome!!!
==================
Capitalized Version:
=====================
Cs190 Is Sooooooo Awesome!!!
==================
Reversed Version:
==================
!!!emosewa ooooooos si 091sc
If this is what you see, then you're done! Look below at grading to see what you need to turn in.
Explain to a TA what debugging method you used.
Explain each bug to a TA and say how you fixed it.
If you find any bugs within the code or misspellings in the write-up, please tell the TA. Thanks!