0% found this document useful (0 votes)
70 views

Intermedient Programming Midterm

The document defines a Grocery class to represent grocery store items. Each Grocery object stores a name, number of units, and price per unit. Getter and setter methods are provided for each. A totalPrice method calculates the total cost of an item. A printInfo method prints item details. A groceryTax function is defined to calculate sales tax on an array of Grocery objects. It calculates the total cost, applies a 15% tax rate, and returns the tax amount. Three tests of the groceryTax function are included to validate it works as expected for different input arrays.

Uploaded by

Mahira Moazzah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Intermedient Programming Midterm

The document defines a Grocery class to represent grocery store items. Each Grocery object stores a name, number of units, and price per unit. Getter and setter methods are provided for each. A totalPrice method calculates the total cost of an item. A printInfo method prints item details. A groceryTax function is defined to calculate sales tax on an array of Grocery objects. It calculates the total cost, applies a 15% tax rate, and returns the tax amount. Three tests of the groceryTax function are included to validate it works as expected for different input arrays.

Uploaded by

Mahira Moazzah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Question 1: Class Definition (20 points)

Define a class Grocery that represents items you can pick up at the grocery store.
● Each item has a char * name, an integer number of units and a price per unit (float).
● All member variables should be private and you need to write getter and setter methods
for each variable.
● You should also make a Grocery constructor and destructor. The non-empty constructor
(if you make more than one) should take in the item's name and price as parameters.
● Example: you might pick up a can of soup (3.99 and 4 boxes of cereal at 4.99 each.
● Your Grocery class should also implement a function "totalPrice" that calculates the total
price for buying that item. The cereal would return 19.96.
● Implement a printInfo function that has a void return method and prints out information
about the item (name, the units, price per unit, and total cost.

int main() {
// Create instances of the Grocery class
Grocery soup("Soup", 3.99);
Grocery cereal("Cereal", 4.99);

// Set units for the items


soup.setUnits(1);
cereal.setUnits(4);

// Print item information


soup.printInfo();
cereal.printInfo();

return 0;
}

#include <iostream>
#include <string.h>

using namespace std;

class Grocery {
private:
char* name;
int units;
float price;

public:
// Constructor with name and price parameters
Grocery(const char* n, float p) {
name = new char[strlen(n) + 1];
strcpy(name, n);
price = p;
units = 0;
}

// Empty constructor
Grocery() {
name = new char[1];
name[0] = '\0';
price = 0;
units = 0;
}

// Destructor
~Grocery() {
delete[] name;
}

// Getter and Setter for name


void setName(const char* n) {
delete[] name;
name = new char[strlen(n) + 1];
strcpy(name, n);
}

char* getName() const {


return name;
}

// Getter and Setter for units


void setUnits(int u) {
units = u;
}

int getUnits() const {


return units;
}

// Getter and Setter for price


void setPrice(float p) {
price = p;
}
float getPrice() const {
return price;
}

// Function to calculate total price


float totalPrice() const {
return units * price;
}

// Function to print item information


void printInfo() const {
cout << "Name: " << name << endl;
cout << "Units: " << units << endl;
cout << "Price per unit: " << price << endl;
cout << "Total cost: " << totalPrice() << endl;
}
};

Question 2: Tests and Taxes (20 points)


● Write a function (groceryTax) that takes an array of Grocery object pointers and the
array size as parameters. The function will then calculate the total taxes. Let's presume
the tax rate is 15%. You would need to find the total cost of all groceries and then the
taxes would be 15% of that amount.
● Declare an array of Grocery objects (this may include GroceryW objects from the next
section) to test.
● Write a series of at least 3 tests to evaluate if groceryTax works as expected. Tests
could be asserts or they could be something like "bool test1 = (groceryTax(groceryArray)
== 49.52564)".....don't worry about the basic math. So (0.15 * 157) would be ok for the
expected taxes on a $157 grocery bill.

float groceryTax(Grocery** groceryArray, int size) {


float totalCost = 0;

// Calculate total cost of all groceries


for (int i = 0; i < size; i++) {
totalCost += groceryArray[i]->totalPrice();
}

// Calculate tax
float tax = totalCost * 0.15;

return tax;
}
int main() {
// Declare an array of Grocery objects
const int ARRAY_SIZE = 3;
Grocery* groceries[ARRAY_SIZE];
groceries[0] = new Grocery("Soup", 3.99);
groceries[0]->setUnits(2);
groceries[1] = new Grocery("Cereal", 4.99);
groceries[1]->setUnits(3);
groceries[2] = new Grocery("Bread", 2.49);
groceries[2]->setUnits(4);

// Test 1: Calculate taxes for the grocery array


float expectedTax = 4.665;
float actualTax = groceryTax(groceries, ARRAY_SIZE);
if (expectedTax == actualTax) {
cout << "Test 1 passed!" << endl;
}
else {
cout << "Test 1 failed. Expected: " << expectedTax << ", Actual: " << actualTax << endl;
}

// Test 2: Calculate taxes for an empty grocery array


float expectedTax2 = 0;
float actualTax2 = groceryTax(nullptr, 0);
if (expectedTax2 == actualTax2) {
cout << "Test 2 passed!" << endl;
}
else {
cout << "Test 2 failed. Expected: " << expectedTax2 << ", Actual: " << actualTax2 << endl;
}

// Test 3: Calculate taxes for a grocery array with one item


Grocery* singleGrocery[1];
singleGrocery[0] = new Grocery("Milk", 3.49);
singleGrocery[0]->setUnits(1);
float expectedTax3 = 0.52485;
float actualTax3 = groceryTax(singleGrocery, 1);
if (expectedTax3 == actualTax3) {
cout << "Test 3 passed!" << endl;
}
else {
cout << "Test 3 failed. Expected: " << expectedTax3 << ", Actual: " << actualTax3 << endl;
}
// Deallocate memory for Grocery objects
for (int i = 0; i < ARRAY_SIZE; i++) {
delete groceries[i];
}

delete singleGrocery[0];

return 0;
}

You might also like