SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
import java.util.*;
public class LetterSquare {
public static final int MOST_WORDS = 10;
public static final String WORDS_FILE = "word_list.txt";
public static final Dictionary dictionary = new Dictionary(WORDS_FILE);
private String[] sides;
private String[] letters;
private String[] words;
public LetterSquare(String[] sides) {
if (sides == null || sides.length != 4) {
throw new IllegalArgumentException(
"parameter must be an array of 4 strings");
}
this.sides = sides;
this.letters = new String[12];
int letterNum = 0;
for (int i = 0; i < sides.length; i++) {
if (sides[i] == null || sides[i].length() != 3) {
throw new IllegalArgumentException(
"invalid side string: " + sides[i]);
}
for (int j = 0; j < 3; j++) {
this.letters[letterNum] = this.sides[i].substring(j, j+1);
letterNum++;
}
}
this.words = new String[MOST_WORDS];
for (int i = 0; i < this.words.length; i++) {
this.words[i] = "";
}
}
public String toString() {
String s = "";
// top of the square (i.e., sides[0])
for (int i = 0; i < 3; i++) {
s += " " + this.sides[0].charAt(i);
}
s += "n";
for (int i = 0; i < 3; i++) {
s += this.sides[1].charAt(i);
s += " " + this.sides[2].charAt(i);
s += "n";
}
for (int i = 0; i < 3; i++) {
s += " " + this.sides[3].charAt(i);
}
s += "n";
return s;
}
private static String lastLetter(String word) {
return word.substring(word.length() - 1);
}
private static String removeLast(String word) {
return word.substring(0, word.length() - 1);
}
private void addLetter(String letter, int wordNum) {
this.words[wordNum] += letter;
}
private void removeLetter(int wordNum) {
this.words[wordNum] = removeLast(this.words[wordNum]);
}
private boolean alreadyUsed(String word) {
for (String w : this.words) {
if (w.equals(word)) {
return true;
}
}
return false;
}
private boolean onSameSide(String letter1, String letter2) {
for (String side : this.sides) {
if (side.contains(letter1) && side.contains(letter2)) {
return true;
}
}
return false;
}
private boolean allLettersUsed() {
for (String letter : this.letters) {
boolean anyWordHasLetter = false;
for (String w : this.words) {
if (w.contains(letter)) {
anyWordHasLetter = true;
break;
}
}
if (!anyWordHasLetter) {
return false;
}
}
return true;
}
private void print
Solution
(int wordNum) {
for (int i = 0; i <= wordNum; i++) {
System.out.println(this.words[i]);
}
}
private boolean isValid(String letter, int wordNum, int charNum) {
return false;
}
private boolean solveRB(int wordNum, int charNum, int maxWords) {
return false;
}
public void solve() {
int maxWords = 1;
while (maxWords <= MOST_WORDS) {
System.out.println("Looking for a solution of length "
+ maxWords + "...");
if (this.solveRB(0, 0, maxWords)) {
return;
}
maxWords++;
}
System.out.println("No solution found using up to "
+ MOST_WORDS + " words.");
}
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
String[] sides = new String[4];
String[] prompts = {"top side: ", "left side: ",
"right side: ", "bottom side: "};
for (int i = 0; i < 4; i++) {
System.out.print(prompts[i]);
sides[i] = console.nextLine();
}
LetterSquare puzzle = new LetterSquare(sides);
System.out.println("Here is the puzzle:");
System.out.println(puzzle);
puzzle.solve();
console.close();
}
}
Task: implement isValid
You should not change the headers that we have provided.
You should start by implementing the isValid helper method that will be used to check if a given
letter would work as the next letter in the current word, given the words and prefixes in the
dictionary and the constraints of the puzzle described at the beginning of the problem.
This method must take three parameters:
- letter: a single-character string representing the letter whose validity is being tested
- wordNum: an integer specifying the index of the position in the words array of the word that is
currently being built
- charNum: an integer specifying the index of the position within the current word that letter is
being considered for.
It should return true if the specified letter is a valid choice for the letter in position charNumof
the word in position wordNum of the words array, and false otherwise. You may assume that
only appropriate values will be passed in. In particular, you may assume that letter is one of the
letters of the puzzle.
The constraints that you need to check will depend on the value of the charNum parameter (and
possibly also of the wordNum parameter).
For example, lets assume that we have the following situation:
We are solving the puzzle shown at the start of the problem (the one with sides {"tae", "nih",
"omk", "lys"}).
We are looking for a solution of at most 2 words.
The current partial solution is {"time", ...}.
We are within the call this.solveRB(0, 4, 2) i.e., we are attempting to expand the word in
position 0 ("time") by finding a letter that would work in position 4 of that word.
Given this situation:
this.isValid("l", 0, 4) should return true because "l" is on a different side of the puzzle than "e"
(the letter that was added to give "time") and "timel" is a word and/or a prefix of a word in the
dictionary (which we know becausedictionary.hasString("timel") returns true)
this.isValid("s", 0, 4) should also return true because "s" is on different side of the puzzle than
"e" and dictionary.hasString("times") returns true
Now imagine that we have added the letter "s" to the partial solution described above to give a
new partial solution {"times", ...} and that we are now focused on the first letter in second word
in the solution (i.e., that we are within the call this.solveRB(1, 0, 2)). Given this situation:
this.isValid("s", 1, 0) should return true because we are focused on the first letter in a new word
and "s" is the last letter of the previous word ("times")
this.isValid("l", 1, 0) should return false because we are focused on the first letter in a new word
and "l" is not the last letter of the previous word.
Other notes:
You should take advantage of one or more of the methods in the Dictionary object given by the
class constant dictionary.
You will need a special case for handling the first character of the first word in the solution. In
that case, any letter of the puzzle is valid!
When is considering a case in which the current word is being expanded by one letter, the
method should return false if adding the letter would produce a word that is already part of the
solution. Otherwise, you could end up producing a solution that repeatedly uses the same word
(e.g., {"toast", "toast", ...}). Note that we have given you a helper method that makes it easy to
check for this case!
isValid should only determine if the specified letter is a valid choice for the next letter. It should
not actually add the letter to the solution.
We strongly encourage you to thoroughly test your isValid method to ensure that it works in all
cases!
The best way to do this is to add some temporary test code to the beginning of the mainmethod
in LetterSquare.
For example, the description above includes some cases involving isValid that are based on the
puzzle shown at the start of the problem (the one with sides {"tae", "nih", "omk", "lys"}).
You could test these cases by adding temporary code that looks like the following to the start of
main:

Mais conteúdo relacionado

Semelhante a import java.util.;public class LetterSquare {public static fina.pdf

I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfarkmuzikllc
 
Beginner's Python Cheat Sheet.pdf
Beginner's Python Cheat Sheet.pdfBeginner's Python Cheat Sheet.pdf
Beginner's Python Cheat Sheet.pdfAkhileshKumar436707
 
Cs hangman
Cs   hangmanCs   hangman
Cs hangmaniamkim
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Java Code The traditional way to deal with these in Parsers is the .pdf
Java Code The traditional way to deal with these in Parsers is the .pdfJava Code The traditional way to deal with these in Parsers is the .pdf
Java Code The traditional way to deal with these in Parsers is the .pdfstopgolook
 
beginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxbeginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxHongAnhNguyn285885
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldBTI360
 
Kotlin, Spek and tests
Kotlin, Spek and testsKotlin, Spek and tests
Kotlin, Spek and testsintive
 
python cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningpython cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningTURAGAVIJAYAAKASH
 
Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat SheetVerxus
 
beginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdfbeginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdfElNew2
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Java script objects 1
Java script objects 1Java script objects 1
Java script objects 1H K
 

Semelhante a import java.util.;public class LetterSquare {public static fina.pdf (20)

Ruby cheat sheet
Ruby cheat sheetRuby cheat sheet
Ruby cheat sheet
 
easyPy-Basic.pdf
easyPy-Basic.pdfeasyPy-Basic.pdf
easyPy-Basic.pdf
 
130706266060138191
130706266060138191130706266060138191
130706266060138191
 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
 
Beginner's Python Cheat Sheet.pdf
Beginner's Python Cheat Sheet.pdfBeginner's Python Cheat Sheet.pdf
Beginner's Python Cheat Sheet.pdf
 
1.5 pattern matching
1.5 pattern matching1.5 pattern matching
1.5 pattern matching
 
Cs hangman
Cs   hangmanCs   hangman
Cs hangman
 
Word games in c
Word games in cWord games in c
Word games in c
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Java Code The traditional way to deal with these in Parsers is the .pdf
Java Code The traditional way to deal with these in Parsers is the .pdfJava Code The traditional way to deal with these in Parsers is the .pdf
Java Code The traditional way to deal with these in Parsers is the .pdf
 
beginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxbeginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptx
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
Kotlin, Spek and tests
Kotlin, Spek and testsKotlin, Spek and tests
Kotlin, Spek and tests
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf
 
python cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningpython cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learning
 
Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat Sheet
 
beginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdfbeginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdf
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Java script objects 1
Java script objects 1Java script objects 1
Java script objects 1
 

Mais de maheshkumar12354

In a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfIn a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfmaheshkumar12354
 
In a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfIn a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfmaheshkumar12354
 
In a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfIn a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfmaheshkumar12354
 
In a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfIn a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfmaheshkumar12354
 
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfIn a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfmaheshkumar12354
 
In 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfIn 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfmaheshkumar12354
 
In 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfIn 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfmaheshkumar12354
 
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfIn 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfmaheshkumar12354
 
Implement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfImplement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfmaheshkumar12354
 
Implement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfImplement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfmaheshkumar12354
 
Implement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdfImplement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdfmaheshkumar12354
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfmaheshkumar12354
 
import java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfimport java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfmaheshkumar12354
 
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfImplement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfmaheshkumar12354
 
If you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfIf you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfmaheshkumar12354
 
Imagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfImagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfmaheshkumar12354
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdfmaheshkumar12354
 
Im posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfIm posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfmaheshkumar12354
 
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfIgnacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfmaheshkumar12354
 
imagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfimagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfmaheshkumar12354
 

Mais de maheshkumar12354 (20)

In a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfIn a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdf
 
In a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfIn a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdf
 
In a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfIn a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdf
 
In a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfIn a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdf
 
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfIn a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
 
In 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfIn 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdf
 
In 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfIn 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdf
 
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfIn 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
 
Implement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfImplement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdf
 
Implement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfImplement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdf
 
Implement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdfImplement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdf
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
import java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfimport java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdf
 
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfImplement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
 
If you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfIf you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdf
 
Imagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfImagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdf
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdf
 
Im posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfIm posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdf
 
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfIgnacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
 
imagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfimagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdf
 

Último

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 

Último (20)

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 

import java.util.;public class LetterSquare {public static fina.pdf

  • 1. import java.util.*; public class LetterSquare { public static final int MOST_WORDS = 10; public static final String WORDS_FILE = "word_list.txt"; public static final Dictionary dictionary = new Dictionary(WORDS_FILE); private String[] sides; private String[] letters; private String[] words; public LetterSquare(String[] sides) { if (sides == null || sides.length != 4) { throw new IllegalArgumentException( "parameter must be an array of 4 strings"); } this.sides = sides; this.letters = new String[12]; int letterNum = 0; for (int i = 0; i < sides.length; i++) { if (sides[i] == null || sides[i].length() != 3) { throw new IllegalArgumentException( "invalid side string: " + sides[i]); } for (int j = 0; j < 3; j++) { this.letters[letterNum] = this.sides[i].substring(j, j+1); letterNum++; } } this.words = new String[MOST_WORDS]; for (int i = 0; i < this.words.length; i++) { this.words[i] = ""; } } public String toString() { String s = ""; // top of the square (i.e., sides[0]) for (int i = 0; i < 3; i++) {
  • 2. s += " " + this.sides[0].charAt(i); } s += "n"; for (int i = 0; i < 3; i++) { s += this.sides[1].charAt(i); s += " " + this.sides[2].charAt(i); s += "n"; } for (int i = 0; i < 3; i++) { s += " " + this.sides[3].charAt(i); } s += "n"; return s; } private static String lastLetter(String word) { return word.substring(word.length() - 1); } private static String removeLast(String word) { return word.substring(0, word.length() - 1); } private void addLetter(String letter, int wordNum) { this.words[wordNum] += letter; } private void removeLetter(int wordNum) { this.words[wordNum] = removeLast(this.words[wordNum]); } private boolean alreadyUsed(String word) { for (String w : this.words) { if (w.equals(word)) { return true; } } return false; } private boolean onSameSide(String letter1, String letter2) { for (String side : this.sides) {
  • 3. if (side.contains(letter1) && side.contains(letter2)) { return true; } } return false; } private boolean allLettersUsed() { for (String letter : this.letters) { boolean anyWordHasLetter = false; for (String w : this.words) { if (w.contains(letter)) { anyWordHasLetter = true; break; } } if (!anyWordHasLetter) { return false; } } return true; } private void print Solution (int wordNum) { for (int i = 0; i <= wordNum; i++) { System.out.println(this.words[i]); } } private boolean isValid(String letter, int wordNum, int charNum) { return false; } private boolean solveRB(int wordNum, int charNum, int maxWords) { return false; } public void solve() { int maxWords = 1;
  • 4. while (maxWords <= MOST_WORDS) { System.out.println("Looking for a solution of length " + maxWords + "..."); if (this.solveRB(0, 0, maxWords)) { return; } maxWords++; } System.out.println("No solution found using up to " + MOST_WORDS + " words."); } public static void main(String[] args) { Scanner console = new Scanner(System.in); String[] sides = new String[4]; String[] prompts = {"top side: ", "left side: ", "right side: ", "bottom side: "}; for (int i = 0; i < 4; i++) { System.out.print(prompts[i]); sides[i] = console.nextLine(); } LetterSquare puzzle = new LetterSquare(sides); System.out.println("Here is the puzzle:"); System.out.println(puzzle); puzzle.solve(); console.close(); } } Task: implement isValid You should not change the headers that we have provided. You should start by implementing the isValid helper method that will be used to check if a given letter would work as the next letter in the current word, given the words and prefixes in the dictionary and the constraints of the puzzle described at the beginning of the problem. This method must take three parameters: - letter: a single-character string representing the letter whose validity is being tested - wordNum: an integer specifying the index of the position in the words array of the word that is currently being built
  • 5. - charNum: an integer specifying the index of the position within the current word that letter is being considered for. It should return true if the specified letter is a valid choice for the letter in position charNumof the word in position wordNum of the words array, and false otherwise. You may assume that only appropriate values will be passed in. In particular, you may assume that letter is one of the letters of the puzzle. The constraints that you need to check will depend on the value of the charNum parameter (and possibly also of the wordNum parameter). For example, lets assume that we have the following situation: We are solving the puzzle shown at the start of the problem (the one with sides {"tae", "nih", "omk", "lys"}). We are looking for a solution of at most 2 words. The current partial solution is {"time", ...}. We are within the call this.solveRB(0, 4, 2) i.e., we are attempting to expand the word in position 0 ("time") by finding a letter that would work in position 4 of that word. Given this situation: this.isValid("l", 0, 4) should return true because "l" is on a different side of the puzzle than "e" (the letter that was added to give "time") and "timel" is a word and/or a prefix of a word in the dictionary (which we know becausedictionary.hasString("timel") returns true) this.isValid("s", 0, 4) should also return true because "s" is on different side of the puzzle than "e" and dictionary.hasString("times") returns true Now imagine that we have added the letter "s" to the partial solution described above to give a new partial solution {"times", ...} and that we are now focused on the first letter in second word in the solution (i.e., that we are within the call this.solveRB(1, 0, 2)). Given this situation: this.isValid("s", 1, 0) should return true because we are focused on the first letter in a new word and "s" is the last letter of the previous word ("times") this.isValid("l", 1, 0) should return false because we are focused on the first letter in a new word and "l" is not the last letter of the previous word. Other notes: You should take advantage of one or more of the methods in the Dictionary object given by the class constant dictionary. You will need a special case for handling the first character of the first word in the solution. In that case, any letter of the puzzle is valid! When is considering a case in which the current word is being expanded by one letter, the method should return false if adding the letter would produce a word that is already part of the solution. Otherwise, you could end up producing a solution that repeatedly uses the same word
  • 6. (e.g., {"toast", "toast", ...}). Note that we have given you a helper method that makes it easy to check for this case! isValid should only determine if the specified letter is a valid choice for the next letter. It should not actually add the letter to the solution. We strongly encourage you to thoroughly test your isValid method to ensure that it works in all cases! The best way to do this is to add some temporary test code to the beginning of the mainmethod in LetterSquare. For example, the description above includes some cases involving isValid that are based on the puzzle shown at the start of the problem (the one with sides {"tae", "nih", "omk", "lys"}). You could test these cases by adding temporary code that looks like the following to the start of main: