CharAtTester.java
import objectdraw.*;
import java.awt.*;
// A class that constructs a panel that can be used to experiment with the
// behavior of the "charAt" method of the String class.
public class CharAtTester extends StringInput implements StringOpTester {
// Field used to enter character position
private LabeledTextField position;
public CharAtTester() {
// Create and add "position" field;
position = new LabeledTextField("Character Position",7);
inputPanel.add(position);
}
// Perform "charAt" operation when requested
public String doIt() {
try {
return "" + sourceField.getText().charAt(Integer.parseInt(position.getText()));
} catch (Exception e) {
return "Value specified for position is invalid or out of range";
}
}
// Return the name of the method this class invokes
public String getChoiceLabel() {
return "charAt";
}
}
EndsWithTester.java
import objectdraw.*;
import java.awt.*;
// A class that constructs a panel that can be used to experiment with the
// behavior of the "endsWith" method of the String class.
public class EndsWithTester extends TwoStringInput implements StringOpTester {
// Perform "endsWith" operation when requested
public String doIt() {
return "" + sourceField.getText().endsWith(soughtString.getText());
}
// Return the name of the method this class invokes
public String getChoiceLabel() {
return "endsWith";
}
}
EqualsIgnoreTester.java
import objectdraw.*;
import java.awt.*;
// A class that constructs a panel that can be used to experiment with the
// behavior of the "equalsIgnoreCase" method of the String class.
public class EqualsIgnoreTester extends TwoStringInput implements StringOpTester {
public String doIt() {
return "" + sourceField.getText().equalsIgnoreCase(soughtString.getText());
}
// Return the name of the method this class invokes
public String getChoiceLabel() {
return "equalsIgnoreCase";
}
}
EqualsTester.java
import objectdraw.*;
import java.awt.*;
// A class that constructs a panel that can be used to experiment with the
// behavior of the "equals" method of the String class.
public class EqualsTester extends TwoStringInput implements StringOpTester {
public String doIt() {
return "" + sourceField.getText().equals(soughtString.getText());
}
// Return the name of the method this class invokes
public String getChoiceLabel() {
return "equals";
}
}
IndexOfTester.java
import objectdraw.*;
import java.awt.*;
// A class that constructs a panel that can be used to experiment with the
// behavior of the "indexOf" method of the String class.
public class IndexOfTester extends TwoStringInput implements StringOpTester {
// Field used to enter character position
protected LabeledTextField position;
public IndexOfTester() {
// Create and add "position" field;
position = new LabeledTextField(" Starting Position ",7);
inputPanel.add(position);
}
// Perform "indexOf" operation when requested
public String doIt() {
try {
return "" + sourceField.getText().indexOf( soughtString.getText(),
Integer.parseInt(position.getText()));
} catch (Exception e) {
return "Value specified for position is invalid or out of range";
}
}
// Return the name of the method this class invokes
public String getChoiceLabel() {
return "indexOf";
}
}
LabeledTextField.java
import objectdraw.*;
import java.awt.*;
// A class that combines a text field and an associated label as a single
// GUI component by putting them toghether in a panel
public class LabeledTextField extends Panel {
// Font used to make controls readable
private final Font bigFont = new Font("Times",Font.PLAIN,18);
// The label that belongs with the text field
private Label lab;
// The field itself
private TextField field;
public LabeledTextField( String text, int width) {
lab = new Label(text);
field = new TextField("",width);
field.setFont(bigFont);
lab.setFont(bigFont);
setLayout(new BorderLayout());
add(lab,"North");
add(field,"South");
}
public String getText() {
return field.getText();
}
public void setText(String text) {
field.setText(text);
}
}
LastIndexTester.java
import objectdraw.*;
import java.awt.*;
// A class that constructs a panel that can be used to experiment with the
// behavior of the "lastIndexOf" method of the String class.
public class LastIndexTester extends IndexOfTester {
// Perform "lastIndexOf" operation when requested
public String doIt() {
try {
return "" + sourceField.getText().lastIndexOf( soughtString.getText(),
Integer.parseInt(position.getText()));
} catch (Exception e) {
return "Value specified for position is invalid or out of range";
}
}
// Return the name of the method this class invokes
public String getChoiceLabel() {
return "lastIndexOf";
}
}
LengthTester.java
import objectdraw.*;
import java.awt.*;
// A class that constructs a panel that can be used to experiment with the
// behavior of the "length" method of the String class.
public class LengthTester extends StringInput implements StringOpTester {
// Perform "length" operation when requested
public String doIt() {
return "" + sourceField.getText().length();
}
// Return the name of the method this class invokes
public String getChoiceLabel() {
return "length";
}
}
StartsWithTester.java
import objectdraw.*;
import java.awt.*;
// A class that constructs a panel that can be used to experiment with the
// behavior of the "startsWith" method of the String class.
public class StartsWithTester extends TwoStringInput implements StringOpTester {
// Perform "startsWith" operation when requested
public String doIt() {
return "" + sourceField.getText().startsWith(soughtString.getText());
}
// Return the name of the method this class invokes
public String getChoiceLabel() {
return "startsWith";
}
}
StringDemos.java
import objectdraw.*;
import java.awt.*;
import java.awt.event.*;
public class StringDemos extends Controller implements ActionListener, ItemListener {
// Choice used to indicate which of the String methods user wants to
// experiment with
private StringOpChoice selector;
// Button used to invoke operation once input are filled in
private Button doTest;
// Panel used to hold choice and doTest button
private Panel controls = new SizeablePanel(600,30);
// Object of class appropriate for exercising method selected by user
private StringOpTester tester;
// Field used to display the result of an invocation
private LabeledTextField result;
// Construct user interface items when program starts
public void begin() {
// Construct the controls
controls.setLayout(new BorderLayout());
selector = new StringOpChoice();
selector.addItemListener(this);
controls.add( selector, "Center" );
doTest = new Button("Show Result");
doTest.setFont(new Font("Times",Font.PLAIN,18));
doTest.addActionListener(this);
controls.add(doTest,"West");
add(controls,"North");
// Add panel of controls for method initially displayed by "selector"
tester = selector.getSelectedTester();
add(tester.getPanel(),"Center");
// Add result display area
result = new LabeledTextField("Result:",50);
add(result,"South");
}
// When the button is clicked, perform the requested operation
public void actionPerformed(ActionEvent e) {
result.setText(tester.doIt());
}
// When a different String method is selected from the choice, replace the
// method tester panel displayed with the appropriate one.
public void itemStateChanged(ItemEvent e) {
StringOpTester newtester;
remove(tester.getPanel());
newtester = selector.getSelectedTester();
newtester.copyInputs(tester);
add(newtester.getPanel(),"Center");
tester = newtester;
validate();
}
}
StringInput.java
import objectdraw.*;
import java.awt.*;
// A class that creates a panel containg fields appropriate for testing
// String methods expecting no parameters (like length and toUpperCase).
public class StringInput
{
// The panel used to hold all the required components
protected Panel inputPanel = new Panel();
// A field in which the string to which the method should be applied can be entered
protected LabeledTextField sourceField;
public StringInput() {
// Create input field
sourceField = new LabeledTextField("Source String",45);
inputPanel.add( sourceField );
}
// Return the panel containing inputs created by this class
public Panel getPanel() {
return inputPanel;
}
public void copyInputs(StringOpTester old) {
sourceField.setText(old.getSource());
}
public String getSource() {
return sourceField.getText();
}
public void setSource( String text) {
sourceField.setText( text);
}
public String getSought() {
return "";
}
public void setSought( String text){
}
}
StringOpChoice.java
import objectdraw.*;
import java.awt.*;
// A specialized Choice menu built to enable one to select a String method
// to test
public class StringOpChoice extends Choice {
// An array used to represent the correspondence between menu items
// in the Choice and objects that perform the assoicated String operations.
private StringOpTester[] opTesters = new StringOpTester[15];
// number of items in the menu
private int testerCount = 0;
// Add entries for all the String methods supported to the menu
public StringOpChoice() {
setFont(new Font("Times",Font.PLAIN,18));
addTester( new CharAtTester() );
addTester( new EqualsTester() );
addTester( new EqualsIgnoreTester() );
addTester( new EndsWithTester() );
addTester( new IndexOfTester() );
addTester( new LastIndexTester() );
addTester( new LengthTester() );
addTester( new ToLowerTester() );
addTester( new ToUpperTester() );
addTester( new StartsWithTester() );
addTester( new SubStringTester() );
}
// Add a single String method tester to the Choice menu and tester array
private void addTester( StringOpTester opTester) {
addItem( opTester.getChoiceLabel() );
opTesters[ testerCount ] = opTester;
testerCount++;
}
// Return the object that applies the String method currently selected
public StringOpTester getSelectedTester() {
return opTesters[ getSelectedIndex() ];
}
}
StringOpTester.java
import java.awt.Panel;
public interface StringOpTester
{
//
String doIt();
String getChoiceLabel();
Panel getPanel();
public void copyInputs(StringOpTester old);
public String getSource();
public String getSought();
public void setSource(String text);
public void setSought(String text);
}
SubStringTester.java
import objectdraw.*;
import java.awt.*;
// A class that constructs a panel that can be used to experiment with the
// behavior of the "substring" method of the String class.
public class SubStringTester extends StringInput implements StringOpTester {
// Two text fields are provided to specify starting and ending positions
private LabeledTextField position, ending;
public SubStringTester() {
position = new LabeledTextField(" Starting Position ",7);
inputPanel.add(position);
ending = new LabeledTextField("Ending Position",7);
inputPanel.add(ending);
}
// Return the name of the method this class invokes
public String getChoiceLabel() {
return "substring";
}
// Perform "charAt" operation when requested
public String doIt() {
try {
return "" + sourceField.getText().substring(Integer.parseInt(position.getText()),
Integer.parseInt(ending.getText()) );
} catch (Exception e) {
return "The starting or ending position specified is invalid or out of range";
}
}
}
ToLowerTester.java
import objectdraw.*;
import java.awt.*;
// A class that constructs a panel that can be used to experiment with the
// behavior of the "toLowerCase" method of the String class.
public class ToLowerTester extends StringInput implements StringOpTester {
// Perform "toLowerCase" operation when requested
public String doIt() {
return "" + sourceField.getText().toLowerCase();
}
// Return the name of the method this class invokes
public String getChoiceLabel() {
return "toLowerCase";
}
}
ToUpperTester.java
import objectdraw.*;
import java.awt.*;
// A class that constructs a panel that can be used to experiment with the
// behavior of the "toUpperCase" method of the String class.
public class ToUpperTester extends StringInput implements StringOpTester {
// Perform "toUpperCase" operation when requested
public String doIt() {
return "" + sourceField.getText().toUpperCase();
}
// Return the name of the method this class invokes
public String getChoiceLabel() {
return "toUpperCase";
}
}
TwoStringInput.java
import objectdraw.*;
import java.awt.*;
// A class that creates a panel containg 2 fields appropriate for testing
// String methods expecting one String parameter (like equals).
public class TwoStringInput extends StringInput {
// Field for entry of parameter string
public LabeledTextField soughtString;
public TwoStringInput() {
soughtString = new LabeledTextField("Sought String",45);
inputPanel.add(soughtString);
}
public void copyInputs(StringOpTester old) {
super.copyInputs(old);
soughtString.setText(old.getSought());
}
public String getSought() {
return soughtString.getText();
}
public void setSought(String text) {
soughtString.setText( text );
}
}