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
StringSetYet Again
In several labs we've written classes that stored and analyzed Stringobjects. The Inheritance lesson had StringSetRevisited 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 WidgetViewerapplication. 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 WidgetViewerobject
- create sSet
- create a local variable JLabel promptinitialized to "Enter a String"
- create inputStrwith some number of columns
- create a local variable JButton pushMeinitialized to "Push to include String"
- create numStrinitialized to "Number of Strings: 0"
- create numCharinitalized to "Number of Characters: 0"
- create an event handler object and add it as a listener to pushMe
- add prompt, inputStr, pushMe, numStr andnumCharto your WidgetViewobject.
Your event handler should add inputStr'scontents to sSetand set inputStrto "". It should update numStrand numCharlabels to contain sSet'scurrent information.
Note: the same event handler that handles button pushes can, with no modification, handle the key for a JTextField. For the adventurous, use inputStr'saddActionEventmethod to add your event handler. (You can use the same object you added to pushMe--you don't even have to create a new one). Then run your program, enter text in the inputStrfield, and press the enter key.
Note 2: In the Writing Classes lesson, the Anatomy of a Java Program item said "...we often want to use the business logic in multiple contexts. We might want a text-based application.... We might want to also use it in a graphical user interface (GUI), and we'll write things like that before the course is over." We've just made good on that promise. The StringSetclass (no matter which incarnation of StringSetyou choose to use) was written as part of a text application. We've now incorporated it into a GUI application with no changes at all. In fact, if our development environment was sophisticated enough, we wouldn't even recompile it. Our GUI app picked up the StringSetclass for free.
Grading Elements
- Program is a WidgetViewerprogram that displays a GUI with the specified widgets
- Program allows a user to enter Stringsand adds them to a StringSetobject
- Program uses a StringSetobject to maintain its Stringinformation
- Program uses a StringSetobject to provide information for display
- Program uses an event handler to detect user action and update the display
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