Question: ***PYTHON*** 3) We also need an aquarium to hold our fish. To do this, we need to define a second class, named Aquarium. An Aquarium

***PYTHON***

3) We also need an aquarium to hold our fish. To do this, we need to define a second class, named Aquarium. An Aquarium has two instance variables: a list of Fish instances, and an integer representing the water capacity of the tank in gallons (each Fish requires 1 gallon of water). An Aquarium also has the following methods:

__init__(self, capacity)

This method creates a new instance variable that is an empty list; this list will eventually be filled with Fish objects. It also assigns its second parameter to a new instance variable representing the Aquariums total capacity in gallons.

__repr__(self) This method returns a single string that contains the string representations of the Fish in the Aquariums internal list, in the same order that they appear in that list. Insert tab ("nt") and newline ("nn") characters to ensure that, when this string is displayed, no more than two Fish are printed per line (if there are an odd number of fish, the last one will be printed on a line by itself). For example, a call to this method might return a string like

"<>< (Bill, red Gourami)nt<>< (Ted, yellow Goldfish)nn<>< (Mary, pink Mollie)".

Note that you can call str() on a Fish (or any other object) to automatically invoke its __repr__() method.

add(self, newFish) If the number of Fish in the Aquarium is less than its capacity, this method appends its argument to the list of Fish and returns True. Otherwise, it returns False without modifying the list.

population(self) This method returns the number of Fish that are currently held in the Aquarium.

countType(self, type) This method returns the number of Fish in the Aquarium whose species matches the type parameter. For example, myTank.countType("Guppy") would return the number of Fish in myTank that have Guppy as their species.

4) Finally, complete the fillAquarium() function. This function takes two arguments: a string representing the name of a (plain text) data file, and a positive (non-zero) integer representing the tank capacity in gallons. It returns a new Aquarium object. fillAquarium() does the following: (a) It creates a new Aquarium with the specified capacity. (b) It uses a loop to open the specified data file. For each line in the data file: -The function calls createFish() with the current line to get a new Fish. -The function attempts to add the new Fish to the Aquarium object. (c) It returns the new Aquarium.

***USE***

class Fish: def __init__(self, name, species, color, weight): self.name = name self.species = species self.color = color self.weight = weight def __repr__(self): return "<>< (" + self.name + self.color + self.species + ")"   def __lt__(self, other): if self.weight < other.weight:#NOT SURE HOW TO FIX THIS THOUGH REGARDING FLOATS return True  else: return False   def kind(self): return self.species def createFish(data): fields = data.split() return Fish(fields[1], fields[0], fields[2], fields[3])
class Aquarium: def __init__(self, capacity): # ADD YOUR OWN CODE TO COMPLETE THIS METHOD  return   def __repr__(self): # ADD YOUR OWN CODE TO COMPLETE THIS METHOD  return "" # CHANGE OR REPLACE THIS LINE   def add(self, newFish): # ADD YOUR OWN CODE TO COMPLETE THIS METHOD  return False # CHANGE OR REPLACE THIS LINE   def population(self): # ADD YOUR OWN CODE TO COMPLETE THIS METHOD  return -1 # CHANGE OR REPLACE THIS LINE   def countType(self, type): # ADD YOUR OWN CODE TO COMPLETE THIS METHOD  return -1 # CHANGE OR REPLACE THIS LINE  def fillAquarium(filename, gallons): # ADD YOUR OWN CODE TO COMPLETE THIS METHOD  return None # CHANGE OR REPLACE THIS LINE

***TEXT FILES***

TEXT 1 TEXT 2 TEXT 3

Tetra Alvin orange 1.5 Guppy Simon gray 2.2 Oscar Theodore blue 1.0 Cichlid Bart yellow 1.7 Barb Lisa green 3.1 Goldfish Homer yellow 4.1 Gourami Marge red 1.2 Mollie Maggie pink 1.0 Cichlid Ned gray 2.4 Barb Maude orange 2.9 Gourami Krusty white 1.1 Cichlid Bob green 2.5

Barb Zippy yellow 2.3 Goldfish Sparky green 2.0 Barb Itchy white 2.0 Cichlid Scratchy black 2.1 Mollie Manny green 3.2 Gourami Moe yellow 3.1 Gourami Jack white 1.7 Goldfish Dexter blue 1.2 Gourami Rob black 2.4 Tetra Eddie yellow 2.2 Mollie Jesse gray 3.2 Cichlid Dave gray 2.2 Guppy Danny yellow 1.6 Gourami Billy yellow 1.4 Mollie Mary gray 1.4 Oscar Sue red 2.7 Mollie Barbara yellow 3.1 Oscar Dinah orange 2.6 Oscar Ellen gray 2.2 Guppy Joan yellow 3.0 Tetra Mario black 1.7 Mollie Luigi white 2.8 Guppy Peach orange 1.4 Guppy Toad red 3.0 Mollie Bowser yellow 2.2 Barb Koopa black 2.0 Gourami Link black 2.5 Mollie Zelda green 3.3 Cichlid Luke orange 3.3 Tetra Leia red 1.6 Mollie Anakin green 2.7 Cichlid Obi-Wan orange 3.2 Oscar Yoda yellow 3.1 Gourami Han blue 3.2 Oscar Chewbacca orange 1.8 Oscar Lando gray 1.4 Oscar Clara blue 3.2 Mollie Jane black 1.3 Tetra Emily orange 1.8

# Test the Aquarium class test_tank = Aquarium(20) print("The test tank's current population is:", test_tank.population()) print("Adding fish now...") test_tank.add(fish_1) test_tank.add(fish_2) if fish_3 != None: test_tank.add(fish_3) print("The test tank's new population is:", test_tank.population()) print() print(test_tank) print() print("The test tank contains", test_tank.countType("Cichlid"), "Cichlid(s).") print("The test tank contains", test_tank.countType("Guppy"), "Guppy (or Guppies).") print("The test tank contains", test_tank.countType("Tetra"), "Tetra(s).") print("The test tank contains", test_tank.countType("Mollie"), "Mollie(s).") print() # Test the fillAquarium() function print("Creating a new 15-gallon aquarium from tank1.txt...") tank1 = fillAquarium("tank1.txt", 15) if tank1 != None: print("tank1 contains", tank1.population(), "fish:") print() print(tank1) print("Creating a new 30-gallon aquarium from tank2.txt...") tank2 = fillAquarium("tank2.txt", 30) if tank2 != None: print("tank2 contains", tank2.population(), "fish:") print() print(tank2) print("Creating a new 10-gallon aquarium from tank3.txt...") tank3 = fillAquarium("tank3.txt", 10) if tank3 != None: print("tank3 contains", tank3.population(), "fish:") print() print(tank3)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!