



|
|
CS1 Course Site
|
Programming Challenge Archive
Librarying a Class
WordTestApp
|
|
|
JavaApplication --
WordTestApp
// General Information
// ---------------------------------------------------
// File: WordTestApp.java
// Type: java application file
// Date: Wed Nov 15, 2000
// Name: blue
// Line: Test program for the Word class
// Application Description
// ---------------------------------------------------
/*
Test program for the Word class
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import white.words.*;
// Application Class
// ---------------------------------------------------
class WordTestApp
{
static public void main (String args[])
{
// Read and display three words
IO.println("Read and display 3 words.");
Word x = new Word();
x.display();
Word y = new Word();
y.display();
Word z = new Word();
z.display();
IO.println();
// Internally create and display three words.
IO.println("Internally create and display 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();
IO.println();
// Compute the length of each word
IO.println("The word lengths.");
IO.println("Length of x = " + x.length());
IO.println("Length of y = " + y.length());
IO.println("Length of z = " + z.length());
IO.println("Length of a = " + a.length());
IO.println("Length of b = " + b.length());
IO.println();
// Compute the syllable count for each word
IO.println("The syllable counts.");
IO.println("x has " + x.nrSyllables() + " syllabl+
es");
IO.println("y has " + y.nrSyllables() + " syllabl+
es");
IO.println("z has " + z.nrSyllables() + " syllabl+
es");
IO.println("a has " + a.nrSyllables() + " syllabl+
es");
IO.println("b has " + b.nrSyllables() + " syllabl+
es");
IO.println();
// Display the words in alphabetical order
IO.println("Display the words in alphabetical ord+
er.");
Word words[] = new Word[5];
words[0] = x;
words[1] = y;
words[2] = z;
words[3] = a;
words[4] = b;
order(words);
for ( int i = 0; i < words.length; i++ )
{
words[i].println();
}
// Display the last syllable of each word
IO.println("Display the last syllable of each wor+
d.");
for ( int i = 0; i < words.length; i++ )
{
IO.println(words[i].lastSyllable());
}
//Display whether two words are equal.
Word c = a;
IO.println("Display if two words are equal.");
IO.println(a.equals(c));
IO.println(b.equals(c));
//Display which of two words is greater.
IO.println("Display which of two words is greater.");
IO.println(a.greater(b,a));
IO.println(b.greater(a,b));
//Describe each of the instance variables.
a.describe();
IO.println(a.nrSyllables());
}
static private void order(Word w[])
{
for ( int i = 0; i < w.length; i++ )
{
for ( int j = i+1; j < w.length; j++ )
{
if ( Word.less(w[j],w[i]) )
{
Word t = w[j];
w[j] = w[i];
w[i] = t;
}
}
}
}
}
// Demo
// ---------------------------------------------------
/*
$ javac WordTestApp.java
$ java WordTestApp < WordTestApp.data
Read and display 3 words.
parallel ( par al lel )
distributed ( dis tri bu ted )
processing ( pro cess ing )
Internally create and display 2 words.
mind ( mind )
design ( de sign )
The word lengths.
Length of x = 8
Length of y = 11
Length of z = 10
Length of a = 4
Length of b = 6
The syllable counts.
x has 3 syllables
y has 4 syllables
z has 3 syllables
a has 1 syllables
b has 2 syllables
Display the words in alphabetical order.
design
distributed
mind
parallel
processing
Display the last syllable of each word.
sign
ted
mind
lel
ing
$
*/
|
|
|