Download a JBuilder project containing code to get you started: monopoly.zip. The project contains two packages.
monopoly.guicontains a graphical user interface (view and controllers) whilemonopoly.modelwill contain the model. Your early work will be exclusively in the model.About
monopoly.guiThe code in
monopoly.guiis complete and working. You should not modify it in any way until after your program plays a legal game. In particular, most of the classes and methods havepackagevisibility rather thanpublicvisibility. You are not permitted to change this.The user interface,
monopoly.gui, requires that the model contain the following public classes with the given public methods:
Class/Method Description Square A class representing a square on the board such as "Vermont Ave." or "Reading Railroad" or "Go". getDescription Returns a string describing the property. The string should contain newline characters ("\n") to format it into appropriate lines. getPlayers Returns an array containing the Players currently on this property.addObserver A method to add an observer that responds to changes to the property object. This code is provided. Player A class representing a player in the game. getName Returns a string containing the name of the player. getOwnedPropertyNames Returns an array filled with the names of the properties owned by this player. getBalance Returns an integer with the player's current bank balance. getNetWorth Returns an integer with the player's current net worth. canBuyOccupiedSquare Returns true if the player can buy the property they have landed on; false otherwise. buyHouse Instructs the player to purchase a house for a property. buyOccupiedSquare Instructs the player to purchase the property they just landed on. addObserver A method to add an observer that responds to changes to the property object. This code is provided. Monopoly A class representing the entire game. playGame A method that plays the game. When this method finishes, the game is over. getNumPlayers Returns the number of players in the game. getNumSquares Returns the number of squares on the board. getPlayer Returns the Playerobject specified by a parameter.getSquare Returns the Squareobject specified by a parameter.About
monopoly.modelThe model is where you will spend the vast majority of your time in the first part of the project. The provided code includes the classes required by the interface and stubs for the methods the user interface calls. A stub is just enough for the program to compile -- if the method is a command, it will be empty; if it's a query, it will return a constant such as
false,0ornull.A class diagram showing the classes and methods required by the user interface is shown below. You will need to add several more classes and methods to the existing classes to make the program work as required.
![]()
Communication between the model and the user interface
Every time the model changes, the view (the display portion of the user interface) must update the information it displays. The view is notified of changes in the model when the model calls the view's
updatemethod. The view then queries the model for its current state and redisplays itself. That is, it uses queries such asgetBalanceandgetOwnedPropertyNamesto find out what information it should display to the user.The model is not one big thing -- it contains many distinct parts. There are a relatively small number of
Playerobjects and a larger number ofSquareobjects. Similarly, the user interface is not a single, massive, view. Parts of it are dedicated to displaying players while other parts are dedicated to displaying squares. Each part of the model (eachPlayerobject and eachSquareobject) keeps a list of observers that are concerned about changes to thatPlayerorSquareobject. Each observer has a method namedupdate. When a model object changes, it should call theupdatemethod in each of the observers on its list. A view adds itself to the list by calling theaddObservermethod inPlayerorSquare.The provided code includes a method,
updateObservers(note the "s" at the end of the name), that calls theupdatemethod in each of the registered listeners. Call this method whenever the model changes. For example, a method to change the bank balance might be:public void adjustBalance(int amt) { this.bankBalance += amt; this.updateObservers(this, null); }The first parameter to
updateObserversis always the model object that changed. The second parameter can included additional information to be used by the view. It is often set tonull.One of the registered observers might be a
PlayerViewobject. It has a method similar to this:public void update(Object model, Object changeInfo) { if (model instanceof Player) { Player p = (Player)model; this.balanceLabel.setText("$" + p.getBalance()); this.netWorthLabel.setText("$" + p.getNetWorth()); ... } }