Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

BoggleData.txt: 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

ID: 3847211 • Letter: B

Question

BoggleData.txt:

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

just contains a dictionary of words from the boggle dictionary.

Assignment Scope

Adding member variables or instance variable in a class

Generating getters/setters for member variables

Instantiating an instance of a class

Calling a method of a class using the reference object

Using Java API class ArrayList

Reading a data fileUsing a try/catch/finally block to handle exceptions and close resource files

IOException

URISyntaxException

Using Java API class Scanner

Using Java API class URL

Using Java API class File

Saving data from a data file in an ArrayList

Adding method signatures to an interface

Adding a constant to an interface

Implementing an interface

Using the enhanced for loop

Deliverables

To complete this assignment you must submit your compressed Netbeans project to Webcourses.

Tasks and Rubric

Activity

Boggle project

Boggle class

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”

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

core package

IBoard interface

NUMBER_OF_DICE equal to value 16

GRID equal to value 4

populateDice with return type void and an empty parameter list

shakeDice with return type ArrayList and an empty parameter list

Board class

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

ArrayList of class String // stores dice data

ArrayList of class String // stores dictionary data

ArrayList of class Die // stores 16 game dice

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.

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

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

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

NUMBER_OF_SIDES equal to value 6

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

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

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

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

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

populateData with no return type and an empty parameter list

ReadDataFile class

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

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

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

Instantiate the member variable of type ArrayList

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

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

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

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

userInterface package

Boggle application

Test Case 1

Test Case 1 passes

Test Case 2

Test Case 2 passes

Test Case 3

Test Case 3 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

displayLetters should look like figure 2        

Test case 3

Run application

Size of ArrayList storing dictionary data should be similar to figure 3

Figure 1 Output from method displayLetters

Die 1: D R L X E I
Die 2: C P O H S A
Die 3: N H N L Z R
Die 4: W T O O T A
Die 5: I O S S E T
Die 6: N W E G H E
Die 7: B O O J A B
Die 8: U I E N E S
Die 9: P S A F K F
Die 10: I U N H M Qu
Die 11: Y R D V E L
Die 12: V E H W H R
Die 13: I O T M U C
Die 14: T Y E L T R
Die 15: S T I T Y D
Die 6: A G A E E N

Figure 2 Output for number of entries in the Dictionary.txt file

There are 62406 entries in the dictionary.

Activity

Boggle project

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

core package

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

userInterface package

Boggle application

Test Case 1

Test Case 1 passes

Test Case 2

Test Case 2 passes

Test Case 3

Test Case 3 passes

Source compiles with no errors

Source runs with no errors

Source includes comments

Total

Explanation / Answer

Boggle.java

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class Boggle
{
   ArrayList<String> diceData;
   String diceFileName = "BoggleData.txt";
   ArrayList<String> dictionaryData;
   String dictFileName = "Dictionary.txt";
   public static void main(String a[]) throws FileNotFoundException
   {
       Boggle bg = new Boggle();
       ReadDataFile rdfBoggle = new ReadDataFile(bg.diceFileName);
       rdfBoggle.populateData();
       ReadDataFile rdfDict = new ReadDataFile(bg.dictFileName);
       rdfDict.populateData();
      
       Board board = new Board(rdfBoggle.getData(),rdfDict.getData());
       board.populateDice();
       //output no.of objects in ArrayList<String> dictionaryData
   }
}

IBoard.java

import java.util.ArrayList;
public interface IBoard
{
   final int NUMBER_OF_DICE = 16;
   final int GRID = 4;
   void populateDice();
   ArrayList shakeDice();
}

Board.java

import java.util.ArrayList;
public class Board implements IBoard
{
   ArrayList<String> diceData;
   ArrayList<String> dictionaryData;
   ArrayList<Die> dieStore;
   public Board(ArrayList<String> diceData,ArrayList<String> dictionaryData)
   {
       this.diceData = diceData;
       this.dictionaryData = dictionaryData;
       this.dieStore = new ArrayList<Die>();
   }
   public void populateDice()
   {
       Die die;
       int index,temp=0;
       for(index=0;index<NUMBER_OF_DICE;index++)
       {
           die = new Die();
           for(int i=0;i<die.NUMBER_OF_SIDES;i++)
           {
               die.addLetter(diceData.get(temp++));
           }
           System.out.print("Die "+(index+1)+": ");
           die.displayLetters();
           dieStore.add(die);
       }
   }
   public ArrayList shakeDice()
   {
       return new ArrayList();
   }
}

IDie.java

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

Die.java

import java.util.ArrayList;
public class Die implements IDie
{
   private ArrayList<String> diceData = new ArrayList<String>();
   public String rollDie()
   {
       return "";
   }
   public void addLetter(String letter)
   {
       diceData.add(letter);
   }
   public void displayLetters()
   {
       for(String letter:diceData)
       System.out.print(letter+" ");
       System.out.println();
   }
}

IReadDataFile.java

public interface IReadDataFile
{
   void populateData();
}

ReadDataFile.java

import java.util.Scanner;
import java.util.ArrayList;
import java.net.URL;
import java.net.URISyntaxException;
import java.io.*;
public class ReadDataFile implements IReadDataFile
{
   private Scanner input;
   private String fileName;
   private ArrayList<String> fileData;
   public ReadDataFile(String fileName)
   {
       this.fileName = fileName;
       fileData = new ArrayList<String>();
   }
   public ArrayList<String> getData()
   {
       return fileData;
   }
   public void populateData()
   {
       URL url;
       File file=null;
       try
       {
           url = getClass().getResource(fileName);
           file = new File(url.toURI());
           input = new Scanner(file);
           while(input.hasNextLine())
           {
               String line = input.nextLine();
               String letters[] = line.split(" ");
               for(String letter:letters)
               fileData.add(letter);
           }
       }
       catch(URISyntaxException e)
       {
           System.out.println("Exception in URI");
       }
       catch(IOException e)
       {
           System.out.println("Exception in input data file");
       }
      
   }
}