Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a class Fish, that does the following. it has four attributes: name (string), age (int), weight (float) and alive (bool). age and weight

image

Write a class Fish, that does the following. it has four attributes: name (string), age (int), weight (float) and alive (bool). age and weight default to 1 and 5.0 if not specified, and alive is always set to True when the fish object is created. . It has an str method that outputs "Fish [name] is [age] years old and is [weight]kg.". The weight is formatted to 2 decimal places. It also has a eat method that given another fish, its own weight increases by 90% of the other fish's weight. It also prints "Fish [name] ate [another fish's name]!". However, if the fish's weight exceeds age 15, then the fish dies from overeating. In that case, it prints out "Fish [name] ate too much and died!". If the given fish is already dead, you should print "Cannot eat dead fish!". If the fish is dead, it will print out "Fish [name] is dead." when trying to print it. It will also print "A dead fish cannot eat!" if it tries to eat. Also, if you are eaten, you are also dead. Note: a fish will never be created that the weight exceeds age *15. Fish("Shark", 5, 30.0) Fish ("Tuna", 2, 20.0) For example: Test shark tuna print(shark) print(tuna) shark.eat(tuna) print(shark) Result Fish Shark is 5 years old and is 30.00kg. Fish Tuna is 2 years old and is 20.00kg. Fish Shark ate Tuna! Fish Shark is 5 years old and is 48.00kg. Fish Tuna is dead. print(tuna) shark Fish("Shark", 5, 70.0) tunal Fish("Tunal", 2, 20.0) tuna2 = Fish ("Tuna2", 2, 20.0) shark.eat(tunal) shark.eat (tuna2) print(shark) print(tuna1) print (tuna2) Fish Shark ate Tuna1! Fish Shark ate too much and died! A dead fish cannot eat! Fish Shark is dead. Fish Tunal is dead. Fish Tuna2 is 2 years old and is 20.00kg.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Heres the Fish class implementation according to the described behavior python class Fish def in... 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_2

Step: 3

blur-text-image_3

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

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions