Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java game program. Would like to know how my code is wrong Boggle class Create member variables of type: ArrayList of class String // stores

Java game program. Would like to know how my code is wrong

Boggle class

Create member variables of type:

ArrayList of class String // stores data from data file with dice data

String // set to the name of input file BoggleData.txt

ArrayList of class String // stores data from data file with dictionary data

String // set to the name of input file Dictionary.txt

Method main() should:

Instantiate an instance of class ReadDataFile passing the file name variable for file BoggleData.txt as an argument

Call method populateData on the above object

Instantiate an instance of class ReadDataFile passing the file name variable for file Dictionary.txt as an argument

Call method populateData on the above object

Instantiate an instance of class Board passing as arguments method call getData on each of the two ReadDataFile object references from above

Call method populateDice on object reference of class Board

Output to the IDE output window the number of objects in the ArrayList of type String that stores the dictionary data

IBoard interface

Add constant fields:

NUMBER_OF_DICE equal to value 16

GRID equal to value 4

Add method signatures:

populateDice with return type void and an empty parameter list

shakeDice with return type ArrayList and an empty parameter list

Board class

Update the class so that it implements interface IBoard

TIP: Use Netbeans right click menu, Insert Code, Implement Method to have the IDE generate the methods for you; you will replace the throw exception statements with the source code you write

Add member variable of type:

ArrayList of class String // stores dice data

ArrayList of class String // stores dictionary data

ArrayList of class Die // stores 16 game dice

Add a custom constructor with two parameters of type ArrayList of class String; it should do the following

Set the member variable of type ArrayList of class String that stores the Boggle data equal to the associated parameter in the method signature

Set the member variable of type ArrayList of class String that stores the dictionary data equal to the associated parameter in the method signature

Instantiate the member variable of type ArrayList of class Die.

Implement method populateDice; the method should do the following:

Declare a variable of type class Die

Declare and initialize a variable of type int to serve as a counter to access the data in the member variable of type ArrayList of type String storing the Boggle data

Loop through the 16 dice (use the constant NUMBER_OF_DICE as your terminating condition):

Instantiate the instance of class Die using the default no-argument constructor

For each Die instance, loop through the six sides of the die (use the constant NUMBER_OF_SIDES as your terminating condition):

Add each of the 6 letters to the Die ArrayList representing the die letters by calling method addLetter in class Die

Display the letters of each die by calling method displayLetters() in class Die on a separate row

Add each die instance to the ArrayList declared specifically for class Die

IDie interface

Add constant field:

NUMBER_OF_SIDES equal to value 6

Add method signatures:

rollDie with return type String and an empty parameter list

addLetter with a return type of void and one parameter of type String representing one of the six letters on the die

displayLetters with a return type of void and an empty parameter list

Die class

Update the class so that it implements interface IDie

TIP: Use Netbeans right click menu, Insert Code, Implement Method to have the IDE generate the methods for you; you will replace the throw exception statements with the source code you write

Add member variable of type:

ArrayList // stores dice data for the sides of the die

TIP: member variables should have an access level modifier of private to protect the data

Implement method addLetter; the method should:

Add the parameter to the ArrayList representing the letters on the die

Implement method displayAllLetters; the method should:

Use an enhanced for loop to output the letter on each of the six sides of the die

inputOutput package

IReadDataFile interface

Create interface IReadDataFile

Add method signature:

populateData with no return type and an empty parameter list

ReadDataFile class

Update the class so that it implements interface IReadDataFile

TIP: Use Netbeans right click menu, Insert Code, Implement Method to have the IDE generate the methods for you; you will replace the throw exception statements with the source code you write

Add member variables using the specified data types:

Scanner // for reading the file

String // for storing the file name

ArrayList of class String // for storing the data from the file

TIP: member variables should have an access level modifier of private to protect the data

Add a custom constructor that receives one parameter of type String representing the name of the data file to read; it should do the following:

Set the member variable of type String for storing the file name equal to the parameter

Instantiate the member variable of type ArrayList

Add a getter for the ArrayList member variable that stores the data read from the data file

TIP: Use the IDE right click menu, Refactor, Encapsulate Fields and select just getter for the member variable of focus

Implement method populateData; it should do the following:Instantiate an instance of Java API class URL passing as an argument member variable representing the file name of the data file

TIP: this is a unique implementation, to instantiate the instance set the URL variable equal to static method call getClass().getResource()

Instantiate an instance of class File using the URL created above

Initialize member variable of type Scanner based on the File instance created above

TIP: pass as an argument to the constructor the reference object of the URL instance with method call .toURI()

Loop through the data file until the end

Add to the ArrayList representing the data in the file each value read from the data file

import inputOutput.ReadDataFile; import java.util.ArrayList; import java.lang.*;

public class Boggle {

private static ArrayList boggleData = new ArrayList(); private static final String dataFileName = "BoggleData.txt"; private static ArrayList dictionaryData = new ArrayList(); private static final String dictionaryFileName = "Dictionary.txt";

public static void main(String[] args) { ReadDataFile data = new ReadDataFile(dataFileName); data.populateData(); ReadDataFile dictionary = new ReadDataFile(dictionaryFileName); dictionary.populateData(); Board newBoard = new Board(data.getData(), dictionary.getData()); newBoard.populateDice(); int itemCount = dictionaryData.size(); System.out.println("There are " + itemCount + " entries in the dictionary"); }

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;

public class Board implements IBoard{

private static ArrayList boggleData; private static ArrayList dictionaryData; private static ArrayList boggleDice;

public Board(ArrayList data, ArrayList dictionary){ boggleData = data; dictionaryData = dictionary; boggleDice = new ArrayList();

} @Override public ArrayList shakeDice() { throw new UnsupportedOperationException("Not supported yet."); } @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

public interface IDie { static final int NUMBER_OF_SIDES = 6; public String rollDie(); public void addLetter(String letter); public void displayLetters(); }

import java.util.ArrayList; public class Die implements IDie { private ArrayList sides = new ArrayList();

@Override public String rollDie() { throw new UnsupportedOperationException("Not supported yet."); }

@Override public void addLetter(String letter) { sides.add(letter); }

@Override public void displayLetters() { for(String value : sides){ System.out.print("" + value + ""); } } }

public interface IReadDataFile { public void populateData(); }

import java.io.File; import java.net.URL; import java.util.ArrayList; import java.util.Scanner;

public class ReadDataFile implements IReadDataFile {

private Scanner inputFile; private String dataFileName; private ArrayList data;

public ReadDataFile(String fileName) { dataFileName = fileName; data = new ArrayList(); } public ArrayList getData() { int i = 0; for(i=0; iURL url = getClass().getResource("/data/" + dataFileName); File file = new File(url.toURI()); inputFile = new Scanner(file); while(inputFile.hasNextLine()){ String line = inputFile.nextLine(); data.add(line); } inputFile.close(); } catch (Exception e){ } finally { if (inputFile != null) inputFile.close(); } }

public static void main(String args[]){ ReadDataFile r = new ReadDataFile("BoggleData.txt"); r.populateData(); r.getData(); } }

I have two questions. One concerns the board. I dont know what parameters I'm supposed to be passing. Thats the error holding me back.

Also, my file is not opening and reading correctly. The two files are part of a project and when I hover over them it says unrecognized file. I'm not sure if thats how its supposed to be but I'd like to know how I can correct that.

Any help is appreciated

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_2

Step: 3

blur-text-image_3

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

Icdt 88 2nd International Conference On Database Theory Bruges Belgium August 31 September 2 1988 Proceedings Lncs 326

Authors: Marc Gyssens ,Jan Paredaens ,Dirk Van Gucht

1st Edition

3540501711, 978-3540501718

More Books

Students also viewed these Databases questions

Question

d. How were you expected to contribute to family life?

Answered: 1 week ago