Sie sind auf Seite 1von 7

Michael Bullinger

7/24/2014
Lab10b 10.2
Spec: Design and implement a program that creates an exception class called StringTooLongException,
designed to be thrown when a string is discovered that has too many characters in it. In the main driver
of the program, read strings from the user until the user enters DONE. If a string that has too many
characters (20) is entered, throw the exception. Allow the thrown exception to terminate the program.
Class: StringTooLongException
Will throw exception if character length is 20 or greater
Class Driver
ThinkTank
Main method throws StringTooLongException
Need scanner, will accept input (in a while loop)
As long as the input is not DONE (will ignore case)
Continue to accept input and add to an array list
if input > 20 characters, throw exception StringTooLongException
print arraylist












Michael Bullinger
7/24/2014
Design: StringTooLongException.java - definition
Extends Exception
Constructor
Super String too long exception
Sorry but your string was too long!






















Michael Bullinger
7/24/2014
Design: ThinkTank.java driver
Imports
Scanner : java.util.Scanner // for input
ArrayList: Java.util. ArrayList // for collecting input
Object Creation
New Scanner object scan
New ArrayList object phrases
Variable Declaration
String input =
Introduction
Print out summary of program
Welcome to Your Think Tank App! This program will have you input as many words, short
phrases, or basically anything you want! The only catch?? They -must- be under 20 characters
long! Seems challenging, right?
Process
While input is not equal to DONE (ignore case)
Prompt: Enter a short phrase
Input = scan.nextLine
If input length > 20
Throw StringTooLongException
Add input to phrases(array list)
When user enters done
Print out arraylist of the phrases input
Println for loop
for(int i = 0; i < n ; i++)
System.out.println( (i+1) + ". " + phrases.get( i )
End Program
Tell user that the program has been terminated
Println This program has ended. Thank you for listening to directions.







Michael Bullinger
7/24/2014
//****************************************************
// StringTooLongException.java by Michael Bullinger
// Will throw this exception and end the program.
// 7/24/2014
//****************************************************

@SuppressWarnings("serial")
public class StringTooLongException extends Exception
{
public StringTooLongException()
{
super("\n\nSorry, but your string was just too long."
+ "\nMaybe you could follow directions?"
+ "\nThe program specifically said less than 20
characters!\n");
}
}


















Michael Bullinger
7/24/2014
//*******************************************************
// ThinkTank.java by Michael Bullinger
// Spec: Design and implement a program that creates an
// exception class called StringTooLongException,
// designed to be thrown when a string is discovered that
// has too many characters in it. In the main driver of
// the program, read strings from the user until the user
// enters DONE. If a string that has too many characters
// (20) is entered, throw the exception. Allow the thrown
// exception to terminate the program. 7/24/2014
//********************************************************

import java.util.ArrayList;
import java.util.Scanner;

public class ThinkTank
{
public static void main (String[] args) throws StringTooLongException
{
// Object Creation and Variable Declaration
Scanner scan = new Scanner(System.in);
String input; ArrayList<String> phrases = new ArrayList<String>();

//-------------------------------------------------------------
// Introduction
//-------------------------------------------------------------
System.out.println("----------------------------------------\n"
+ "Welcome to Your Think Tank App!"
+ "\nThis program will have you input as many words, short
phrases,\n"
+ "or basically anything you want! The only catch?? \nThey -must-
be under"
+ " 20 characters long! Seems challenging, right?"
+ "\n----------------------------------------\n");

//-------------------------------------------------------------
// Process: Accept phrases and add to ArrayList
//-------------------------------------------------------------

input="";
while (!input.equalsIgnoreCase("DONE"))
{
System.out.print("Enter a short phrase (DONE to exit): ");
input = scan.nextLine();
if (input.length()>20) // throws exception if input is too long
throw new StringTooLongException();
if (!input.equalsIgnoreCase("done"))
phrases.add(input); // adds to arraylist
}
scan.close();

//----------------------------------------------------------------
// Output: Print the arraylist
//----------------------------------------------------------------

Michael Bullinger
7/24/2014
System.out.println("\nYour phrases:"
+ "\n----------------------------");
int n = phrases.size();
for(int i = 0; i < n ; i++)
{
System.out.println( (i+1) + ". " + phrases.get( i ) ); // prints arraylist
}


//-----------------------------------------------------------------
// Program termination
//-----------------------------------------------------------------

System.out.println("\n\n[Termination of Program!!!!!]\n"
+ "Well, that's basically it. If you can read this, then you can
follow directions!"
+ "\nCongratulations are being smarter than your average human
being!"
+ "\nThe program has finished executed. Good bye!");

}


}















Michael Bullinger
7/24/2014
RESULTS ERROR THROWN










RESULTS SUCCESSFUL ATTEMPT

Das könnte Ihnen auch gefallen