Question: JAVA PROGRAM The StringPile class To remind you, here's the activity from unit 2: Imagine a program that reads an arbitrary number of Strings in

JAVA PROGRAM

The StringPile class

To remind you, here's the activity from unit 2:

Imagine a program that reads an arbitrary number of Strings in from the input (where each line of input is treated as a String), then outputs: the shortest String (if multiple Strings have the shortest length, output the one that occurs first in the input), the longest String (again, if multiple Strings have the longest length, output the first-occurring one), and print the Strings in alphabetical order.

So the StringPile class is going to do most of that work. In particular, here's the class "skeleton":

 public class StringPile { public void load(Scanner sc); public String getShortest(); public String getLongest(); public void printInOrder(PrintStream ps); }

A couple notes: (a) the load() method takes a Scanner reference. In particular, you can pass a Scanner that "wraps" System.in (but you must set that up before you call load(), of course). Your method implementation should, of course, read from this Scanner; (b) similarly, printInOrder() will print to whatever PrintStream object its parameter refers to. In particular, you can pass in System.out.

Remember that you are not to implement the sort yourself, but to use the Java API.

Also note that you can detect the end of the input with a Scanner object by using the hasNextLine() method.

Your Task

So, you have three things to do.

Note you can do these in any order, or work on them simultaneously, as you wish: once you have the method signatures established, the implementation, application, and testing of them should be totally separate.

Implement the StringPile class. Notice that there is nothing here about implementation details. Do you need instance variables? Do you need to write any "helper" methods? What exactly should the load() method do? Another thing to think about: must every "get" method correspond to an instance variable?

Use your StringPile class to implement the exercise.

Create some test data and test your application.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!