CSCI 2300 | Data Structures and Algorithms | Spring 2000 |
In this lab we will explore some aspects of program design using project1 . Since all of you have already submitted your solutions to this project, this lab will review the elements of good class design, which can greatly simplify the complexity of the code. The main data structure used in this project is the Linked-List, which everyone understands. The difficulty of the code comes from the complex interactions among the different objects in the program, which include Animals, Cages, the scheduled and waiting list, and the List of Cages. Careful design, by paying attention to the functionality to be coded within each object, is crucial in successfully solving this project. This also happened to be the problem that most of you faced, i.e., how to untangle the links among the various objects.
Let's see what a good program design can do for the simplicity and understandibility
of your code.
Work through the following steps, in order:
Now write the code for the Scheduler::Schedule routine. A skeleton of
the code is provided for you. You must iterate over each cage and first
check if the animal is within the weight requirements for that cage. If
yes, set in_weight_range to true, and try to schedule the animal
in that cage. If the animal is scheduled then also set in_schedule to true.
You should not have to write extensive code for this; use the routines
already provided for you in the different classes.
(note: remember to call animal.AssignCage( CAGE NUM) to store the cage
number where the animal is scheduled for a successful insertion)
After we exit the iteration, we check if in_scheduled is true and push the animal in the scheduled list. Otherwise, we check if in_weigth_range is true, and push the animal in the waiting list (in this case the animal is within range but no cage has required dates free). Finally, if both the flags are false, it means that the animal did not fit into any cage. Note the power of modular objects. In the Schedule function we need to worry only about how we can use the functionality provided by the different classes, without having to worry how they are implemented. The greatly simplifies the coding, especially for large projects.
After writing the code test your program on the cages.txt
file, with the sample input file input.txt.
Ignore the cancel command when you test.
After writing the code test your program on the cages.txt
file, with the sample input file input.txt.
After trying each cage, we check if the in_schedule flag is true. If yes, it means that the revision was successful, and some dates may now be free. So we have to try to see if other animals can be scheduled from the waiting list. On the other hand if in_schedule is false, it means that the revision was not successful, and we have to restore the old status. We reschedule the old animal in old cage (using the temporary values stored earlier). Now write the code that accomplishes both these tasks. i.e., try from waiting list if any of the dates previously occupied by the animal are now free or put the old animal back in its cage if revision was not successful.
After writing the code test your program on the cages.txt
file, with the sample input file
input2.txt .
Delete the following files from your project
Include the following file in your projectCarefully study the file Hash.h and its public interface.
Next look at the new Scheduler::Schedule routine which has been suitably modified to use the public interface of HashTable. Note that HashTable doesn't provide a push_back function, but instead provides an Insert that takes the Key (animal name) and the ElementType (animal) as its arguments.
Next look at how we have modified the Scheduler::InSchedule routine. It uses the Find function of HashTable instead of the list find from STL. The HashTable::Find returns a pointer to a <Key, ElementType> pair. This is typedef as entry_ptr in Hash.h. In our case entry_ptr for scheduled_ and waiting_ lists points to <string, Animal> pair. If ep is of type entry_ptr, then ep->second returns the second element of the pair, i.e., the Animal. We use this to get to the Animal returned by the Find routine.
Finally study the new Scheduler::TryFromWaitList routine. Notice how
instead of directly using the STL iterator class to iterate through
all the animals in the waiting_ list, we use the function HashTable::ToVector
to get a vector of all the animals in the waiting list. We then iterate
over this vector of exported_type. Make sure you understand what's going
on here.
Uncomment the code within the Scheduler::Cancel routine. Make appropriate
changes so that the code uses the HashTable interface. Test your program
on the input input.txt
Next uncomment the code within the Scheduler::Change routine.
Make appropriate changes so that the code uses the HashTable interface.
Test your program on the input input2.txt