Question: Write a class encapsulating a board game, which inherits from Game. A board game has the following additional attributes: the number of players and whether
Write a class encapsulating a board game, which inherits from Game. A board game has the following additional attributes: the number of players and whether the game can end in a tie. Code the constructor and the toString method of the new class. You also need to include a client class to test your code.
Here is the code for the Game class:
public class Game { private String description;
/** * Overloaded constructor:
* Allows client to set beginning value for description * This constructor takes one parameter
* Calls mutator methods * @param newDescription the description of the game */ public Game( String newDescription ) { setDescription( newDescription ); }
/** getDescription method * @return a String, the description of the game */ public String getDescription( ) { return description; }
/** * Mutator method:
* Allows client to set value of description * @param newDescription the new description for the game */ public void setDescription( String newDescription ) { description = newDescription; }
/** * @return a string representation of the game */ public String toString( ) { return( "description: " + description ); }
/** * equals method * Compares two Game objects for the same field value * @param o another Game object * @return a boolean, true if this object * has the same field value as the parameter g */ public boolean equals( Object o ) { if ( ! ( o instanceof Game ) ) return false; else { Game g = (Game) o; return ( description.equals( g.description ) ); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
