In several labs weve written classes that stored and analyze
In several labs we\'ve written classes that stored and analyzed String objects. The Inheritance lesson had StringSet Revisited and Writing Classes Better had the original StringSet. Use one of these previously written classes in this program (it doesn\'t really matter which--they do the same thing).
Write a WidgetView application. create a class StringAnalysis.
This class has an event handler inner class extending WidgetViewActionEvent
StringSet sSet
JTextField inputStr
JLabel numStr
JLabel numChar
create a WidgetView object
create sSet
create a local variable JLabel prompt initialized to \"Enter a String\"
create inputStr with some number of columns
create a local variable JButton pushMe initialized to \"Push to include String\"
create numStr initialized to \"Number of Strings: 0\"
create numChar initalized to \"Number of Characters: 0\"
create an event handler object and add it as a listener to pushMe
add prompt, inputStr, pushMe, numStr and numChar to your WidgetView object.
Solution
---------------- Class StringSet ------------------------------------------------
public class StringSet{
int numberOfString;
int numberOfChar;
public StringSet() {
numberOfString = 0;
numberOfChar = 0;
}
public void addString(String str){
numberOfString = numberOfString+1;
numberOfChar = numberOfChar+str.length();
}
public int getNumberofString(){
return numberOfString;
}
public int getNumberOfChar(){
return numberOfChar;
}
}
------------ Class StringAnalysis ----------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class StringAnalysis {
StringSet sSet;
JTextField inputStr;
JLabel numStr;
JLabel numChar;
public StringAnalysis() {
JFrame f =new JFrame();//creating instance of JFrame
JLabel abc = new JLabel(\"Enter a String\");
JButton jb= new JButton(\"Push to include String\");
sSet = new StringSet();
inputStr = new JTextField(5);
numStr = new JLabel(\"Number of Strings: 0\");
numChar = new JLabel(\"Number of Characters: 0\");
jb.setBounds(20,100,200,40);
abc.setBounds(20,20,150,40);
inputStr.setBounds(180,20,150,40);
numStr.setBounds(20,200,200,40);
numChar.setBounds(20,300,200,40);
f.add(abc);
f.add(inputStr);
f.add(jb);
f.add(numStr);
f.add(numChar);
f.setSize(400,500);//400 width and 500 height
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sSet.addString(inputStr.getText());
inputStr.setText(\"\");
numStr.setText(\"Number of Strings: \"+sSet.getNumberofString());
numChar.setText(\"Number of Characters:\"+sSet.getNumberOfChar());
}
});
inputStr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sSet.addString(inputStr.getText());
inputStr.setText(\"\");
numStr.setText(\"Number of Strings: \"+sSet.getNumberofString());
numChar.setText(\"Number of Characters:\"+sSet.getNumberOfChar());
}
});
}
public static void main(String[] args) {
StringAnalysis s1 = new StringAnalysis();
}
}

