Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Are you comparable? Points: 95% A useless overview stop comparing yourself to others. Smert fum awesome also awesome In Objectville, cartoons like the above are

image text in transcribed
image text in transcribed
image text in transcribed
Are you comparable? Points: 95% A useless overview stop comparing yourself to others. Smert fum awesome also awesome 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! 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 al], int size) for (int i = 0; i compareTo(*a[3+2]) == 1) swap (as), a[5+2]): 1 It also wrote the following code that checks it a group of objects are equal: bool all_same(Comparable a[], int size) { for (int i = 0; i compareTo(*a[i+1])!=) return false; return true; 1 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 compare to method The compare to method returns +1 (larger) -1 (smaller) or o (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 nparable 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 Fans Sering ca Bergetica AN model Brattester og best of 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 compare to 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! Are you comparable? Points: 95% A useless overview stop comparing yourself to others. Smert fum awesome also awesome 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! 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 al], int size) for (int i = 0; i compareTo(*a[3+2]) == 1) swap (as), a[5+2]): 1 It also wrote the following code that checks it a group of objects are equal: bool all_same(Comparable a[], int size) { for (int i = 0; i compareTo(*a[i+1])!=) return false; return true; 1 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 compare to method The compare to method returns +1 (larger) -1 (smaller) or o (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 nparable 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 Fans Sering ca Bergetica AN model Brattester og best of 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 compare to 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

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

Multidimensional Array Data Management In Databases

Authors: Florin Rusu

1st Edition

1638281483, 978-1638281481

More Books

Students also viewed these Databases questions

Question

Prepare an ID card of the continent Antarctica?

Answered: 1 week ago

Question

What do you understand by Mendeleev's periodic table

Answered: 1 week ago

Question

Evaluating Group Performance?

Answered: 1 week ago