PROGRAMMING (60 points) Scrabble Help: The file dictionary.txtPreview the docume
ID: 3709284 • Letter: P
Question
PROGRAMMING (60 points) Scrabble Help: The file dictionary.txtPreview the document contains all of the words in the Official Scrabble Player's Dictionary, Second Edition. (Note this list contains some offensive language.) Write a class, WordLists, in Java that generates useful word lists for scrabble players using this list. Your class should contain the following methods: WordLists(String fileName): a constructor that takes the name of the dictionary file as the only parameter. lengthN(int n): returns an ArrayList of all length n words (Strings) in the dictionary file. startsWith(char firstLetter, int n): returns an ArrayList of words of length n beginning with the letter firstLetter containsLetter(char included, int n): returns an ArrayList of words of length n containing the letter included but not beginning with it. multiLetter(char included, int m): returns an ArrayList of words with at least m occurrences of the letter included. Use the included template for your WordLists.javaPreview the document file. Write your own test class for your WordLists class that tries these methods out and writes the word lists (the ArrayLists of strings) to text files.
A Template for the WordLists.java file is in the Assignment. You must create your own test class (separate file with main method) that tests each of your methods and writes the resutls to a text file. In addition to the source files include a text file named readMe.txt with an explanation of how your program works. That is, write in plain English, instructions for using your software, explanations for how and why you chose to design your code the way you did. The readMe.txt file is also an opportunity for you to get partial credit when certain requirements of the assignment are not met.
Template:
//*************************************
//
// WordLists.java
//
// Class to aid with Scrabble
// Programming Project 5, COMS W1004
//
//
// Your Name:jia yan
// Your Uni:
//**************************************
import java.util.ArrayList;
public class WordLists{
//instance variables
private String[] words; //array of words taken in
private int wordCount;
private File name;
private Boolean hasLetter;
public WordLists(String fileName){
// your code here
}
public ArrayList<String> lengthN(int n){
// your code here
}
public ArrayList<String> startsWith(char firstLetter, int n){
// your code here
}
public ArrayList<String> containsLetter(char included, int n){
// your code here
}
public ArrayList<String> multiLetter(char included, int m){
// your code here
}
} // end of class
Part of the dictionary,its a lot:
aa
aah
aahed
aahing
aahs
aal
aalii
aaliis
aals
aardvark
aardvarks
aardwolf
aardwolves
aargh
aas
aasvogel
aasvogels
aba
abaca
abacas
abaci
aback
abacus
abacuses
abaft
abaka
abakas
abalone
abalones
abamp
abampere
abamperes
abamps
abandon
abandoned
abandoning
abandonment
abandonments
abandons
abas
abase
abased
abasedly
abasement
abasements
abaser
abasers
abases
abash
abashed
abashes
abashing
abashment
abashments
abasing
abatable
abate
abated
abatement
abatements
abater
abaters
abates
abating
abatis
abatises
abator
abators
abattis
abattises
abattoir
abattoirs
abaxial
abaxile
abba
abbacies
abbacy
abbas
abbatial
abbe
abbes
abbess
abbesses
abbey
abbeys
abbot
abbotcies
abbotcy
abbots
abbreviate
abbreviated
abbreviates
abbreviating
abbreviation
abbreviations
abbreviator
abbreviators
abdicate
abdicated
abdicates
abdicating
abdication
abdications
abdicator
abdicators
abdomen
abdomens
abdomina
abdominal
abdominally
abduce
abduced
abducens
abducent
abducentes
abduces
abducing
abduct
abducted
abducting
abduction
abductions
abductor
abductores
abductors
abducts
abeam
abed
abele
abeles
abelmosk
abelmosks
aberrance
aberrances
aberrancies
aberrancy
aberrant
aberrants
aberration
aberrational
aberrations
abet
abetment
abetments
abets
abettal
abettals
abetted
abetter
abetters
abetting
abettor
abettors
abeyance
abeyances
abeyancies
abeyancy
abeyant
abfarad
abfarads
abhenries
abhenry
abhenrys
abhor
abhorred
abhorrence
abhorrent
abhorrently
abhorrer
abhorrers
abhorring
abhors
abidance
abidances
abide
abided
abider
abiders
abides
abiding
abigail
abigails
abilities
ability
abioses
abiosis
abiotic
abject
abjectly
abjectness
abjuration
abjurations
abjure
abjured
abjurer
abjurers
abjures
abjuring
ablate
ablated
ablates
ablating
ablation
ablations
ablative
ablatives
ablaut
ablauts
ablaze
able
ablegate
ablegates
abler
ables
ablest
ablings
ablins
abloom
abluent
abluents
ablush
abluted
ablution
ablutions
ably
abmho
abmhos
abnegate
abnegated
abnegates
abnegating
abnegation
abnegations
abnegator
abnegators
abnormal
abnormalities
abnormality
abnormally
abnormals
abo
aboard
abode
aboded
abodes
aboding
abohm
abohms
aboideau
aboideaus
aboideaux
aboil
aboiteau
aboiteaus
aboiteaux
abolish
abolished
abolishes
abolishing
abolishment
abolishments
abolition
abolitionary
abolitionism
abolitionist
abolitionists
abolitions
abolla
abollae
aboma
abomas
abomasa
abomasal
abomasi
abomasum
abomasus
abominable
abominably
abominate
abominated
abominates
abominating
abomination
abominations
abominator
abominators
aboon
aboral
aborally
aboriginal
aboriginally
aborigine
aborigines
aborning
abort
aborted
aborter
aborters
aborting
abortion
abortionist
abortionists
abortions
abortive
abortively
aborts
abos
abought
aboulia
aboulias
aboulic
abound
abounded
abounding
abounds
about
above
aboveboard
aboves
abracadabra
abradant
abradants
abrade
abraded
abrader
abraders
abrades
abrading
abrasion
abrasions
abrasive
abrasives
abreact
abreacted
abreacting
abreacts
abreast
abri
abridge
abridged
abridgement
abridgements
abridger
abridgers
abridges
abridging
abridgment
abridgments
abris
abroach
abroad
abrogate
abrogated
abrogates
abrogating
abrogation
abrogations
abrupt
abrupter
abruptest
abruptly
abruptness
abscess
abscessed
abscesses
abscessing
abscise
abscised
abscises
abscisin
abscising
abscisins
abscissa
abscissae
abscissas
abscission
abscissions
abscond
absconded
absconding
absconds
absence
absences
absent
absented
absentee
absenteeism
absentees
absenter
absenters
absenting
absently
absentminded
absentmindedly
absentmindedness
absents
absinth
absinthe
absinthes
absinths
absolute
absolutely
absoluteness
absoluter
absolutes
absolutest
absolution
absolutions
absolutism
absolutist
absolutistic
absolutists
absolve
absolved
absolver
absolvers
absolves
absolving
absonant
absorb
absorbabilities
absorbability
absorbable
absorbed
absorbencies
absorbency
absorbent
absorber
absorbers
absorbing
absorbingly
absorbs
absorption
absorptions
absorptive
abstain
abstained
abstainer
abstainers
abstaining
abstains
abstemious
abstemiously
abstention
abstentions
abstentious
absterge
absterged
absterges
absterging
abstinence
abstinent
abstract
abstracted
abstractedly
abstracter
abstracters
abstractest
abstracting
abstraction
abstractions
abstractive
abstractly
abstractness
abstractor
abstractors
abstracts
abstrict
abstricted
abstricting
abstricts
abstruse
abstrusely
abstruseness
abstruser
abstrusest
absurd
absurder
absurdest
absurdities
absurdity
absurdly
absurdness
absurds
abubble
abulia
abulias
abulic
abundance
abundances
abundant
abundantly
abusable
abuse
abused
abuser
abusers
abuses
abusing
abusive
abusively
abusiveness
abut
abutilon
abutilons
abutment
abutments
abuts
abuttal
abuttals
abutted
abutter
abutters
abutting
abuzz
abvolt
abvolts
abwatt
abwatts
aby
abye
abyes
abying
abys
abysm
abysmal
abysmally
abysms
abyss
abyssal
abysses
acacia
acacias
academe
academes
academia
academias
academic
academically
academician
academicians
academicism
academics
academies
academy
acajou
acajous
acaleph
acalephae
acalephe
acalephes
acalephs
acanthi
acanthus
acanthuses
acari
acarid
acaridan
acaridans
acarids
acarine
acarines
acaroid
acarpous
acarus
acaudal
acaudate
acauline
acaulose
acaulous
accede
acceded
acceder
acceders
accedes
acceding
accelerando
accelerate
accelerated
accelerates
accelerating
acceleration
accelerations
accelerative
accelerator
accelerators
accelerometer
accelerometers
accent
accented
accenting
accentor
accentors
accents
accentual
accentuate
accentuated
accentuates
accentuating
accentuation
accentuations
accept
acceptabilities
acceptability
acceptable
acceptably
acceptance
acceptances
acceptant
acceptation
acceptations
accepted
acceptee
acceptees
accepter
accepters
accepting
acceptor
acceptors
accepts
access
accessaries
accessary
accessed
accesses
accessibilities
accessibility
accessible
accessibleness
accessibly
accessing
accession
accessional
accessions
accessories
accessory
accident
accidental
accidentally
accidentalness
accidently
accidents
accidie
accidies
acclaim
acclaimed
acclaiming
acclaims
acclamation
acclamations
acclimate
acclimated
acclimates
acclimating
acclimation
acclimations
acclimatization
acclimatizations
acclimatize
acclimatized
acclimatizes
acclimatizing
acclivities
acclivity
accolade
accolades
accommodate
accommodated
accommodates
accommodating
accommodatingly
accommodation
accommodations
accommodative
accompanied
accompanies
accompaniment
accompaniments
accompanist
accompanists
accompany
accompanying
accomplice
accomplices
accomplish
accomplished
accomplisher
accomplishers
accomplishes
accomplishing
accomplishment
accomplishments
accord
accordance
accordances
accordant
accordantly
accorded
accorder
accorders
according
accordingly
accordion
accordionist
accordionists
accordions
accords
accost
accosted
accosting
accosts
account
accountabilities
accountability
accountable
accountably
accountancy
accountant
accountants
accounted
accounting
accountings
accounts
accouter
accoutered
accoutering
accouterment
accouterments
accouters
accoutre
accoutred
accoutrement
accoutrements
accoutres
accoutring
accredit
accreditation
accreditations
accredited
accrediting
accredits
accrete
accreted
accretes
accreting
accretion
accretionary
accretions
accrual
accruals
accrue
accrued
accruement
accruements
accrues
accruing
acculturate
acculturated
acculturates
acculturating
acculturation
acculturations
accumulate
accumulated
accumulates
accumulating
accumulation
accumulations
accumulative
accumulator
accumulators
accuracies
accuracy
accurate
accurately
accurateness
accursed
accurst
accusal
accusals
accusant
accusants
Explanation / Answer
import java.io.*;
public class WordList {
static String[] words; // An array to hold the words from the file.
// Note that the array will be expanded as
// necessary, in the insertWord() subroutine.
static int wordCount; // The number of words currently stored in
// the array.
public static void main(String[] args) {
TextReader in; // A stream for reading from the input file.
PrintWriter out; // A stream for writing to the output file.
String inputFileName; // Input file name, specified by the user.
String outputFileName; // Output file name, specified by the user.
words = new String[10]; // Start with space for 10 words.
wordCount = 0; // Currently, there are no words in the array.
/* Get the input file name from the user and try to create the
input stream. If there is a FileNotFoundException, print
a message and terminate the program. */
TextIO.put("Input file name? ");
inputFileName = TextIO.getln().trim();
try {
in = new TextReader(new FileReader(inputFileName));
}
catch (FileNotFoundException e) {
TextIO.putln("Can't find file "" + inputFileName + "".");
return;
}
/* Get the output file name from the user and try to create the
output stream. If there is an IOException, print a message
and terminate the program. */
TextIO.put("Output file name? ");
outputFileName = TextIO.getln().trim();
try {
out = new PrintWriter(new FileWriter(outputFileName));
}
catch (IOException e) {
TextIO.putln("Can't open file "" + outputFileName + "" for output.");
TextIO.putln(e.toString());
return;
}
/* Read all the words from the input stream and insert them into
the array of words. Reading from a TextReader can result in
an error of type TextReader.Error. If one occurs, print an
error message and terminate the program. */
try {
while (true) {
// Skip past and non-letters in the input stream. If an
// end-of-stream has been reached, end the loop. Otherwise,
// read a word and insert it into the array of words.
while ( ! in.eof() && ! Character.isLetter(in.peek()) )
in.getAnyChar();
if (in.eof())
break;
insertWord(in.getAlpha());
}
}
catch (TextReader.Error e) {
TextIO.putln("An error occured while reading from the input file.");
TextIO.putln(e.toString());
return;
}
/* Write all the words from the list to the ouput stream. */
for (int i = 0; i < wordCount; i++)
out.println(words[i]);
/* Finish up by checking for an error on the output stream and
printing either a warning message or a message that the words
have been output to the output file. */
if (out.checkError() == true) {
TextIO.putln("Some error occured while writing output.");
TextIO.putln("Output might be incomplete or invalid.");
}
else {
TextIO.putln(wordCount + " words from "" + inputFileName +
"" output to "" + outputFileName + "".");
}
} // end main()
static void insertWord(String w) {
// Insert the word w into the array of words, unless it already
// appears there. All the words in the array are in lower case,
// and w is converted to lower case befoer it is processed.
// Note that the words in the array are kept in alphabetical order.
// If the array has grown too big to hold w, then it is doubled
// in size.
int pos = 0; // This will be the postion in the array where w belongs.
w = w.toLowerCase();
/* Find the position in the array where w belongs, after all the
words that precede w alphabetically. If a copy of w already
occupies that position, then it is not necessary to insert
w, so return immediately. */
while (pos < wordCount && words[pos].compareTo(w) < 0)
pos++;
if (pos < wordCount && words[pos].equals(w))
return;
/* If the array is full, make a new array that is twice as
big, copy all the words from the old array to the new,
and set the variable, words, to refer to the new array. */
if (wordCount == words.length) {
String[] newWords = new String[words.length*2];
System.arraycopy(words,0,newWords,0,wordCount);
words = newWords;
}
/* Put w into its correct position in the array. Move any
words that come after w up one space in the array to
make room for w. */
for (int i = wordCount; i > pos; i--)
words[i] = words[i-1];
words[pos] = w;
wordCount++;
} // end insertWord()
} // end class WordList
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.