Sunday, November 30, 2008

Task: Use Log4J to print debug statements

I hope you have been printing debug messages as your code executes. I assume you must be using System.out.println(msg); to print the debug messages.

In this task we will replace use Log4J to print debug messages.

There is a much better way to print debug messages, because Log4J allows us to redirect the messages to any arbitrary destination, such as console, file, log server, etc. Log4J also allows us to associate levels such as 'error', 'warning', etc with these messages. Besides these there are a few other advantages to using Log4J as well.

Download Log4J and replace all instances of System.out.println() with Log4J debug messages.

Describe what you did, any problems that you may have faced and how you overcame them, in a blog post.

Leave a comment here after completing this task.

Task: Determine how much code your unit tests cover

So now, we have unit tests for all our code. But do the unit tests cover the code sufficiently. Do they they cover all paths in the programs logic? Do they cover all classes and methods?

Download EMMA, which is a free and open source code coverage tool, and find out how well your unit tests cover the tested code. Then try and increase your test coverage to 100%.

Describe what you did, any problems that you may have faced and how you overcame them, in a blog post.

Leave a comment here after completing this task.

Saturday, November 29, 2008

Task: Unit testing the Swing GUI

We created a Swing GUI in task 9, but did not unit test it. Going with the philosophy of test driven development, we should have first written unit tests for the GUI and then implemented it. Well we will bend the rules this one time and write the tests after creating the GUI.

Download FEST. This is a Swing unit testing library, that uses JUnit 4.0. Write unit tests for the GUI you created in task 9, using FEST.

Some Thoughts:
FEST depends on JUnit 4.0, which uses Java Annotations. If your Annotations knowl
Describe what you did, any problems that you may have faced and how you overcame them, in a blog post.

Describe what you did, any problems that you may have faced and how you overcame them, in a blog post.

Leave a comment here after completing this task.

Task: An exercise with Swing

Add a Swing GUI to task 8. The GUI should provide a means for the user to choose all the files they want to parse. We will print the word count as well as the line count of all the chosen files. So the user no longer needs to specify that.

The output should be printed in a read-only scrollable JTextArea on the GUI.

The design of the GUI is left as an exercise to the developer.

Coding Requirement:
Be sure that initialization of the GUI happens on the Event Dispatch Thread (EDT). Understand why it must happen on the EDT (what is the EDT?). At the moment you do not need to write any test code for the GUI.

Display a title for the application in the titlebar.

Be sure to print the debug log to the console as your program executes.

Some Thoughts:
Even though we are not testing the GUI, it is also part of the software we are building, and must be tested. Think about how you can test the GUI. We will work on this in the next exercise.

Describe what you did, any problems that you may have faced and how you overcame them, in a blog post.

Leave a comment here after completing this task.

Resources:
  1. The Swing Single Thread Rule - Cay Horstman
  2. Threads and Swing

Tuesday, November 11, 2008

Task: An exercise with threads

Refactor task 7 to allow the user to specify multiple input files. Parse each file and print either the word count or the line count as specified by the user.

Processing of each file should be done in a separate thread. Do not print the output from individual threads, because the output from different threads may overlap, making it difficult for the user to read. Instead each thread should give its output to the main thread as an object and the main thread should print it after all threads have finished processing their files.

Coding Requirements:
Be sure to print the debug log to the console as your program executes.

Some thoughts:
How will you do test driven development for multi-threaded code? Are there any standard paradigms?

Describe what you did, any problems that you may have faced and how you overcame them, in a blog post.

Leave a comment here after completing this task.

Task: An exercise with Collections

Refactor task 6 to add a feature. Over and above what we did in task 6, we also want to print the number of times each word occurs in the input text file. This detail should be printed such that the word count appears in the same order as the words are encountered in the input file. The program will also accept an ignore file containing words that should be ignored for the count.

Output for the text appearing above will be something similar to this:
Refactor - 1
task - 2
6 - 2
...

Coding Requirement:
In the code, you must use a Map to store the words as keys and the count as it's value. From this exercise onwards we will also print a debug log as the program code executes. What this means is, you should print something to the console at important points in the program logic. These statements should show the progression of the program. In case the program fails, the debug log might help you determine the source of error quickly without having to debug the code.

Monday, November 10, 2008

Task: First Steps Into Test Driven Development

From here on we be using TDD (Test Driven Development) methodology.

While writing test driven code, we first write a unit test for the code we are about to write, and then we write the actual code. The code is refactored till the test passes. This process continues until we have a working solution. Writing code thus, also means we write code piecemeal in an iterative manner (which is another good coding practice).

We will use JUnit 3.8 as the testing framework. You may need to download it if your IDE does not already have it.

For this task you will write a simple program which takes a text file as an argument along with another argument which specifies whether the user seeks a word count or a line count. based on what the user asked for, print either the word count or a line count of the text file.

Remember you have to do Test Driven Development, which means first write an empty (solution) class, then write a test to test one of the public methods of your class, then implement the method and refactor it till the test passes. Then write another test for another public method, and implement that method in the actual class... so on and so forth.

As a practice of the solution class is called FileStats, the test class will be called FileStatsTest. Also if we are writing a test method for a method called methodA(), then the nam of the test method will typically be testMethodA(). See documentation of JUnit 3.8 for more details.

All the test classes will reside in the test directory and will have the same package as the solution class.

Describe what you did, any problems that you may have faced and how you overcame them, in a blog post.

Leave a comment here after completing this task.

Resources:
  1. http://agilesoftwaredevelopment.com/videos/test-driven-development-basic-tutorial
  2. http://www.ic.sunysb.edu/stu/yosong/cse219/junit.html
  3. http://www.scribd.com/doc/524491/JUnit-Tutorial

Task: Refactor HelloWorld to use JSAP

In the HelloWorld example you wrote for Task 3, you wrote custom logic to parse the command line arguments.

JSAP is a very good open source library that does command line argument parsing. Download JSAP and use it to parse command line arguments in the HelloWorld exercise.

While refactoring code, try and use refactoring support in your IDE.

Describe what you did, any problems that you may have faced and how you overcame them, in a blog post.

Leave a comment here after completing this task.

Sunday, November 09, 2008

Check-in code into a source control system

Register an open source project on http://code.google.com and check in your workspace project into the SVN repository.

If you are using the Eclipse IDE, you may have to download a plugin for SVN.

Describe what you did, including any problems you may have faced, and how you overcame them, in a blog post.

leave a comment here after completing this task.

Task: Hello World

Everything starts with a HelloWorld, and so shall we :-)

Write a HelloWorld program that takes in command line parameters to determine what should be printed. The program usage is as follows:

HelloWorld -g -n
-g A compulsory parameter which denotes a greeting such as Hello, or Hi
-n
A compulsory parameter which denotes the name that will follow the greeting in the output

Running
HelloWorld -g Hello -n Parag

will print

"Hello Parag"

HelloWorld -g Hello
Should print the usage string

If any of the arguments are not provided, the program should abort with a usage string.

Describe what you did, including any problems you may have faced, and how you overcame them, in a blog post.

Leave a comment here after completing the task.

Task: Create a working environment for Java

Ensure that you have installed JDK 1.6 (latest update), and an IDE such as Eclipse or Netbeans.

In your IDE, create a new project called JavaInsights101_. Create an empty readme.txt file in the project. Create a 'src' directory, and a 'test' directory in the project.

Describe what you did, any problems that you may have faced and how you overcame them, in a blog post.

Leave a comment here after completing this task.

Task: Create a blog

To start with, all participants must create a blog which they can use as a regular learning journal, and a means for reflection, sharing, and collaboration.

If you need help with blogging, check out Freeman Murray's track on blogging for more information:

Also register and introduce yourself on this mailing list. We will use this list for asking questions and other discussions related to this track.

JavaInsights 101

Description:

This learning track is for developers who have completed at least one course in Core Java (or are familiar with basic principles of Java, like Syntax, compiling, and running Java programs) and would like to improve their understanding of the Java language and ecosystem.

Why Is This Track Needed?:

A typical Core Java course focuses on the Java syntax and simple exercises to understand Java. These are an important first step, however, usually due to lack of time such a course cannot equip the learner with deeper knowledge of how to develop good software in the Java ecosystem. The Java landscape is very huge and cannot be dealt with in a single course. When dealing with such magnitude, the best way to learn is self learn. This track's focus is to help developers by providing guidance as they get their feet wet, and give them the initial momentum for continuous self learning. The track will provide developers with:
  1. An understanding of coding and design best practices
  2. An exposure to the Java ecosystem of open source libraries and frameworks
  3. An understanding of how to unit test Java code
  4. An understanding of source control systems and how to use them
  5. An understanding of how to use development and code analysis tools
  6. A deeper understanding of some of the topics already learned in a regular Core Java course
Learning Methodology:

For this track we will use the concept of networked collaborative learning. Breifly, this means that everyone is a learner and a mentor. Learning happens by understanding, practicing, and participating. New media technologies (such as blogs, and podcasts) will be used for participation.

The JavaInsights101 track consists of 16 tasks. I recommend that you try and complete them in 16 weeks. Each task is explained in a separate blog post. After completing a task, please write a blog post (describing your solution, problems faced if any, and how you overcame them) on your own blog and leave a comment on the post describing the task (on this blog). Be sure that your comment contains the URL of your post describing your solution for the task.

Participants are encouraged to write blog posts regularly as well as read posts of other participants and leave (constructive) comments on their blogs.

There is a supporting Google Group for this track, which can be used for asking questions, and other discussions related to the track. Participants are also encouraged to go beyond this Google Group and ask questions on public Java forums, such as Java Ranch and Java.net . Participating on these forums will give you a wider perspective and will also help you connect with the Java developer community. For the benefit of other participants, whenever you ask a question on another forum, either write a blog post with the URL of the question, or write a quick mail on the mailing list, describing your question and it's URL. This will help other participants who have similar questions. Moreover if someone already knows the answer, they can post it on the question thread.

Registration:

To register for this track leave a comment on this post with your blog URL and go to Task 1. If you do not already have a blog, do Task1 first and then leave a comment on this post.

Fees:

This is a pay-it-forward course, which means you do not have to pay any fees monetarily. However, you are strongly urged to volunteer after completing this track by helping new participants, by either answering their questions on the forum, or by reviewing their code.

Frequently Asked Questions

Learning Tasks:
  1. Create a blog
  2. Create a working environment for Java
  3. Hello World
  4. Task: Check-in code into a source control system
  5. Refactor HelloWorld to use JSAP
  6. First Steps Into Test Driven Development
  7. An exercise with Collections
  8. An exercise with Threads
  9. An exercise with Swing
  10. Unit testing the Swing GUI
  11. Unit test code coverage
  12. Using Log4J for logging
  13. Code review
  14. Using ANT for compiling your project
  15. Ensuring code quality with FindBugs
  16. Your journey begins now