Code Monkey home page Code Monkey logo

05---object-oriented-programming-paperboy's Introduction

What You Will Learn

  • Understanding classes and instances

Exercise 4: Paperboy

Back before the Internet existed, people used to have newspapers delivered to their homes. It was so popular that one of the earliest video games was based on making sure you delivered papers to every house in a suburban neighbourhood.

Let's see if we can make a class that represents a paperboy and get them to deliver all of their papers on time!

Start a new project (create a new subfolder) and add a new file called "paperboy.py". Run your program and commit your work after each step.

Each paperboy should have several attributes:

  • Name
  • Experience (the number of papers they've delivered)
  • Earnings (amount of money they've earned)

Every day, each paperboy is given a house number to start at and a house number to finish at. They get paid $0.25 for every paper they deliver and $0.50 for every paper over their quota. If at the end of the day they haven't met their quota, they lose $2.

The minimum number of papers to deliver is 50. The quota is calculated as half of your experience added to the minimum. So the first time a paperboy makes a delivery, the quota is 50. The next time, the quote is 50 plus half the number of papers that the paperboy has delivered in the past. In this way the quota should increase after every delivery the paperboy makes.

Each paperboy should have at least these methods:

  • __str__
  • quota
    • This method should calculate and return the paperboy's quota for his next delivery
  • deliver(self, start_address, end_address)
    • This method will take two house numbers and return the amount of money earned on this delivery as a floating point number. It should also update the paperboy's experience!
    • Let's assume that the start_address is always a smaller number than the end_address
    • As a stretch exercise you can figure out how to ensure it still works if the above assumption isn't met!
  • report
    • This method should return a string about the paperboy's performance
    • e.g. "I'm Tommy, I've delivered 90 papers and I've earned $38.25 so far!"

Here's some sample code, using your Paperboy class:

tommy = Paperboy("Tommy")

tommy.quota() #  50
tommy.deliver(101, 160) # 17.5
tommy.earnings # 17.5
tommy.report() # "I'm Tommy, I've delivered 60 papers and I've earned $17.5 so far!"

tommy.quota() # 80
tommy.deliver(1, 75) # 16.75
tommy.earnings # 34.25
tommy.report() # "I'm Tommy, I've been delivered 135 papers and I've earned $34.25 so far!"

Check Your Understanding

  • What is the difference between a class and an instance of a class?
  • What is an instance variable/attribute?
  • What is an instance method?
  • How is the scope of an instance variable (attribute) different from the scope of a local variable?

05---object-oriented-programming-paperboy's People

Contributors

adamc66 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.