lab10-exam2-practice-spec

EECS 183 Lab 10: Exam 2 Practice

Lab attendance Due at your scheduled lab time and day, Thursday or Friday, April 8th or 9th

Lab assignment Due Monday, April 12, 2021, 11:59 pm Eastern

In this lab, you are completing a practice exam with your peers in preparation for Exam 2.

Prerequisite

Attend the discussion with your lab instructor at your assigned lab time. Check here for the lab section Zoom link

You will complete this lab in small groups of about 4 students. For all labs in EECS 183, to receive a grade, every student must individually submit the Lab assingment.

Starter Files

You can download the starter files using this link.

The IDE setup tutorials for Visual Studio and XCode include a video about how to set up a project using the starter files. You can access the tutorials here:

After downloading and unzipping, you will find the following files in the starter-files folder:

IMPORTANT: For this lab, you must include all of the files in a single project in Xcode or Visual Studio.

Lab Assignment

Tasks to Complete

  1. Start a new project with your IDE using the starter files.
  2. For each of the three practice exam questions below:
    • Solve the practice exam question with your group.
    • Transfer your solution to the starter code and submit to the autograder to check your solutiuon. You will submit solutions for these three functions:
    • MonsterArmy::spawnMonster()
    • MonsterArmy::pointsByType()
    • monsters() * The following files will be submitted to the autograder:
    • MonsterArmy.cpp
    • monsters.cpp

NOTE: The questions below appeared on a previous EECS 183 Exam 2.

Classes Definitions

The following class definitions for Monster and MonsterArmy are relevant to the questions on the exam practice.

class Monster {
private:
    // type of Monster, e.g., "FluffyKitten"
    string type;
    
    // amount of hitPoints, e.g., 1000
    int hitPoints;
    
public:
    /**
     * Requires: Nothing.
     * Modifies: type, hitPoints
     * Effects:  Default constructor. Sets hitPoints to 0,
     *           type to ""
    */
    Monster();
    
    /**
     * Requires: Nothing.
     * Modifies: Nothing.
     * Effects:  returns type
    */
    string getType();
    
    /**
     * Requires: Nothing.
     * Modifies: Nothing.
     * Effects:  returns hitPoints
    */
    int getHitPoints();
    
    /**
     * Requires: Nothing.
     * Modifies: type, hitPoints
     * Effects:  Sets hitPoints to points,
     *           type to newType
    */
    void setData(string newType, int points);
};

const int MAX_ARMY_SIZE = 100;

class MonsterArmy {
private:
    // army is an array of Monster
    Monster army[MAX_ARMY_SIZE];
    
    // currentSize is the number of Monster class instances 
    // previosly added to the army array
    int currentSize;
    
public:

    /**
     * Requires: Nothing.
     * Modifies: currentSize
     * Effects:  Default constructor
    **/
    MonsterArmy(); 
    
    /**
     * Requires: type is a non-empty string, points >= 0.
     * Modifies: currentSize, army
     * Effects:  if space is available in the army array, sets the data
     *           of the monster at position currentSize in the array
     *           using the monster type and hit points given.
     *           Returns true if a monster was modified and false
     *           otherwise. currentSize should correctly keep track
     *           of the number of monsters spawned.
     * Note: you may assume the array army has been
     *       initialized with currentSize number of monsters.
    **/
    bool spawnMonster(string type, int points);

    /**
     * Requires: type is a non-empty string
     * Modifies: nothing
     * Effects:  returns the sum of total hit points
     *           for the given Monster type
    **/
    int pointsByType(string type);
};

MonsterArmy::spawnMonster()

Implement the MonsterArmy class spawnMonster() member function below, just as it would appear in the MonsterArmy.cpp file. Assume all headers necessary are already included for you. Only implement the spawnMonster() function here. RME and declaration are included here for reference.

/**
 * Requires: type is a non-empty string, points >= 0.
 * Modifies: currentSize, army
 * Effects:  if space is available in the army array, sets the data
 *           of the monster at position currentSize in the array
 *           using the monster type and hit points given.
 *           Returns true if a monster was modified and false
 *           otherwise. currentSize should correctly keep track
 *           of the number of monsters spawned.
 * Note: you may assume the array army has been
 *       initialized with currentSize number of monsters.
**/
bool spawnMonster(string type, int points);

MonsterArmy::pointsByType()

Implement the MonsterArmy class pointsByType() member function below, just as it would appear in the MonsterArmy.cpp file. Assume all headers necessary are already included for you. Only implement the pointsByType() function here. RME and declaration are included here for reference.

HINT: You must use the class member currentSize for the loop condition for this function, not MAX_ARMY_SIZE.

/**
 * Requires: type is a non-empty string
 * Modifies: nothing
 * Effects:  returns the sum of total hit points
 *           for the given Monster type
**/
int pointsByType(string type);

monsters()

Your last task is to implement a program for a user to build their monster army via keyboard input and compute the total hit points for the demogorgon monster type. Assume that all necessary libraries are included. See below for sample runs of the program.

/**
 * Requires: size > 0
 * Modifies: army, ins
 * Effects:  extracts type and points for each monster from the ins
 *           input stream. The number of monsters to enter is given by
 *           the size parameter.
 */
void buildArmy(MonsterArmy & army, int size, istream & ins) {
    string type;
    int hitPoints = 0;
    int count = 0; 
    while (count < size && ins >> type >> hitPoints) {
        if (army.spawnMonster(type, hitPoints)) {
            count++;
        } else {
            return;
        }
    }
    return;
}

void monsters() {
    // TODO: replace the comments below with statements by filling in the blanks
    // See lab sepcification for sample runs of this function
	
    // __________ armySize = 5;
	
    // __________ << "Enter army size (>0): ";
	
    // __________ >> __________
	
    // __________ army;
	
    // buildArmy(__________, __________ , cin );
	
    // __________ totalHitPoints = army.__________("demogorgon");
	
    // __________ << "Your army of darkness is " << __________;
	
    cout << " demogorgon hit points strong!" << endl;	
    
    return;
}

Sample Runs

Sample run of the program user input is highlighted in bold.

** Sample run 1 ***

Enter army size: 3
Enter each monster for your 3 unit army:
demogorgon 100
vampire 50
fluffyKitten 10
Your army of darkness is 100 demogorgon hit points strong!

** Sample run 2 ***

Enter army size: 4
Enter each monster for your 4 unit army:
demogorgon 100
vampire 50
demogorgon 10
dragon 50
Your army of darkness is 110 demogorgon hit points strong!

How to Submit

IMPORTANT: For all labs in EECS 183, to receive a grade, every student must individually submit the Lab Submission. Late submission for Labs will not be accepted for credit. For this lab, you will receive ten submissions per day with feedback.