Question: Java Tasks: I need help with Class Dir and Class BoardOptions. Below is an overview of the whole project. Here are the two tasks that

Java Tasks:

I need help with Class Dir and Class BoardOptions. Below is an overview of the whole project.

Here are the two tasks that I am seeking help.

class Dir

This class helps us represent the four directions. Each direction is represented by an object that stores a String representation. As mentioned, the implementation details will lend to a fuller understanding of enumerations. We could have just used a single String directly, but then there's the possibility of any arbitrary String showing up when we only want one of these four objects, so our approach will avoid that.

fields

public final String TEXT. The text version never changes, so it may safely be public final.

public static Dir UP. It may be surprising to see an object of the class itself showing up as a field, but that's possible. This creates a Dir whose TEXT is "up".

public static Dir DOWN. The TEXT is "down".

public static Dir LEFT. The TEXT is "left".

public static Dir RIGHT. The TEXT is "right".

public static Dir[] values. This array of Dir must contain references to the four static fields just defined, in this order: {UP,DOWN,LEFT,RIGHT}.

Manual Inspection Criteria (5%): these five static definitions are all correctly instantiated - values references the other four, it doesn't create more Dir objects.

methods

private Dir(String TEXT). Notice that the only constructor is private! We can call it while inside this class (to make the four objects), but nowhere else, so that guarantees those are the only Dir objects.

Manual Inspection Criteria (5%): the constructor is private (and all submitted code compiles with this, i.e. no attempts to create Dirobjects exist outside of the four expected ones in this class).

@Override public String toString(). Returns the TEXT.

(note: there's no need to define an equals method; the double-equals operator is sufficient, because only these four direction objects will ever exist!)

---------------------------------------Task 2:------------------

class BoardOptions

This class mostly just groups together eight different values that all relate to a particular Board's properties. However, pairing the default values with the class's withX methods allows us to easily create a Board with only minimal effort. As an example, here's a BoardOptions which describes a Board with the standard 4x4 dimensions, but the tiles are each 2x3 instead of 4x4.

new BoardOptions().withTileHeight(2).withTileWidth(3) 

We can call those withX methods in any order, chained together, to get the settings right, because every one of them both changes the one requested value and then also returns a reference to the overall BoardOptions object itself, ready to call another method on it.

This class's code will be pretty repetitive - lots of very short definitions.

fields

private char spacerV. The character used to draw the "vertical" lines of the whole-board representation. Default value: '|'.

private char spacerH. The character used to draw the "horizontal" lines of the whole-board representation. Default value: '-'.

private char spacerX. The character used to draw the "intersection" parts of the whole-board representation. Default value: '+'.

private int numRows. The number of rows of the board. Default value: 4.

private int numCols. The number of columns of the board. Default value: 4.

private int tileHeight. The height of each tile on the board. Default value: 1.

private int tileWidth. The width of each tile on the board. Default value: 2.

private int gapID. The id of the special Tile that represents the open spot on the puzzle; since we number the tiles from 1 to (numRows * numCols), it needs to be maintained to actually be equal to numRows*numCols.

Manual Inspection Criteria (5%): all fields are given default values in their definitions, not via constructor definitions/overloading.

methods

First, we have "getters" of all our private fields; they just return a copy of the value.

public char getSpacerV().

public char getSpacerH().

public char getSpacerX().

public int getNumRows().

public int getNumCols().

public int getTileHeight().

public int getTileWidth().

public int getGapID().

Our first constructor is effectively just the default constructor: it accepts no parameters, and relies upon the defaults already given in each field definition (above). The second constructor manually accepts parameters for every single setting.

public BoardOptions(). Relies on all default values above.

public BoardOptions(char spacerV, char spacerH, char spacerX, int numRows, int numCols, int tileHeight, int tileWidth, int gapID). Basic constructor that assigns each field to the same-named incoming parameter's value.

Almost lastly, we have the "update" methods, which both change one setting and also return a reference to this overall BoardOptions object, allowing us to chain calls to these methods as shown at the class's introduction. They should feel like parts of a constructor call, in a way.

public BoardOptions withSpacerV(char spacerV).

public BoardOptions withSpacerH(char spacerH).

public BoardOptions withSpacerX(char spacerX).

public BoardOptions withNumRows(int numRows). Must also update gapID to again equal numRows * numCols.

public BoardOptions withNumCols(int numCols). Must also update gapID to again equal numRows * numCols.

public BoardOptions withTileHeight(int tileHeight).

public BoardOptions withTileWidth(int tileWidth).

public BoardOptions withGapID(int gapID). Note - since withNumRows/withNumCols automatically updates the gapID in general, if you want a unique gap location, this needs to be after those with-calls; using it last is suggested.

Manual Inspection Criteria (5%): all these withX methods modify this object, they don't create any brand - new objects.

Lastly lastly, toString and equals definitions.

@Override public String toString(). Returns a String based on all the fields, which matches this pattern (which is using all default values, and contains three space characters):

BoardOptions(board:4x4, tiles:1x2, spacers:|-+, gap:#16) 

@Override public boolean equals(Object other). other must be a BoardOptions object, and all fields of the two objects must match.

----------------Here are the testers--------

Tester for class Dir: https://paste.ee/p/78EjK

Tester for class BoardOptions: https://paste.ee/p/qc3iJ

Thank you very much.

Overview These classes play a role in our project: Board: mostly contains a 2D array of Tile objects Tile: representing one movable piece of our puzzle. (The empty space is also represented as a special Tile piece). Boardoptions: a somewhat unusual way to represent all the options of our puzzle game, with flexible ways to update. coord: a simple representation of a row/column position. Dir: representations of up, down, left, and right; this will give you great insight into how enum definitions work in Java later on (hint they're just special classes that have this same structure enforced upon them). D grid, showing 3x4 board. (This isn't Java, just a diagram). We will introduce these classes from simpler/easier to larger/more difficult. Working through the project in the given order is a soft recommendation

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!