Write a program that will create word squares. A word square is a grid so that e
ID: 667600 • Letter: W
Question
Write a program that will create word squares. A word square is a grid so that each row and each column spells
a word from a given word list. Below are two sample word squares that my program found.
blast
ratch word
atria asia
scion glom
shank sots
One requirement of this program is that you use no recursion and/or backtracking. You must find solutions
randomly. Specifically, you must use the algorithm I describe below.
Begin by filling the grid with *’s. Then randomly select a (four-letter) word from the word list and put it in the
first row. In the example below the program randomly found the word “shim”. Then try to fill the first column. In
this case the program needs to find a random word that begins with “s” and it finds “spin”. Then move on to the
second row. In this case we need a word that begins with “p” and it finds “polo”. Next, move on to the second
column. We need a word that begins with “ho”. “Hoop” was found this time. Continue alternating between rows
and columns until either a word square is found or it gets stuck. In this case the program got stuck on the third
column because it could not find a word in the word list that begins with “iln”. If it gets stuck before finding a
complete word square, start the process over and try again. Continue until a word square is finally found or you
have to give up. For now give up after trying 1,000,000 times.
**** shim shim shim shim shim
**** **** p*** polo polo polo
**** **** i*** i*** io** ions
**** **** n*** n*** np** np**
Notice that in my completed 4 by 4 example above the first word is “word”. I selected that to be the word in the
first row. You need to be able to either specify the first word in the word square or let it be random.
You must implement the two classes with the methods shown below and use them to find word squares. Do not
make any changes to the code already provided. You may add code, of course, but do not remove any. This
assignment is as much about following instructions as it is about programming. Be sure your methods do exactly
what is expected of them. You must use findRange. Use it to find the range of indices in the word_list array that
contains words with the given prefix. Specifications, algorithms, and other aspects of the program are likely to
change as our class discussions progress. You are, of course, responsible for making sure your program adheres to
all the most recent requirements.
What follows are some more general rules that you must follow for this and, as applicable, all other programs
you write in this class. Failure to follow these rules will cost you points.
1. I will eventually post a final test program. A small sample test program is already posted to help get you started.
A word list file has also been posted. It is from this file that you should find all the words for your word squares.
You are to turn in a printout of your program and a printout of the output obtained from running the final test
program. Don’t wait for my final test program to appear to start debugging your program. You should begin
testing with your own test programs. Also, the final test program I post may not be the same test program I run
on your programs. You cannot assume your program is correct just because it works on any of the posted test
programs.
2. In addition to handing in a listing of your program and a printout of your output, you must also email me both of
your java classes in a single file named prog1.java. Do not email me your output or my test program. Be sure
that my main (test) program is in a different file from your two classes. Also, do not make your classes "public".
The subject of your email should be “Sam Loyd – prog 1” if your name is Sam Loyd and your prog1.java file
should be an attachment. If you deviate from any of these rules you will lose points because doing so makes it
much more difficult for me to manage and test your programs. Be sure to follow all of these instructions
perfectly. For example, in the email subject pay attention to what is upper case, what is lower case, the location
of the hyphen, etc.
4. For all Java code you turn in be sure to:
a) Choose clear and suggestive variable names.
b) No part of your printed output should have lines wrapped to the next line or disappearing off the end of the
page. If you are not sure what this means, ask.
c) Use comments generously to describe how your methods work. Comments should appear inside your
methods, too – not just at the top. That said, don't overdo the comments.
d) There should be no more than one return statement in any method.
e) Do not use any break statements to exit loops.
f) Do not use global variables. If you are not sure if you are using them, ask.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException; // for BufferedReader
class WordSquare {
private int word_size, word_ct;
private String word_list[];
private char result[][];
// sets word_size and word_ct and loads the word-size words into word_list
public WordSquare(int word_size, String fileName) throws IOException {
int ct;
// get the word size
this.word_size = word_size;
word_ct = 0;
String inputLine;
// first count the number of word_size words in fileName
BufferedReader inFile = new BufferedReader(new FileReader(fileName));
inputLine = inFile.readLine();
while (inputLine != null) {
if (inputLine.length() == word_size)
word_ct++;
inputLine = inFile.readLine();
} inFile.close();
// then put those words into an array that has the exact size needed
. . .
}
// try to build a word square of size word_size
// uses the algorithm as described in the handout
public boolean buildSquare(String givenWord) {
String w, pref;
char pref_letters[];
int i, level, ct, row, col;
boolean OK = false;
Range wordRange;
// initialize the square to contain *'s
result = new char[word_size][word_size];
for (row = 0; row < word_size; row++) {
for (col = 0; col < word_size; col++)
result[row][col] = '*';
}
// try to build the required word square
for (ct = 0; ct < 1000000 && !OK; ct++) {
. . .
} return OK;
}
// return the range of indices in word_list that have prefix prefix
// return a range of (-1, -1) if there are none
// You must use binary search to find the general location of the range
public Range findRange(String prefix) {
. . .
}
public String toString() {
. . .
}
// who are you? Put your name here!
public static String myName() {
return ". . ." ;
}
}
class Range {
. . .
I have this But it has error from last part. Fix it If you can. Do not pay attantion to variable names just try to fix it.
package proj1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.management.BufferPoolMXBean;
class myWordSquare {
/* variable declarations */
private int wordsize, wordcount;
private String wordlist[];
private char result[][];
public myWordSquare(int wordsize, String nameOfFile) throws IOException {
int counts;
this.wordsize = wordsize;
wordcount = 0;
String fileInputLine;
BufferedReader inFile = new BufferedReader(new FileReader(nameOfFile));
fileInputLine = inFile.readLine();
while (fileInputLine != null) {
if (fileInputLine.length() == wordsize) {
wordcount++;
fileInputLine = inFile.readLine();
}
}
inFile.close();
wordlist = new String[wordcount];
BufferedReader myInfile = new BufferedReader(new FileReader(nameOfFile));
fileInputLine = myInfile.readLine();
int i = 0;
fileInputLine = myInfile.readLine();
while (fileInputLine != null)
{
if (fileInputLine.length() == wordsize)
{
wordlist[i] = new String(fileInputLine);
i++;
}
fileInputLine = myInfile.readLine();
}
myInfile.close();
}
public boolean makingSquare(String wordGiven)
{
String w, preference;
char prefletters[];
int i, levels, counts, rows, columns;
boolean isOk = false;
myRange mywordRange;
result = new char[wordsize][wordsize];
for (rows = 0; rows < wordsize; rows++) {
for (columns = 0; columns < wordsize; columns++)
result[rows][columns] = '*';
}
for (counts = 0; counts < 1000000 && !isOk; counts++) {
i = 0;
for (rows = 0; rows < wordsize; rows++) {
prefletters[i] = wordGiven.charAt(i);
preference = new String(prefletters);
mywordRange = findMyRange(preference);
if (mywordRange.getmyRange1() == -1&& mywordRange.getmyRange2() ==- 1)
continue;
else
w = wordlist[mywordRange.getmyRange1()];
System.out.println("++++");
for(columns = 0; columns < wordsize; columns++)
{
System.out.println(w + " ");
}
System.out.println();
}
}
return isOk;
}
public myRange findMyRange(String myprefix) {
myRange myTemp;
int first = 0;
int last = 0;
for (int i = 0; i < wordcount; i++) {
if (wordlist[i].toLowerCase().contains(myprefix.toLowerCase()) && first == 0) {
myTemp.setmyRange1 = i;
first = i;
}
else if (wordlist[i].toLowerCase().contains(myprefix.toLowerCase())) {
last = i;
}
}
myTemp.setmyRange1 = last;
}
if (last == 0 && first == 0) {
myTemps.setrange1 = -1;
myTemp.setmyRange2 = -1;
}
return myTemp;
}
public String toString() {
return "";
}
public static String myName() {
return "your name";
}
}
class myRange {
int myrange1;
int myrange2;
public myRange() {
myrange1 = 0;
myrange2 = 0;
}
public setmyRange1(int r1) {
myrange1 = r1;
}
public setmyRange2(int r2) {
myrange2 =r2;
}
public getmyRange1() {
return myrange1;
}
public getmyRange2() {
return myrange2;
}
}
Explanation / Answer
Answer:
Note: Asked to fill the incomplete methods.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException; // for BufferedReader
class WordSquare
{
private int word_size, word_ct;
private String word_list[];
private char result[][];
// sets word_size and word_ct and loads the word-size words into word_list
public WordSquare(int word_size, String fileName) throws IOException
{
int ct;
// get the word size
this.word_size = word_size;
word_ct = 0;
String inputLine;
// first count the number of word_size words in fileName
BufferedReader inFile = new BufferedReader(new FileReader(fileName));
inputLine = inFile.readLine();
while (inputLine != null)
{
if (inputLine.length() == word_size)
word_ct++;
inputLine = inFile.readLine();
}
inFile.close();
// then put those words into an array that has the exact size needed
word_list=new String[word_ct];
BufferedReader inFile1 = new BufferedReader(new FileReader(fileName));
inputLine = inFile1.readLine();
int i=0;
//System.out.print("Hi");
inputLine = inFile1.readLine();
while (inputLine != null)
{
if (inputLine.length() == word_size)
{
word_list[i]=new String(inputLine);
i++;
}
inputLine = inFile1.readLine();
}
inFile1.close();
}
// try to build a word square of size word_size
// uses the algorithm as described in the handout
public boolean buildSquare(String givenWord)
{
String w, pref;
char pref_letters[];
int i, level, ct, row, col;
boolean OK = false;
Range wordRange;
// initialize the square to contain *'s
result = new char[word_size][word_size];
for (row = 0; row < word_size; row++)
{
for (col = 0; col < word_size; col++)
result[row][col] = '*';
}
// try to build the required word square
for (ct = 0; ct < 1000000 && !OK; ct++)
{
i=0;
for(row=0;row<word_size;row++)
{
pref_letteres[i]=givenWord.charAt(i);
pref=new String(pref_letters);
wordRange=findRange(pref);
if(wordRange.getRange1()==-1&&wordRange.getRange2()==-1)
continue;
else
w=word_list[wordRange.getRange1()];
System.out.println("****");
for(col=0;col<word_size;col++)
{
Ststem.out.print(w+" ");
}
System.out.println();
}
}
return OK;
}
// return the range of indices in word_list that have prefix prefix
// return a range of (-1, -1) if there are none
// You must use binary search to find the general location of the range
public Range findRange(String prefix)
{
Range temp;
int first=0;
int last=0;
for(int i=0;i<word_ct;i++)
{
if(word_list[i].toLowerCase().contains(prefix.toLowercase())&&first==0)
{
temp.setRange1=i;
first=i;
}
else if(word_list[i].toLowerCase().contains(prefix.toLowercase()))
{
last=i;
}
}
temp.setRange2=last;
}
if(last==0&&first==0)
{
temp.setrange1=-1;
temp.setRange2=-1;
}
return temp;
}
public String toString()
{
return “ “;
}
// who are you? Put your name here!
public static String myName()
{
return “ur name” ;
}
}
class Range
{
int range1;
int range2;
public Range()
{
range1=0;
range2=0;
}
public setRange1(int r1)
{
range1=r1;
}
public setRange2(int r2)
{
range2=r2;
}
public getRange1()
{
return range1;
}
public getRange2()
{
return range2;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.