Archive | November, 2012

Code ‘n …coffee?

30 Nov

Chai latte, actually, ’cause I just can’t take coffee these days.  Makes my heart race and my hair stand on end. At my age, that’s risky business better saved for the sudden joyous preponderance of short skirts in Springtime.

Just wanted to post an exercise I did today from ‘Big Java’ (first edition, purchased at Boomerang’s for $1 w00t!).  The code looks much longer than it is with the javadoc comments in it, but I am gabberflasted at what javadoc can do.  One quick command and I’ve got a very slick web site explaining everything there is to know about my wonderful code.

Here ’tis:

import java.text.NumberFormat;
public class Purse
{
    /**
	 * Constructs an empty purse.
	 */
	public Purse()
	{
	    // empty purse
		this.nickels = 0;
		this.dimes = 0;
		this.quarters = 0;
		this.total = 0.0;
	}

    /**
	 * Constructs a non- empty purse.
	 */
	public Purse(int nickels, int dimes, int quarters)
	{
	    // non-empty purse
		this.nickels = nickels;
		this.dimes = dimes;
		this.quarters = quarters;
	}

	/**
	 * Add nickels to the purse.
	 * @param count the number of nickels to add
	 */
	public void addNickels(int count)
	{
	   this.nickels += count; 
	}

	/**
	 * Add dimes to the purse.
	 * @param count the number of dimes to add
	 */
	public void addDimes(int count)
	{
	   this.dimes += count; 
	}

	/**
	 * Add quarters to the purse.
	 * @param count the number of dimes to add
	 */
	public void addQuarters(int count)
	{
	   this.quarters += count; 
	}

	/**
	 * Get the total value of the coins in the purse.
	 * @return the sum of all coin values
	 */
	public double getTotal()
	{
	   total = nickels * .05 +
	           dimes * .10 +
			   quarters * .25;

	   return total; 
	}

	public String getDollarTotal()
	{
	   total = nickels * .05 +
	           dimes * .10 +
			   quarters * .25;

	   dollarTotal = currency.format(total);
	   return dollarTotal; 
	}

    private int nickels;
	private int dimes;
	private int quarters;
	private double total;
	private String dollarTotal;
    NumberFormat currency =
	    NumberFormat.getCurrencyInstance();
}

The NumberFormat business is something I picked up from Beginning Programming With Java For Dummies. Don’t let the title fool you, it also works great for brilliant minds like ours. NumberFormat lets you take a double value and turn into I nicely formatted dollar amount String. Snazzy!

The exercise in the book provided the method names, but not the implementation. Nothing difficult, but always fun to figure it out, see it work and try to make it look nice. I had to write another class with a main method to test it out. That’s here:

public class TestPurse
{
    public static void main(String[] args)
	{
		// create new empty purse
	    Purse myPurse = new Purse();
		Purse hisPurse = new Purse(20, 10, 4);

		myPurse.addNickels(29);
		hisPurse.addNickels(27);
		myPurse.addDimes(13);
		hisPurse.addDimes(15);
		myPurse.addQuarters(2);
		hisPurse.addQuarters(34);

		// output totals
		System.out.println("My purse: " + myPurse.getDollarTotal());
		System.out.println("His purse: " + hisPurse.getDollarTotal());
    }
}

Fitnesse Trivia example code notes

30 Nov

The ‘trivia’ example for Fitnesse appears here in their on-line documentation for fixtures.  In the first example they forget to pass along the code for the Player class, so I thought I’d post mine here.  As I said before, it just a simple constructor class, but the other classes won’t compile and the test won’t run without it.

package myFixtures.trivia;

public class Player {
private String playerName;

    public Player(String playerName) {
        this.playerName = playerName;
    }
}

You might notice that the import fit.ColumnFixture; line is missing which you’ll see in the other classes in their example. That’s because I had to be fancy and use the Slim framework instead of Fit. Because of that, my fitnesse page code is different as well:

!define TEST_SYSTEM {slim}
!path /Users/KWHome/Dev/fitnesse/files/examples/

This example uses Slim instead of Fit.

"You can add players to the game, and you can 
 ask the game how many players are playing."

|myFixtures.trivia.AddRemovePlayerFixture|
|playerName  |addPlayer?  |countPlayers? |
|Al          |true        |1             |
|Bertha      |true        |2             |
|Stanley     |true        |3             |

Bear in mind these are just my first fumbling attempts at getting things into place and making sense of this. Snarky comments and kindly offers of ‘a better way’ are both welcome! And please say hello to the third member, Stanley, who wasn’t in the actual example code but insisted on tagging along anyways.

This post also taught me (after a furious bit of Googling) the value of the </pre> tag to format code in posts. Oh, and when you’re editing your Fitnesse table definitions, don’t forget to hit that handy ‘format’ button at the bottom before you hit ‘save’ Makes all the columns nice ‘n even.

public class HelloWorld

29 Nov

{

This blog is here as a repository for my thoughts, notes and musings as I learn the Java programming language.  Why am I learning Java?  Well, after 10+ years as a manual Software QA professional, it’s time for me to bit the bullet and figure out exactly what it is they’re doing on the other side of the aisle.  The more I know about what these developers are doing, the more capably and thoroughly I can test their product.  I’m also looking to apply this knowledge towards software test automation.  I also know enough about the subject to know that doing it well is extremely difficult, and virtually every angle I have approached it from in the past tells me, “This would so much easier if you knew Java!”

From where I’ve sat, the Java road has always looked far too steep.  The books too damn thick and the incantations just a bit too arcane.

The heck with that.  Time to tame the Beast!

Wish me luck…  ; )

-Kent

}

Type, Don’t Paste

29 Nov

My wife and I are both long time Ventures fans, to the point where “Walk, Don’t Run” was the primary theme song at our wedding.  We played the song together for years in a Boston band called The Projectiles in the mid 90’s which is how we met, but that’s not what this post is about.

This post would be a sure hit if they ever put out an album like, “Learn Java With The Ventures!”

Type, Don’t Paste.

Anyways, this is just an observation I wanted to make after working through the book “Beginning Programming With Java”, which I recommend whole-heartedly.  I did the book end to end and it’s given me enough of a foundation that all the really big books aren’t quite so scary any more.  Like many programming books, this one has all the code examples inside available for download.

Handy?

NO!

When sitting down to learn Java, type the damn Java in yourself.  I was speaking with my brother on this on Thanksgiving and he expressed exactly the same idea when he spoke of learning Python.  Great minds think alike and type their own code.  Check out his outsource software QA company Vericode when you have a moment.  He’s been in the business successfully since the early 90’s and is a wealth of knowledge and inspiration.  Thank you Scott!

So that’s all this post is about.  I use a bit of cut and paste when I’m playing with something like Fitnesse, where I’m trying to learn, in a way, the whole thing at a gulp.  When just sitting down doing Java from a book or something, though, type the damn code!

A Trivial Success With Fitnesse

29 Nov

Fitnesse is a self-contained wiki-based web front-end for Fit, which is a framework for test automation using ‘fixtures’, (relatively) simple (usually Java) applications which supply methods allowing Fit to tie in to and verify results from the application under test.  Now it actually uses a newer framework called ‘Slim’, but I digress.

So I took a break from my cold stone bench at the Java Monastery to look at this testing tool and hoped I might at least partially digest a few new buzz words while I’m at it.

TDD (gulp).  BDD (gag) (pounds table while clutching throat) (gulp).  I read on and peruse some of their examples.  It almost makes sense.

But, as it turns out, learning Fitness, which is quite popular in agile testing and dev environments, would be A WHOLE LOT EASIER if I just knew Java!

Fortunately, I *have* been studying a bit, and there are some kindly souls out there who have provided some very helpful tutorials.

One here, by Brett Schuchert, seemed just the thing:

Brett’s Fitnesse tutorials

I bumbled my way through it and ultimately got it to work, which at first glance I wasn’t convinced I could do.  Doing so taught me more about using Eclipse, more about how automation is done and…more about Java.  I even found a minor problem in Mr. Schuchert’s Fitnesse code which turned out to be quite old and no longer in adherence with the latest spec.  I questioned him about this by email while still working it out myself and he was extremely cordial and encouraging.  He encouraged me to go on with his later tutorials while stressing that they exist to demonstrate features and learning foremost, and should not be taken as example of ‘best practices’.  I have done so and am learning (and reinforcing) a lot of basic Java concepts by absorption in the process.  For this I can’t thank him enough!

I have also delved into some of the examples in the Fitnesse documentation itself, and am getting a lot more confidence with using both the Fit and Slim frameworks.  On the Java side, this is teaching me simple but critical things such as packaging, debugging (simple) code and testing classes without actually running them at the command line, the way I would a HelloWorld or other sort of console output program.

I’ll be detailing these and other exploits in future postings.  One tutorial in the Fitnesse documentation, for example, was missing an entire class which I ended up having to write up myself.  It was all of six lines or so, just a ‘constructor’ method to accept a String value, but I’d be lying if I said I didn’t feel a little adrenaline surge when it all suddenly worked!

All this and more.  Can’t wait, can you?

; ) -Kent