Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A useless overview In Objectville, cartoons like the above are taboo. Objects must be able to compare themselves to others! Otherwise, putting them into order

A useless overview

In Objectville, cartoons like the above are taboo. Objects must be able to compare themselves to others! Otherwise, putting them into order won't be possible! Not only that, but checking if they are in order won't be possible and checking if they are unique won't be possible. What if two objects are identical and we have no way of telling that? Simply put, objects in Objectville must be comparable!

Meet the objects

There are 3 different classes of objects in Objectville: Slacker, Workaholic and Work-Family-Balanced objects.

  • Slackers compare themselves to each other by the number of classes they missed in college (the more the better).

  • Workaholics compare themselves to each other by the number of hours they work per day (the more the better).

  • Work-family-balanced objects compare themselves to each other by the absolute difference between their work and family hours. The smaller the difference, the better.

The code for the these three classes is given.

Slackers die if they get compared to Workaholics or Work-Family-Balanced objects. Similarly, Work-Family-Balanced objects can't stand being compared to Slackers or Workaholics. The case is also not different for how Workaholics consider comparison to objects of the other two classes.

What a nation!

Meet the government

The government in Objectville is not authoritarian. Although it sorts its objects, it defers to each class of objects the decision of how they should be compared. If a class does not want its objects to be compared to objects of other classes, then be it (the government won't interfere!)

The government wrote the following code to sort objects, regardless of their class:

void sort(Comparable * a[], int size) { for (int i = 0; i < size - 1; i++) for (int j = 0; j < size - 1 - i; j++) if (a[j]->compareTo(*a[j+1]) == 1) swap(a[j], a[j+1]); }

It also wrote the following code that checks if a group of objects are equal:

bool all_same(Comparable * a[], int size) { for (int i = 0; i < size-1; i++) if (a[i]->compareTo(*a[i+1]) != 0) return false; return true; }

Two slackers are the same if they missed the same number of classes in college. Two Workaholics are the same if they work the same number of hours per day. Two Work-Family-Balanced objects are the same if the absolute difference between their work and family hours is the same.

It is clear from the code written by the government that all objects must be comparable! Each object must know how to compare itself to other objects (of the same class) using the compareTo method.

The compareTo method returns +1 (larger), -1 (smaller) or 0 (equal).

Your role in Objectville

You have been assigned the task of ensuring that the code written by the government works without touching it! If someone has to change, then it is the citizens of Objectville, not the government. Therefore,

  • you are allowed to add code to the Slacker, Workaholic and WorkFamilyBalanced classes.

  • You are allowed to add new classes to Objectville to help in making the three available classes comparable.

  • You are allowed to add new non-member functions.

However,

  • you are not allowed to remove anything that is already in the three classes.

  • You are also not allowed to add, remove or change any code that is in main(), sort() or all_same() (this is code written by the government!).

You will be considered successful, if you manage to make the given code run and produce the expected output:

The error "terminate called ... " might be different on platforms other than Ed. However, regardless of what platform you use, the program must terminate with an error.

Hints

  • a[i]->compareTo(*a[i+1]) The above line appears in both sort and all_same. Look at at it carefully and infer the following: Does compareTo take an object, a reference or a pointer?

  • Everything we have studied about base class pointers and derived class pointers applies also to base class references and derived class references.

  • dynamic_cast returns NULL if the cast fails when trying to cast pointers. dynamic_cast throws a runtime error if the cast fails when trying to cast references!

#include #include using namespace std;

class Slacker { string name; int classesMissed; public: Slacker(int missed, string n): classesMissed(missed), name(n) {} // they don't even bother to validate their input! string toString() const { return name + " missed " + to_string(classesMissed) + " classes"; } };

class Workaholic { int workHours; // they don't have a name ... work is more important! public: Workaholic(int wh) { assert(wh > 12 && wh < 25); workHours = wh; } string toString() const { return "Workaholic worked " + to_string(workHours) + " hours non-stop!"; } };

class WorkFamilyBalanced { string name; int familyHours; int workHours; public: WorkFamilyBalanced(int fh, int wh, string n): name(n) { assert(fh >= 0 && wh >= 0); familyHours = fh; workHours = wh; } string toString() const { int diff = abs(workHours - familyHours);

return name + " spent " + to_string(workHours) + " hours at work" + " and " + to_string(familyHours) + " with family " + "(diff = " + to_string(diff) + ")"; } };

//------------------------------------------------------------------------- void sort(Comparable * a[], int size) { for (int i = 0; i < size - 1; i++) for (int j = 0; j < size - 1 - i; j++) if (a[j]->compareTo(*a[j+1]) == 1) swap(a[j], a[j+1]); }

bool all_same(Comparable * a[], int size) { for (int i = 0; i < size - 1; i++) if (a[i]->compareTo(*a[i+1]) != 0) return false; return true; } //-------------------------------------------------------------------------

int main() { cout << "Sorting 3 slackers (based on the number of classes missed)." << endl; Comparable * a[3] = { new Slacker(81, "Jimmy"), new Slacker(50, "Tania"), new Slacker(73, "Josh") };

sort(a, 3); for (int i = 0; i < 3; i++) cout << dynamic_cast(a[i])->toString() << endl;

if (all_same(a, 3)) cout << "All the slackers are the same!" << endl; else cout << "Not all slackers are the same!" << endl; if (is_sorted(a, 3)) cout << "The slackers are sorted!" << endl; else cout << "The slackers are NOT sorted!" << endl; //-------------------------------------------------------------------------

cout << " Sorting 3 workaholics (based on their work time)." << endl; Comparable * b[3] = { new Workaholic(23), new Workaholic(23), new Workaholic(23) };

sort(b, 3); for (int i = 0; i < 3; i++) cout << dynamic_cast(b[i])->toString() << endl;

if (all_same(b, 3)) cout << "All workaholics are the same!" << endl; else cout << "Not all workaholics are the same!" << endl; if (is_sorted(b, 3)) cout << "The workaholics are sorted!" << endl; else cout << "The workaholics are NOT sorted!" << endl; //-------------------------------------------------------------------------

cout << " Sorting 3 work-family-balanced objects " << endl << "(based on the absolute difference between work and family time)." << endl; Comparable * c[3] = { new WorkFamilyBalanced(8, 8, "Ahmad"), new WorkFamilyBalanced(7, 12, "Fatima"), new WorkFamilyBalanced(9, 8, "Salwa") };

sort(c, 3); for (int i = 0; i < 3; i++) cout << dynamic_cast(c[i])->toString() << endl;

if (all_same(c, 3)) cout << "All the balanced objects are the same!" << endl; else cout << "The balanced objects are not all the same!" << endl; if (is_sorted(c, 3)) cout << "The balanced objects are sorted!" << endl; else cout << "The balanced objects are NOT sorted!" << endl; //-------------------------------------------------------------------------

cout << " Attempting to sort objects of mixed classes." << endl; Comparable * d[4] = { new Slacker(100, "John"), new Workaholic(24), new WorkFamilyBalanced(2, 2, "Ahmad") };

sort(d, 4); cout << "IF THIS LINE IS REACHED, YOUR PROGRAM IS INCORRECT!" << endl << "REACHING THIS LINE MEANS THAT OBJECTS OF DIFFERENT " << endl << "CLASS HAVE BEEN COMPRED, WHICH IS STRICTLY FORBIDDEN " << endl << "IN OBJECTVILLE!" << endl;

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Discuss the Rights issue procedure in detail.

Answered: 1 week ago

Question

Discuss the Rights issue procedure in detail.

Answered: 1 week ago

Question

Explain the procedure for valuation of shares.

Answered: 1 week ago

Question

Which months of this year 5 Mondays ?

Answered: 1 week ago

Question

Define Leap year?

Answered: 1 week ago