Help with Java code BoggleData.txt contains: D R L X E I C P O H S A N H N L Z R
ID: 3848875 • Letter: H
Question
Help with Java code
BoggleData.txt contains:
D R L X E I
C P O H S A
N H N L Z R
W T O O T A
I O S S E T
N W E G H E
B O O J A B
U I E N E S
P S A F K F
I U N H M Qu
Y R D V E L
V E H W H R
I O T M U C
T Y E L T R
S T I T Y D
A G A E E N
Dictionary.txt is a file that has about 60,000 words. one per line.
I also have no idea what to do for step 1 in boggle.java
Tasks and Rubric
Activity
Boggle project
boggle package
Boggle.java
Set the ArrayList member variable that stores the Boggle data equal to method call shakeDice in class Board
core package
Board class
Add member variable of class ArrayList specifically using class String as the data type for the ArrayList to store the game data
Generate a getter for the game data member variable
Randomly select 1 of the 16 dice
Call method rollDie in class Die
Store the returned value in the ArrayList created above for the game data
Be sure to use each die only once, you will have to keep track of which die was used
Implement a method that will loop through the ArrayList of game data and display the board to the output window of the IDE (see figure 2)
Die class
Randomly select one of the six letters of the die that will be used as the game data
Return that value
Boggle application
Test Case 1
Test Case 1 passes
Test Case 2
Test Case 2 passes
Source compiles with no errors
Source runs with no errors
Source includes comments
Total
Perform the following test cases
Test Cases
Action
Expected outcome
Test Case 1
Project view
Completed project view should look like figure 1
Test case 2
Run application
shakeDice output should be similar to figure 2
Figure 1: Output from method displayGameData()
Boggle Board:
I A N W
S W A E
F M D W
O L D A
My code:
//import statements
import core.Board;
import inputOutput.ReadDataFile;
import java.util.ArrayList;
public class Boggle {
//arrays for files
private static ArrayList diceData = new ArrayList();
private static String dataFileName = "BoggleData.txt";
private static ArrayList dictionaryData = new ArrayList();
private static String dictionaryFileName = "Dictionary.txt";
//main method
public static void main(String[] args) {
Boggle boggle = new Boggle();
//read dice
ReadDataFile data = new ReadDataFile(boggle.dataFileName);
data.populateData();
//read dictionary
ReadDataFile dictionary = new ReadDataFile(boggle.dictionaryFileName);
dictionary.populateData();
Board board = new Board(data.getData(), dictionary.getData());
board.populateDice();
//display number of entries
System.out.println(" There are " + dictionary.getData().size()
+ " entries in the dictionary");
}
}
import java.util.ArrayList;
import java.util.Random;
public class Board implements IBoard{
//arrays to store data
private static ArrayList diceData;
private static ArrayList dictionaryData;
private static ArrayList diceStore;
private static ArrayList gameData;
//constructer to store data
public Board(ArrayList diceData, ArrayList dictionary){
this.diceData = diceData;
this.dictionaryData = dictionaryData;
this.diceStore = new ArrayList();
gameData = new ArrayList();
}
@Override
public void populateDice() {
Die die;
int counter = 0;
for(int dice=0; dice < NUMBER_OF_DICE; dice++){
die = new Die();
for(int side=0 ; side< die.NUMBER_OF_SIDES; side++){
die.addLetter(diceData.get(counter++));
}
System.out.print("Die " + dice + ": ");
die.displayLetters();
System.out.println();
diceStore.add(die);
}
}
@Override
public ArrayList shakeDice() {
Random r = new Random();
int[] temp = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int cnt=0;
while(cnt<16) {
int indx=r.nextInt(16);
if(temp[indx]==1)
continue;
temp[indx]=1;
gameData.add(diceStore.get(indx).rollDie());
cnt++;
}
return gameData;
}
public void displayGameData(){
System.out.println("Boggle board:");
for(int kk=0;kk<4;kk++){
for(int aa=0;aa<4;aa++)
System.out.print(gameData.get(kk*4+aa)+" ");
System.out.println();
}
}
public ArrayList getGameData() {
return gameData;
}
}
import java.util.*;
public interface IBoard {
//constant fields
public static final int NUMBER_OF_DICE = 16;
public static final int GRID = 4;
//method signatures
public void populateDice();
public ArrayList shakeDice();
}
import java.util.ArrayList;
import java.util.Random;
public class Die implements IDie {
private ArrayList sides = new ArrayList();
@Override
public String rollDie() {
Random r=new Random();
return sides.get(r.nextInt(sides.size()));
}
@Override
public void addLetter(String letter) {
sides.add(letter);
}
@Override
public void displayLetters() {
for(String value : sides){
System.out.print(value + " ");
}
}
}
public interface IDie {
//consant field
static final int NUMBER_OF_SIDES = 6;
//method signatures
public String rollDie();
public void addLetter(String letter);
public void displayLetters();
}
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadDataFile implements IReadDataFile {
private Scanner inputFile;
private String fileName;
private ArrayList fileData;
public ReadDataFile(String fileName) {
this.fileName = fileName;
fileData = new ArrayList();
}
public ArrayList getData() {
return fileData;
}
@Override
public void populateData() {
try {
URL url = getClass().getResource("/data/" + fileName);
// System.out.println(url.toString());
File file = new File(url.toURI());
inputFile = new Scanner(file);
while(inputFile.hasNextLine()){
String line = inputFile.nextLine();
String letters[] = line.split(" ");
for(String letter : letters)
fileData.add(letter);
}
inputFile.close();
}
catch (IOException | URISyntaxException e){
System.out.print(e.toString());
}
finally {
if (inputFile != null)
inputFile.close();
}
}
}
public interface IReadDataFile {
//method signature
public void populateData();
}
Activity
Boggle project
boggle package
Boggle.java
Set the ArrayList member variable that stores the Boggle data equal to method call shakeDice in class Board
core package
Board class
Add member variable of class ArrayList specifically using class String as the data type for the ArrayList to store the game data
Generate a getter for the game data member variable
Implement method shakeDice to do the following:Loop through the 16 diceRandomly select 1 of the 16 dice
Call method rollDie in class Die
Store the returned value in the ArrayList created above for the game data
Be sure to use each die only once, you will have to keep track of which die was used
Implement a method that will loop through the ArrayList of game data and display the board to the output window of the IDE (see figure 2)
Die class
Implement method rollDie to do the following:Randomly select one of the six letters of the die that will be used as the game data
Return that value
Boggle application
Test Case 1
Test Case 1 passes
Test Case 2
Test Case 2 passes
Source compiles with no errors
Source runs with no errors
Source includes comments
Total
Explanation / Answer
Hello. The question you have posted has missing figures to check for Test case output. With the information available, I have made changes to your code to display the boggle board. Also if you can upload the dictionary file and provdie the link , it can be tested. I tested with a dummy Dictionary file. Please see the output below and the changed file. Mainly all files have been changed to use ArrayList<String> where applicable. Please post a comment on any help you need for this and I shall respond.
For step 1, you need to set diceData = board.shakeDice() in Boggle.java I have done it on line 35 of Boggle.java
For the test cases, one of them is regarding the project structure i.e package names etc. I have moved the files into core and boggle package as indicated in the question. Can you help more if the complete question with images is available.
Note: The board data shown will not exactly match the question as the letters on dice are randomly selected.
ouput
Die 0: D R L X E I
Die 1: C P O H S A
Die 2: N H N L Z R
Die 3: W T O O T A
Die 4: I O S S E T
Die 5: N W E G H E
Die 6: B O O J A B
Die 7: U I E N E S
Die 8: P S A F K F
Die 9: I U N H M Qu
Die 10: Y R D V E L
Die 11: V E H W H R
Die 12: I O T M U C
Die 13: T Y E L T R
Die 14: S T I T Y D
Die 15: A G A E E N
Boggle board:
O R U Y
D S R H
O N S R
W F L W
There are 25 entries in the dictionary
input file Dictionary.txt
aloe aloes alone
als also azlon
azlons azo azon
azons eel een
ene enes enol
enols enoses ens
eon eons eorl
eorls eses esne
esnes
Boggle.java
package boggle;
//import statements
import core.Board;
import java.util.ArrayList;
public class Boggle {
//arrays for files
private static ArrayList diceData ;
private static String dataFileName = "BoggleData.txt";
private static ArrayList dictionaryData = new ArrayList();
private static String dictionaryFileName = "Dictionary.txt";
//main method
public static void main(String[] args) {
Boggle boggle = new Boggle();
//read dice
ReadDataFile data = new ReadDataFile(boggle.dataFileName);
data.populateData();
//read dictionary
ReadDataFile dictionary = new ReadDataFile(boggle.dictionaryFileName);
dictionary.populateData();
Board board = new Board(data.getData(), dictionary.getData());
board.populateDice();
//set data to Boggle shakeDice()
diceData = board.shakeDice();
board.displayGameData();
//display number of entries
System.out.println(" There are " + dictionary.getData().size()
+ " entries in the dictionary");
}
}
Board.java
package core;
import java.util.ArrayList;
import java.util.Random;
public class Board implements IBoard{
//arrays to store data
private static ArrayList<String> diceData;
private static ArrayList<String> dictionaryData;
private static ArrayList<Die> diceStore;
private static ArrayList<String> gameData;
//constructer to store data
public Board(ArrayList<String> diceData, ArrayList<String> dictionary){
this.diceData = diceData;
this.dictionaryData = dictionaryData;
this.diceStore = new ArrayList<Die>();
gameData = new ArrayList<String>();
}
@Override
public void populateDice() {
Die die;
int counter = 0;
for(int dice=0; dice < NUMBER_OF_DICE; dice++){
die = new Die();
for(int side=0 ; side< die.NUMBER_OF_SIDES; side++){
die.addLetter(diceData.get(counter++));
}
System.out.print("Die " + dice + ": ");
die.displayLetters();
System.out.println();
diceStore.add(die);
}
}
@Override
public ArrayList shakeDice() {
Random r = new Random();
int[] temp = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int cnt=0;
while(cnt<16) {
int indx=r.nextInt(16);
if(temp[indx]==1)
continue;
temp[indx]=1;
gameData.add(diceStore.get(indx).rollDie());
cnt++;
}
return gameData;
}
public void displayGameData(){
System.out.println("Boggle board:");
for(int kk=0;kk<4;kk++){
for(int aa=0;aa<4;aa++)
System.out.print(gameData.get(kk*4+aa)+" ");
System.out.println();
}
}
public ArrayList getGameData() {
return gameData;
}
}
Die.java
package core;
import java.util.ArrayList;
import java.util.Random;
public class Die implements IDie {
private ArrayList<String> sides = new ArrayList<String>();
@Override
public String rollDie() {
Random r=new Random();
return sides.get(r.nextInt(sides.size())) ;
}
@Override
public void addLetter(String letter) {
sides.add(letter);
}
@Override
public void displayLetters() {
for(String value : sides){
System.out.print(value + " ");
}
}
}
ReadDataFile.java
package boggle;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadDataFile implements IReadDataFile {
private Scanner inputFile;
private String fileName;
private ArrayList fileData;
public ReadDataFile(String fileName) {
this.fileName = fileName;
fileData = new ArrayList();
}
public ArrayList getData() {
return fileData;
}
@Override
public void populateData() {
try {
URL url = getClass().getResource("/data/" + fileName);
// System.out.println(url.toString());
File file = new File(url.toURI());
inputFile = new Scanner(file);
while(inputFile.hasNextLine()){
String line = inputFile.nextLine();
String letters[] = line.split(" ");
for(String letter : letters)
fileData.add(letter);
}
inputFile.close();
}
catch (IOException | URISyntaxException e){
System.out.print(e.toString());
}
finally {
if (inputFile != null)
inputFile.close();
}
}
}
Dictionary.txt
aloe aloes alone
als also azlon
azlons azo azon
azons eel een
ene enes enol
enols enoses ens
eon eons eorl
eorls eses esne
esnes
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.