Question
StringSet Yet Again In several labs we've written classes that stored and analyzed String objects. The Inheritance lesson had StringSet Revisited and Writing Classes Better
StringSet Yet Again
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).
The Program
Write a WidgetViewer application. Create a class StringAnalysis.
This class has an event handler inner that extends WidgetViewerActionEvent
it has instance variables
StringSet sSet
JTextField inputStr
JLabel numStr
JLabel numChar
In the constructor,
create a WidgetViewer 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.
Your event handler should add inputStr's contents to sSet and set inputStr to "". It should update numStr and numChar labels to contain sSet's current information.
(Here is the StringSet program that is supposed to be used):
import java.util.Scanner;
public class StringSet {
private String[] arr; private int count;
public StringSet() { arr = new String[200]; count = 0; }
void add(String newStr){ arr[count++] = newStr; }
int size(){ return count; }
int numChars(){ int total = 0; for(int i = 0; i < count; ++i){ total += arr[i].length(); } return total; }
int countStrings(int len){ int total = 0; for(int i = 0; i < count; ++i){ if(arr[i].length() == len) ++total; } return total; }
}
class StringSetTester {
public static void main(String[] args){ StringSet stringSet = new StringSet(); Scanner kybd = new Scanner(System.in); System.out.print("How many strings will you enter? "); int numStr = kybd.nextInt(); // stops after the number, leaves end of line or other whitespace kybd.nextLine(); // "eats" everything up to and including the next newline for(int i = 0; i < numStr; ++i){ stringSet.add(kybd.nextLine()); } System.out.println("Size: " + stringSet.size()); System.out.println("numChars: " + stringSet.numChars()); int len; while(true){ System.out.print("Enter a string length to search for (or 0 to exit): "); len = kybd.nextInt(); if(len == 0) break; System.out.println("Number of strings with length " + len + " are " + stringSet.countStrings(len)); } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started