Question: Java Program Phase One Complete an implementation of the StringPile class, and write a main program that tests it. Your main program must be in

Java Program

Phase One

Complete an implementation of the StringPile class, and write a main program that tests it. Your main program must be in a class called PileTester. In particular, your tester should read from standard input (System.in) and write to standard output (System.out). It should simply call the four StringPile methods in order: first load, then get (and print) the shortest and longest strings, then print the pile in sorted order. So, if your input is

 the beautiful hippo

Then your output should be

 the beautiful beautiful hippo the

Note that there is NO OTHER OUTPUT.

Also note that all modern operating systems allow you to "redirect" standard input and output to files. So, if your input is in the file input.txt, then the command

 java PileTester < input.txt > output.txt

will run PileTester on the data in that file, and write the results to output.txt.

Phase Two

Style and documentation! When you are writing a Java program (or a program in most any language), your main audience is not the computer but other human beings. Your code must be easy for other programmers to read, digest, and comprehend. This is why variable names, whitespace, and other "fussy" things are so important. Indeed, most software companies have their own "style guides" so that everyone's code follows the same conventions, so everyone knows what they're looking at. For this class, I've made a simplified version of Google's Java style guidelines. There are a few items in the guidelines that don't apply to this project (like enum, exceptions, and a few other small things). But everything through section 5 should be pretty clear. Make sure you adhere to this guide!

Section 7 discusses something called "javadoc," which we will talk about later. For now, use "regular comments" and be sure that you document the purpose of: variables, methods, classes; you may also want to document any particularly tricky implementation details.

Phase Three

Finally: timing is everything! We talked a bit about how your choice of implementation will affect how quickly each of your methods run. So, now make a copy of your PileTester class, and call it PileTimer. Java (of course) gives us some tools that help us time the execution of parts of our applications. This page has a somewhat detailed discussion, but here's the deal: we can ask the JVM to record the time before we do something, then again after; if we subtract them, we get the execution time (or something very close) of the task. So we could write

 long startTime = System.nanoTime(); // ... do something interesting here ... long endTime = System.nanoTime(); System.out.println("That took " + (endTime - startTime)/1000000.0 + " milliseconds);

(Note that I divided by a million in floating point to get fractional milliseconds.)

Use this technique to time the four methods you call in PileTimer. These should be, y'know, pretty fast, though the exact timings will depend on your hardware and other factors. If you want to compare, here is the classfile for a class called FastStringPileyour code should work about as quickly as this code does.

Here is the stringpile

import java.util.Scanner;

import java.io.PrintStream;

import java.util.ArrayList;

import java.util.List;

import java.util.Collections;

public class StringPile

{

Scanner theScanner;

ArrayList arr = new ArrayList<>();

public Object printInOrder;

public void load(Scanner sc)

{

theScanner = sc;

while(theScanner.hasNextLine())

{

arr.add(theScanner.nextLine());

}

}

public String getShortest()

{

if(arr.size() > 0)

{

String shortest = arr.get(0);

int shortestLength = arr.get(0).length();

for(int i = 1; i < arr.size(); i++)

{

if(arr.get(i).length() < shortestLength)

{

shortestLength = arr.get(i).length();

shortest = arr.get(i);

}

}

return shortest;

}

return "its broken because the arraylist is empty";

}

public String getLongest()

{

if(arr.size() > 0)

{

String longest = arr.get(0);

int longestLength = arr.get(0).length();

for(int i = 1; i < arr.size(); i++)

{

if(arr.get(i).length() > longestLength)

{

longestLength = arr.get(i).length();

longest = arr.get(i);

}

}

return longest;

}

return "its broken because the arraylist is empty";

}

public void printInOrder(PrintStream ps)

{

Collections.sort(arr);

for(String s : arr)

{

ps.println(s);

}

}

}

here is the main.

import java.util.Scanner;

import java.util.ArrayList;

import java.util.List;

import java.util.Collections;

import java.io.File;

public class StringPileTest

{

public static void main(String[] args) throws Exception

{

Scanner s = new Scanner(new File("words.txt"));

StringPile strPile = new StringPile();

strPile.load(s);

System.out.println(strPile.getShortest());

System.out.println(strPile.getLongest());

System.out.println();

System.out.println("The input is the following");

System.out.println();

strPile.printInOrder(System.out);

}

}

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!