Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

In Java please define the following class. this class is one of many used to create a tile sliding game where there is one less

In Java please define the following class. this class is one of many used to create a tile sliding game where there is one less tile than the total number of spaces on the board.

comment thought process and only add methods as needed but please try to limit usage to only the provided methods.

class BoardOptions- somewhat unusual way to represent all the options of our puzzle game, with flexible ways to update.

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions