wpe3B5.jpg (4091 bytes) Question #6(a-c)/ Answer


Question 6a

The following class is defined as part of a project to simulated a vending machine. The machine dispenses
10 different items and gives change.

class VendType
{
private:
  int Quantity[10];	// Amounts of te items currently in machine
  float Price[10];	// Corresponding prices of items.
  float Change;		// Current amount of change available
  bool ChangeAvailable;	// True if machine can give change.

public:
  void Initialise(int NewQuantity[10], float NewPrice[10], float NewChange);
  // Initialises a VendType object with the values in the arrays and sets
  // Change to NewChange. Sets changedAvailable to true if Change is 
  // positive.

  float Supply(int ItemNumber, float MoneyOffered);
  // If MoneyOffered is sufficient to buy the item numbered
  // ItemNumber and there is one in stock, then reduces stock
  // by 1. If the item cann ot be supplied for either reason, returns
  // the value MoneyOffered. Otherwise, returns the necessary
  // change (if available) or zero if no change. 
  // Updates other data as necesary.

  void RefillItem(int ItemNumber, int ItemQuantity);
  // Tops up the given item with the given quantity

  void RefillChange(float MOreChange);
  // Tops up change. Updates other data as necessary.
};

Write that part of the code file for this class which is necessary to implement the Initialise and Supply methods.
Note that you are not asked for definitions of the other two methods.
[7]


Question 6b

The following class is defined as part of a project to simulated a photocopying machine. The machine can
enlarge or reduce and can be set to produce up to 19 copies of the current sheet.

class CopierType
{
private:
  float Ratio;	// The current copying ratio. Must be between 0.71 and 1.41
  int NumCopies; // Number of copies required of current sheet
  int PaperCount; // Current number of sheets of paper available for making copies. Max is 4000.

public:
  void Initialise(int PaperLoaded);
  // Initialises a CopierType. Sets amount of aper to the value of
  // the parameter PaperLoaded, sets Ratio to 1.0 and NumCopies to 1 

  void SetRatio(float NewRatio);
  // Set copy ratio. If NewRatio is out of permitted range setts
  // to max permitted or min permitted as appropriate.
  
  void SetCopies(int CopiesWanted);
  // Sets number of copies. If Copies wanted is out of permitted range, sets 
  // to max permitted or min permitted as appropriate.

  bool CanCopy(void);
  // Returns true if there is enough paper to fulfill copy job, false otherwise.

  void Copy(void);
  // Signals that the machine has done the copying. Updates PaperCount.
};

Write that C++ code file which implements the five methods for this class. [7].


Question 6c

The following C++ code shows part of a class declaration for a theatre seat booking database. The information
shown does not include data about the seats available, it is only that concerned with allowing a number of
agents access to the information. Each agent has a unique identification integer which is positive.

class SeatDatatType
{
private:
  int CurrentWriter;   // Id of agent currently writing information to database
  int IsLocked; // Set true if an agent currently has write access to database
public:
  void Iinitialise(void);
  // Initialises a SeatDataType object. Sets CurrentWriter to zero, IsLocked to false

  bool GetWriteAccess(int AgentId);
  // If AgentId is positive and IsLocked is false, this sets IsLocked true
  // CurrentWriter to AgentId and returns true, otherwise returns false

  bool FreeWriteAccess(int AgentId);
  // If AgentId matches CuurrentWriter, updates data and returns true,
  // else returns false

  bool CanCloseDown(void);
  // Returns true if no agent currently has write access to the database,
  // false otherwise
};

Write the C++ code file which implements the four methods for this class. [7]

first previous key.gif (977 bytes) next last