Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Model a Farm Using OOP

00:00 The challenge is to create a simplified model of a farm. Also, keep in mind that there are many correct answers. So this is not going to be an exact solution, but there are going to be a lot of different solutions that are all correct.

00:11 There are specific requirements that you can use as a guideline. Keep in mind that they’re open to interpretation. You should have at least four classes: the parent Animal class, and at least three child classes that inherit from Animal.

00:25 Each of those classes should have a few attributes and at least one method and model some behavior appropriate for a specific animal or for all animals.

00:32 So that could be walking, running, eating, sleeping, or something that only, I don’t know, a horse would do. Keep it simple. Utilize inheritance. Make sure you output details about the animals and their behaviors.

00:44 This is part one, but we have a second slide. Here we go.

00:49 Continue modeling your farm. You want to create two more classes, maybe a Field and maybe a Barn. And these places should have an attribute where they can store Animal objects.

00:58 So here you’re going into the direction of composition. Imagine the day-to-day on a farm. You may want to move a cow from a Field into the Barn. Can you do that?

01:08 What’s the methods that you need to be able to move the cow? Which data structure is going to hold the cow on the Field or in the Barn?

01:16 How many can fit into a Barn? Maybe you want to continue this. Maybe you feed the animals with other classes that could represent foods. Maybe there’s like this level of hunger, level of enjoyment that gets modified when you feed them.

01:29 Get creative with this idea, but also don’t get carried away too much. It’s just a model.

01:36 The focus of this assignment is less about the class syntax, which you’ve already trained before in the review exercises, but it’s about software design in general. And again, this is highly subjective, so it’s intentionally left open-ended so that you get a chance to think about how you would organize your code into classes.

01:53 This course is also there to give you a chance to see how someone else would do it.

01:58 This whole thing makes me very happy because it’s highly subjective, which means I can just go ahead and code something up. And it’s not really going to be wrong. There will be many solutions.

02:08 Your solution is very likely going to look different from mine, but it doesn’t mean that it’s wrong. And overall, this gives us lots of chances to learn from each other.

02:17 Why did you code something in a certain way? Why did I code it in a certain way? And there’s this chance to compare, and that’s what this whole challenge is about.

02:25 Try out your way of doing something and then compare it to someone else’s. Expect mistakes, expect opinionated decisions, and hopefully discussions in the Discussion tab.

02:37 If you don’t understand why I did something a certain way, or you think that there’s a better way to do it, or just a different way that you like more, post about it.

02:45 Show your code and then we can talk about it. And with that, I have one more thing for you, a very important tip. Sketch it out before you start programming. I actually mean draw it by hand.

02:57 Now you can take pen and paper, or you can use a digital tool. It doesn’t really matter what material you use. The important thing is that you use your hands and that you can iterate over it often. Draw out your image of the code that you want to build, and I mean, do that before you actually write any code. So grab a pen and paper, sketch out a model of your farm. Identify what classes you want to build, which attributes they should have and which methods they should have.

03:24 Think about inheritance that can help you to prevent code duplication. And do this as often as you need before actually starting to code, just until you have a good mental model of what that farm abstraction is going to look like, because it costs a lot less energy to just draw a circle or a square and write a word there than to actually build that class.

03:48 If you do that beforehand, it just sorts your brain and gets you a mental model of the code that you’re actually going to build, which is super helpful, and it’s going to make your developing experience much nicer. So do that first.

04:02 Draw the idea of the farm that you want to model, and only start coding afterwards. I’ll also sketch my idea on digital paper in the next lesson. When you’re done sketching and coding your own farm, then continue with the next lesson and start to compare your solution and your process to mine. Have fun and see you there.

alphafox28js on Nov. 25, 2023

This may have not been covered yet… But… what is the methodology for “Calling” my instance attribute methods and returning a value when I run the script so as I do not have to highlight and [Shift+Enter] each individual one....I would rather see them all in one shot in order as defined.

from P_Canine_Class_Animal import Canine
from C_Canine_Class_Animal import Canine, Pitbull, Shepherd

####################################################

#instantiate Parent Class
Canine = Canine('Wolf', 10, '1.5m', 'Fur', 'Long')
Canine.Species()
Canine.define()
Canine.speak("Wolf! Wolf!")

####################################################

#instantiate child/sub classes which points to the breed & adds in Attributes of Breed.
Pitbull = Pitbull('Pitbull', 3, '.5m', 'Fur', 'Short')
Pitbull.speak('GgrrWoof!') # Will assign defined sound ('string') to Class.
Pitbull.speak() # Will Default Inheritance to Parent Class via super().speak(sound) method.
Pitbull.Eye_Color()
Pitbull.Ears()
Pitbull.Tail()

####################################################

Shepherd = Shepherd('Shepherd', 7, '.7m', 'Fur', 'Long')
Shepherd.speak("WOOF! WOOF! WOOF!")
Shepherd.Eye_Color()
Shepherd.Ears()
Shepherd.Tail()

####################################################

Martin Breuss RP Team on Dec. 12, 2023

@alphafox28js hi looks like I missed your question here. But I’m actually not quite sure what you’re asking 🤔

Are you running these examples in the IDLE REPL, or did you save them to a file? If you saved the code to a file, then you should be able to run the script and print whichever values you want to see in one go.

But I assume I misunderstood your question, maybe you can try to explain again what you’re looking for?

Become a Member to join the conversation.