Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these Strings (or a reference to

Write a class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these Strings (or a reference to them, to be precise) and can perform limited calculations on the entire series.

The StringSet class has the following specification:

// an instance variable of type String[] // an int instance variable that indicates the number of String objects that the StringSet currently contains // a single no-argument constructor // a mutator that adds a String newStr to the StringSet object void add(String newStr) 
// an accessor that returns the number of String objects that have been added to this StringSet object int size() // an accessor that returns the total number of characters in all of the Strings that have been added to this StringSet object int numChars() // an accessor that returns the number of Strings in the StringSet object that have exactly len characters int countStrings(int len) Write a class StringSetTester that has a main method. It should ask the user for the number of Strings to add to a StringSet object. Afterward, use StringSet's size and numChars methods to print information about the collection of Strings entered. Also print the number of Strings that are exactly 5 and 8 characters long. 
Test Data: Number of strings: 4 String 1 = "Once upon a midnight dreary" String 2 = "Go Heels" String 3 = "Java" String 4 = "Have fun" 
. Hint: because Scanner's nextInt and nextLine process whitespace differently, you may want to use code similar to the following 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 // the next kybd.nextLine() will read user input 

Cheat-sheet for Writing a Class

Unless otherwise specified in an assignment

Follow the naming conventions (and we'll NEVER "specify otherwise" regarding this)

Until we get to nested (or inner) classes, have only one class per file, and that class should be public

Methods should be public (unless you're defining a private utility)

Avoid static (final static is fine--that's how Java does constants). If your development environment suggests "you can fix this problem by using static," it's leading you down the garden path; you have some other problem that needs to be fixed.

Every member should be public or private (i.e., don't use the default or protected access control specifiers)

The main method should be in a tester class, and probably be the only method in that class

All User input/output should be in the main method

All instance variables should be private

Don't use break except in a switch statement. Don't use continue at all.

Output is suppose to look like following:

How many strings will you enter? 2

Enter String1: this is so much fun

Enter String2: really?

Number of chars: 26

Number of strings: 2

Enter the string length to search for (or 0 to exit): 5

Number of strings of length 5 = 0

Enter the string length to search for (or 0 to exit): 7

Number of strings of length 7 = 1

Enter the string length to search for (or 0 to exit): 0

How many strings will you enter? 0

Number of chars: 0

Number of strings: 0

Enter the string length to search for (or 0 to exit): 1

Number of strings of length 1 = 0

Enter the string length to search for (or 0 to exit): 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

More Books

Students also viewed these Databases questions

Question

15-5 How will MIS help my career?

Answered: 1 week ago