Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Polymorphism An animal shelter holds only dogs and cats, and operates on a strictly first in, first out basis. People must adopt either the oldest

Polymorphism

An animal shelter holds only dogs and cats, and operates on a strictly "first in, first out" basis. People must adopt either the "oldest" (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data structures to maintain this system and implement operations such as enqueue, dequeueAny, dequeueDog and dequeueCat. Assume each animal has a unique name and thus you don't need to worry. You need to use at most O(n) storage and O(1) for all operations, wherenis the total number of animals in the shelter. Your program should read all ta*.dat in the same directory as your p10.py or p10.ipynb, and generate output for each ta*.dat as the example below. You should use classes for dogs and cats. You need handle whitespace as the whitespace convention and handle errors as always.

Example Input and Output (as comments below) in chronological order:

enqueue(dog, Bark)# put the dog named Bark to shelter

enqueue(cat, Mimi)# put the cat named Mimi to shelter

enqueue(cat, Tiger)# put the cat named Tiger to shelter

dequeue(cat)# the cat named Mimi is adopted

dequeue()# the dog named Bark is adopted

dequeue(dog)# no dog left to be adopted

enqueue(dog, Bark)# put the dog named Bark to shelter

dequeue(dog)# the dog named Bark is adopted

image text in transcribedimage text in transcribedimage text in transcribed
Polymorphism An animal shelter holds only dogs and cats, and operates on a strictly "rst in, rst out" basis. People must adopt either the "oldest" (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specic animal they would like. Create the data structures to maintain this system and implement operations such as enqueue, dequeueAny, dequeueDog and dequeueCat. Assume each animal has a unique name and thus you don't need to worry. You need to use at most O(n) storage and 0(1) for all operations, where n is the total number of animals in the shelter. Your program should read all ta*.dat in the same directory as your p10.py or p10.ipynb, and generate output for each ta*.dat as the example below. You should use classes for dogs and cats. You need handle whitespace as the whitespace convention and handle errors as always. Example input and Output (as comments below) in chronological order: enqueue(dog, Bark) # put the dog named Bark to shelter enqueuelcat, Mimi) # put the cat named Mimi to shelter enqueuelcat, Tiger) # put the cat named Tiger to shelter dequeuelcat) # the cat named Mimi is adopted dequeuel) # the dog named Bark is adopted dequeueldog) # no dog left to be adopted enqueueldog, Bark) # put the dog named Bark to shelter dequeueldog) # the dog named Bark is adopted This is a broadcast email. I have a better solution for p10 as below: You simply keep two lists, one for dog and one for cat. Eeach entry in the two lists contain the name of the pet and a "logical" timestamp, which is just a counter starting with 0 and increment every time an enqueue() is called. When you do dequeue(dog), you just remove the first entry from dog list. When you do dequeue(cat), you just remove the first entry from cat list. When you do dequeue(), i.e., you just want a pet no matter it is a dog or a cat (but you must get the "oldest" one), you just compare the first entry from the two lists, and remove the first one with small timestamp.. Thanks, Ming-Hwa Wang, Ph.D.\f

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions