



|
|
My Intro to Object-Oriented Programming
|
Programming Challenge Archive
Programming Assignment # 5
OfficialDictionaryTestApp.java
|
|
|
In this assignment, CG provided us with test programs to run classes we refined.
The classes were
Word.java
and
Dictionary.java
.
The test programs were
OfficialWordTestApp.java
and
OfficialDictionaryTestApp.java
.
JavaApplication - Programming Assignment # 5 --
OfficialDictionaryTestApp.java
// General Information
// ---------------------------------------------------
// File: DictionaryTestApp.java
// Type: java application file
// Date: Tue Dec 05, 2000
// Name: Jeffrey R. Norkoli
// Line: Test program for the Dictionary class
// Application Description
// ---------------------------------------------------
/*
Official test program for the Dictionary class
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import white.words.*;
// Application Class
// ---------------------------------------------------
class OfficialDictionaryTestApp
{
static public void main (String args[])
{
// Create a little dictionary and display it.
// Internally create and display two words.
IO.println();
IO.println("--> INTERNALLY CREATE 2 WORDS.");
String as[] = new String[1];
as[0] = "mind";
Word a = new Word("mind",as);
String bs[] = new String[2];
bs[0] = "de";
bs[1] = "sign";
Word b = new Word("design",bs);
a.display();
b.display();
// Internally create and display a dictionary
IO.println();
IO.println("--> INTERNALLY CREATE DICTIONARY.");
Dictionary small = new Dictionary("Small");
small.add(a);
small.add(b);
small.display();
// Create and display a second dictionary, from exte+
rnal
// sources
IO.println();
IO.print("--> CREATE A DICTIONARY EXTERNALLY");
Dictionary pldehp = new Dictionary("pldehp");
pldehp.add("wf1.data");
pldehp.add("wf2.data");
pldehp.add("wf3.data");
pldehp.display();
// Search for languages
IO.println();
IO.println("SEARCHING FOR LANGUAGES...");
String[] claysyllables = new String[1];
as[0] = "clay";
Word clay = new Word("clay",as);
String smalltalksyllables[] = new String[2];
smalltalksyllables[0] = "small";
smalltalksyllables[1] = "talk";
Word smalltalk = new Word("smalltalk",smalltalksy+
llables);
if ( pldehp.search(clay) )
{
IO.println("CLAY is in the dictionary.");
}
else
{
IO.println("CLAY is NOT in the dictionary.");+
}
if ( pldehp.search(smalltalk) )
{
IO.println("SMALLTALK is in the dictionary.")+
;
}
else
{
IO.println("SMALLTALK is NOT in the dictionar+
y.");
}
int xclay = pldehp.search("clay");
IO.println("Position of CLAY = " + xclay);
int xsmalltalk = pldehp.search("smalltalk");
IO.println("Position of SMALLTALK = " + xsmalltal+
k);
// Sort the dictionary alphabetically
IO.println();
IO.println("DICTIONARY IN ALPHABETICAL ORDER.");
pldehp.sort();
pldehp.display();
// Sort the dictionary by length
IO.println();
IO.println("DICTIONARY ORDERED BY WORD LENGTH.");+
pldehp.sortByLength();
pldehp.display();
// Sort the dictionary by syllable count
IO.println();
IO.println("DICTIONARY ORDERED BY SYLLABLE COUNT.+
");
pldehp.sortBySyllableCount();
pldehp.display();
// Select 10 words at random from PLDEHP
IO.println();
IO.println("--> PRINT 10 RANDOM WORDS FROM PLDEHP+
.");
for ( int x = 1; x <= 10; x++ )
{
pldehp.select().println();
}
// Select 10 2-syllable words at random from PLDEHP
IO.println();
IO.println("--> PRINT 10 RANDOM 2 SYLLABLE WORDS.+
");
for ( int x = 1; x <= 10; x++ )
{
pldehp.select(2).println();
}
// Select 10 words at random from PLDEHP which
// have "va" "la"
IO.println();
IO.println("--> PRINT 10 RANDOM 'VA LA' WORDS.");+
String[] sounds1 = {"va","la"};
for ( int x = 1; x <= 10; x++ )
{
pldehp.select(sounds1).println();
}
// Select 10 words at random from PLDEHP which
// have "in" "van" "thon"
IO.println();
IO.println("--> PRINT 10 RANDOM 'IN VAN THON' WOR+
DS.");
String[] sounds2 = {"van","in","thon"};
for ( int x = 1; x <= 10; x++ )
{
pldehp.select(sounds2).println();
}
// Select 10 words at random from PLDEHP which
// have 3 syllables and end in either "in" "van" "th+
on"
IO.println();
IO.print("--> PRINT 10 RANDOM ");
IO.print("'IN VAN THON' WORDS ");
IO.println("OF 3 SYLLABLES.");
for ( int x = 1; x <= 10; x++ )
{
pldehp.select(3,sounds2).println();
}
// Create a sport-related sentence using user-define+
d
// dictionaries.
IO.println();
IO.print("--> PRINT A SPORT-RELATED SENTENCE ");
IO.println("USING USER-DEFINED DICTIONARIES.");
Dictionary sportNouns = new Dictionary("sportNouns");
sportNouns.add("sportn.data");
Dictionary verbs = new Dictionary("verbs");
verbs.add("verbs.data");
Dictionary adjectives = new Dictionary("adjectives");
adjectives.add("adjs.data");
Dictionary sportArticle = new Dictionary("sportArticle")+
;
sportArticle.add("sportart.data");
Dictionary sportNouns2 = new Dictionary("sportNouns2");
sportNouns2.add("sportn2.data");
Word noun1 = sportNouns.select();
Word verb = verbs.select();
Word article = sportArticle.select();
Word adjective = adjectives.select();
Word noun2 = sportNouns2.select();
String output = sportSentence(noun1, verb, article, adje+
ctive, noun2);
IO.println(output);
// Compare two sports using user-defined
// dictionaries.
IO.println();
IO.print("--> COMPARE TWO SPORTS ");
IO.println("USING USER-DEFINED DICTIONARIES.");
Word cnoun1 = sportNouns.select();
Word cverb = verbs.select();
Word cadjective = adjectives.select();
Word cnoun2 = sportNouns.select();
String cOutput = sportCompareSentence(cnoun1, cverb, cad+
jective, cnoun2);
IO.println(cOutput);
}
static private String sportSentence(Word a, Word +
b, Word c, Word d, Word e)
{
String helper = a.word();
String helper2 = helper.substring(0,1);
String helper3 = helper2.toUpperCase() + helper.subs+
tring(1);
return ( helper3 + " " + b.word() + " " + c.word() ++
" " + d.word() + " " + e.word() + "." );
}
static private String sportCompareSentence(Word a+
, Word b, Word c, Word d)
{
String helper = a.word();
String helper2 = helper.substring(0,1);
String helper3 = helper2.toUpperCase() + helper.subs+
tring(1);
return ( helper3 + " " + b.word() + " as " + c.word(+
) + " as " + d.word() + "." );
}
}
// Demo
// ---------------------------------------------------
/*
$ javac OfficialDictionaryTestApp.java
$ java OfficialDictionaryTestApp
--> INTERNALLY CREATE 2 WORDS.
mind ( mind )
design ( de sign )
--> INTERNALLY CREATE DICTIONARY.
SMALL Dictionary
mind ( mind )
design ( de sign )
--> CREATE A DICTIONARY EXTERNALLY
PLDEHP Dictionary
lisp ( lisp )
smalltalk ( small talk )
sml ( s m l )
prolog ( pro log )
python ( py thon )
perl ( perl )
java ( ja va )
modula ( mod u la )
javascript ( ja va script )
html ( h t m l )
solitude ( sol i tude )
moonglow ( moon glow )
caravan ( car a van )
slytherin ( sly ther in )
hufflepuff ( huf fle puff )
ravenclaw ( ra ven claw )
gryffindor ( gryf fin dor )
quidditch ( quid ditch )
animagi ( an i ma gi )
hewhomustnotbenamed ( he who must not be named )
SEARCHING FOR LANGUAGES...
CLAY is NOT in the dictionary.
SMALLTALK is in the dictionary.
Position of CLAY = -1
Position of SMALLTALK = 1
DICTIONARY IN ALPHABETICAL ORDER.
PLDEHP Dictionary
animagi ( an i ma gi )
caravan ( car a van )
gryffindor ( gryf fin dor )
hewhomustnotbenamed ( he who must not be named )
html ( h t m l )
hufflepuff ( huf fle puff )
java ( ja va )
javascript ( ja va script )
lisp ( lisp )
modula ( mod u la )
moonglow ( moon glow )
perl ( perl )
prolog ( pro log )
python ( py thon )
quidditch ( quid ditch )
ravenclaw ( ra ven claw )
slytherin ( sly ther in )
smalltalk ( small talk )
sml ( s m l )
solitude ( sol i tude )
DICTIONARY ORDERED BY WORD LENGTH.
PLDEHP Dictionary
sml ( s m l )
java ( ja va )
lisp ( lisp )
perl ( perl )
html ( h t m l )
prolog ( pro log )
python ( py thon )
modula ( mod u la )
animagi ( an i ma gi )
caravan ( car a van )
moonglow ( moon glow )
solitude ( sol i tude )
slytherin ( sly ther in )
smalltalk ( small talk )
quidditch ( quid ditch )
ravenclaw ( ra ven claw )
hufflepuff ( huf fle puff )
gryffindor ( gryf fin dor )
javascript ( ja va script )
hewhomustnotbenamed ( he who must not be named )
DICTIONARY ORDERED BY SYLLABLE COUNT.
PLDEHP Dictionary
lisp ( lisp )
perl ( perl )
java ( ja va )
prolog ( pro log )
python ( py thon )
moonglow ( moon glow )
smalltalk ( small talk )
quidditch ( quid ditch )
sml ( s m l )
solitude ( sol i tude )
slytherin ( sly ther in )
modula ( mod u la )
caravan ( car a van )
ravenclaw ( ra ven claw )
hufflepuff ( huf fle puff )
gryffindor ( gryf fin dor )
javascript ( ja va script )
html ( h t m l )
animagi ( an i ma gi )
hewhomustnotbenamed ( he who must not be named )
--> PRINT 10 RANDOM WORDS FROM PLDEHP.
solitude
sml
ravenclaw
modula
quidditch
hufflepuff
modula
slytherin
java
moonglow
--> PRINT 10 RANDOM 2 SYLLABLE WORDS.
python
moonglow
quidditch
java
quidditch
python
moonglow
moonglow
smalltalk
moonglow
--> PRINT 10 RANDOM 'VA LA' WORDS.
java
java
modula
modula
modula
java
modula
java
modula
java
--> PRINT 10 RANDOM 'IN VAN THON' WORDS.
caravan
python
slytherin
python
slytherin
python
python
caravan
caravan
python
--> PRINT 10 RANDOM 'IN VAN THON' WORDS OF 3 SYLLABLES.
caravan
slytherin
slytherin
slytherin
caravan
caravan
caravan
slytherin
slytherin
slytherin
--> PRINT A SPORT-RELATED SENTENCE USING USER-DEFINED DICT+
IONARIES.
Rugby wasn't a smart education.
--> COMPARE TWO SPORTS USING USER-DEFINED DICTIONARIES.
Baseball was as cool as bowling.
$
*/
|
|
|