BBC (Brian's Boot Camp)

Not to be confused with the British Broadcasting Corporation, the BBC will focus on Brian's fast paced journey of the mastery of the Java programming language.

Wednesday, March 02, 2005

Assignment #3 Answer

This morning, I put chapter 12 and Assignment 3 behind me. At the rist of invoking the Sin of Pride, I'm pretty proud of myself. Huzzah indeed.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class StringSwapper extends JFrame implements ActionListener {
JTextField inputField = new JTextField("String", 5);
JButton pressMe = new JButton("Press Me");
JTextField outputField = new JTextField(5);

public StringSwapper() {
super("Convert a String");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
pressMe.addActionListener(this);

getContentPane().setLayout(flow);
getContentPane().add(inputField);
getContentPane().add(pressMe);
getContentPane().add(outputField);
pack();
setVisible(true);
}

public void actionPerformed(ActionEvent evt) {
String input = new String(inputField.getText());
int len = input.length();
char[] gnirts = new char[len];

for (int i = len - 1, c = 0; i > -1; i--, c++) {
gnirts[c] = input.charAt(i);
}

String output = new String(gnirts);
outputField.setText(output);
setTitle("Huzzah!");
}

public static void main(String[] args) {
StringSwapper ss = new StringSwapper();
}
}

2 Comments:

Blogger Sten said...

Excellent. Very nice, good job. In this assignment you proved you understand events, layout managers, and even some string manipulation.

If I may comment on a couple of best practices:

1) You're adding components directly to the frame (well, the content pane of the frame). This is totally fine and legal, but generally I think it's more common to add your components to a JPanel, and add that JPanel to your frame. Thusly:

JPanel p = new JPanel(); // default is flow layout

p.add(comp1);
p.add(comp2);

getContentPane().add(p);

or in jdk1.5 simply add(p);

This componentizes your layout a little, gives it a little more flexibility. That way, if I told you I wanted an exit button below all the other widgets, you could easily add it to the SOUTH portion of the frame.

2) pack(). I never liked pack(), but you're in the right to use it, this is more personal preference. I always prefer explicitly setting the size and location of the frame. I feel like pack() is non-deterministic. Again, nothing wrong with using it though.

3) Nice little bonus of changing the title after a string reverse.

4) Strings are a complicated, and exceptional class. You'll find all sorts of 'best practices' written about Strings and String performance.

You almost never use the String constructor with a String:
String str = new String("hi there");

You would shorten it to:

String str = "hi there";

5) Nice for loop. Again, what you have works well, but you could have also used the StringBuffer class so that your actionPerformed method could be reduced to one line:

outputField.setText(new StringBuffer(inputField.getText()).reverse().toString());

7:52 AM  
Blogger Brian T. Grant said...

Thank you! On your comments:

1) Noted. My decision to not use a JPanel followed more from the examples in the 21 Days book where he is only using them where there is need. But I can definitely see, through coding over time, it just makes sense to use JPanels all the time both for uniformity and flexibility.

2) I haven't had a lot of places to choose whether or not to use it, but I like pack() in principle. It appeals to me from the standpoint that its the language's own mechanism for deciding the size of a frame based on its contents. Like item 1, I can see where, through practice, you may want more consistent/precise control over something.

3) Heh, thanks. Obviously that was just in there for the fun of it.

4) I know when I first started out, I had declared both Strings as class variables. I know there was a reason why I moved them down to method variables, but I don't recall the reason.

5) I was really excited about that for loop when I figured it out, but I had a sense that there was probably some way to just take a String and reverse it.

When I decided that I'd measure the input string length and then count forward through one string and backward through the char[], thought of the for loop you illustrated here.

I have yet to encounter StringBuffers in the 21 Days book, though I've come across references to it in the API docs.

9:24 AM  

Post a Comment

<< Home