jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exercise 9-2 K&B SCJP 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exercise 9-2 K&B SCJP 5" Watch "Exercise 9-2 K&B SCJP 5" New topic

Exercise 9-2 K&B SCJP 5

Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
Hello folks,

Just practicing away and I came across this exercise where you have to Synchronize on a StringBuffer object that initialised with a value of "A". You then have to print that out 100 times on a line, then increment the value. We're supposed to create 3 instances of this thread and start them one after the other so when main is run it should print out 3 lines, one with A's, one with B's and one with C's.

It asks us to use the StringBuffer object so that we don't have to create a new object as StringBuffers are mutable. It also says to increment the value of the StringBuffer to check the methods available to us in Chapter 6.

Now I've managed to do the exercise, but not in exactly the way they are describing, see below

Ok so I got the desired result, but I'm not satisfied that I haven't been able to acheive it in the way K&B describes! Any hints?
Ankit Garg

Joined: Aug 03, 2008
Posts: 9463

did you see that there is a setCharAt method in the StringBuffer class ...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
I gave it a shot. I made sb static so after running this:

I got the output desired:

be a well encapsulated person, don't expose your privates, unless you public void getWife()!
I agree. Here's the link: http://aspose.com/file-tools
subject: Exercise 9-2 K&B SCJP 5
It's not a secret anymore!