Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Implement method shakeDice to do the following:Loop through the 16 dice

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

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

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(); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Support For Data Mining Applications Discovering Knowledge With Inductive Queries Lnai 2682

Authors: Rosa Meo ,Pier L. Lanzi ,Mika Klemettinen

2004th Edition

3540224793, 978-3540224792

More Books

Students also viewed these Databases questions