A7 Classes - sheerazwalid/COMP-I GitHub Wiki

Assignment Folder

Place source code files for this assignment in a folder named classes within your Cloud9 workspace.

Exercise 1: Rectangle class (20 points)

Implement a rectangle class in accordance with the following UML class diagram.

+---------------------------------------------------+
|                                                   |
|                    Rectangle                      |
|                                                   |
+---------------------------------------------------+
|                                                   |
| - width : double                                  |
| - height : double                                 |
|                                                   |
+---------------------------------------------------+
|                                                   |
| + Rectangle(width : double, height : double)      |
| + area() : double                                 |
| + isSquare() : bool                               |
|                                                   |
+---------------------------------------------------+

The rectangle class should be useable in the following program. Include this program in your submitted work.

int main() {
    Rectangle foo(3, 4);  // Create a rectangle with width of 3 and height of 4.
    Rectangle bar(5, 5);  // Create a rectangle with width of 5 and height of 5.
    cout << "The area of rectangle foo is: " << foo.area() << endl;
    cout << "The area of rectangle bar is: " << bar.area() << endl;
    assert(foo.isSquare() == false);
    assert(bar.isSquare() == true);
    return 0;
}

Exercise 2: Number class (20 points)

Implement a number class to represent integers in accordance with the following UML class diagram.

+---------------------------------------------------+
|                                                   |
|                      Number                       |
|                                                   |
+---------------------------------------------------+
|                                                   |
| - n : int                                         |
|                                                   |
+---------------------------------------------------+
|                                                   |
| + Number(n : int)                                 |
| + setValue(n : int)                               |
| + isPrime() : bool                                |
| + isLucky() : bool                                |
|                                                   |
+---------------------------------------------------+

The number class represents integers. The integer value it represents is passed into the constructor and stored in member variable n. The setValue function takes an integer argument, which it copies into it's private member variable n. The isPrime function returns true if member variable n is prime and false if not prime. The isLucky function returns true if the member variable n is divisible by 7.

In the main function of your program, provide test code for the isPrime and isLucky functions. Use assert statements for this purpose. Make sure that your test code executes every line of code in the 2 functions.

Submit your work

Submit your work by sending the url of your Cloud9 workspace to the teaching assistant and CC me. The subject line of your email should be 201 Assignment: Classes.

Example quiz questions

Number Class

Instances of the Number class represent integers. The integer value they represent is passed into the constructor and stored in member variable n. The setValue function takes an integer argument, which it copies into it's private member variable n. The isPrime function returns true if member variable n is prime and false if not prime. The UML class diagram for the number class is as follows.

+---------------------------------------------------+
|                                                   |
|                      Number                       |
|                                                   |
+---------------------------------------------------+
|                                                   |
| - n : int                                         |
|                                                   |
+---------------------------------------------------+
|                                                   |
| + Number(n : int)                                 |
| + setValue(n : int)                               |
| + isPrime() : bool                                |
|                                                   |
+---------------------------------------------------+
  1. Provide an implementation of the constructor for the Number class.

  2. Provide an implementation of the setValue function in the Number class.

  3. Provide an implementation of the isPrime function for the Number class.

  4. Provide test code for the isPrime function of the Number class. Use assert statements for this purpose. Make sure that your test code executes every line of code in the function.

BeanJar Class

The declaration of the BeanJar class is given below. Instances of the BeanJar class represent bean jars of varying capacity. The member variable maxBeans represents the capacity of the jar. The member variable beans represents the current number of beans in the jar, which must be less than or equal to maxBeans. The class provides functions to add beans, remove all beans, and get the number of beans that are currently in the bean jar. The functions to add beans returns a boolean value that indicates whether the operation succeeded or failed. When the addBeans function fails, the number of beans in the jar will not have been changed. For example, if a bean jar has a capacity of 10 beans and there are 9 beans currently in the jar, then addBeans(2) will return false and the number of beans in the jar will remain at 9. The removeAllBeans function simply sets the number of beans in the jar to zero.

class BeanJar {
public:
    BeanJar(int maxBeans, int beans);
    int getBeans() const;
    bool addBeans(int beans); 
    void removeAllBeans(); 

private:
    int maxBeans;
    int beans;
};
  1. Draw the UML class diagram for the BeanJar class.

  2. Provide an implementation of the BeanJar constructor.

  3. Provide an implementation of the getBeans function.

  4. Provide an implementation of the removeAllBeans function.

  5. Provide an implementation of the addBeans function.

  6. Develop test code to test the addBeans function. Make sure the test code provides good coverage in the sense that it executes all lines of code in the addBeans function. Use the assert function in your test code.

Egg Carton Class

The UML class diagram for the EggCarton class is shown below. Instances of this class represent egg cartons that can hold up to 12 eggs. The class constructor takes two arguments: the initial number of eggs that will be in the carton and the weight in grams of each egg. The class provides functions to get eggs, add eggs and get the total weight. The getEggs function returns the number of eggs in the carton. The addEggs function increases the number of eggs in the carton by the amount passed in as an argument to the function. If the number of eggs would exceed 12, the addEggs function will not change the number of eggs in the carton and will return false to indicate that the operation did not succeed. If the number of eggs would remain less than or equal to 12, then the addEggs functions adds those eggs to the carton and returns true to indicate a successful operation. The getTotalWeight function returns the total weight of the eggs in the carton, which is the product of grams per egg and the number of eggs in the carton.

+----------------------------------------------------------------+
|                                                                |
|                           EggCarton                            |
|                                                                |
+----------------------------------------------------------------+
|                                                                |
| - eggs : int                                                   |
| - gramsPerEgg : double                                         |
|                                                                |
+----------------------------------------------------------------+
|                                                                |
| + EggCarton(eggs : int, gramsPerEgg : double)                  |
| + getEggs() : int                                              |
| + addEggs(eggs : int) : bool                                   |
| + getTotalWeight() : double                                    |
|                                                                |
+----------------------------------------------------------------+
  1. Provide an implementation of the EggCarton constructor.

  2. Provide an implementation of the getEggs function.

  3. Provide an implementation of the addEggs function.

  4. Provide an implementation of the getTotalWeight function.

  5. Develop test code to test the addEggs function. Make sure the test code provides good coverage in the sense that it executes all lines of code in the addEggs function. Use the assert function in your test code.