Giter Club home page Giter Club logo

subclassing-2023's Introduction

Homework

For homework let's practice subclasses. In your repository make two files. One named baserobot.py. That file describes a class BaseRobot that needs to have the following properties and methods.

The BaseRobot class should have the following properties:

  • self.x: The x position of the robot
  • self.y: The y position of the robot

And the following methods:

  • __init__: The constructor should take in two numbers and set them to be the x position of the robot (self.x) and the y position of the robot (self.y).
  • move: Move takes in two numbers in increases that x and y positions of the robot by those numbers
  • beep: Beep prints the word beep to the console.
  • __str__: returns a string that says the position of the robot.

The code for the __str__ method should look like this:

def __str__(self):
    return f"Robot is at position ({self.x}, {self.y})"

Then have another file, armbot.py, that describes a subclass of BaseRobot called ArmBot. An ArmBot can do all the things that BaseRobot can do, but with these differences:

The ArmBot class should have properties it inherits from the BaseRobot and the following properties:

  • self.arm_position: The position of the arm of the robot
  • self.claw_open: A boolean value indicating if the claw is open or closed

The Armbot should have the following methods:

  • __init__: In the init method it should call the init of the BaseRobot class using super, but also set a value for its arm position, self.arm_position to be zero. Also, it sets a value for the claw indicating that the claw is closed. Do this by setting self.claw_open to False.
  • move_arm: This method takes in a number and increases the arm_position by that amount.
  • grab_claw: This method sets the value of self.claw_open to be False.
  • release_claw: This method sets the value of self.claw_open to be True.
  • __str__: Reports the position of the robot and status of the arm and the claw.

The code for the __str__ method should look like this:

def __str__(self):
    return super().__str__()+" with arm at position {self.arm_position} and claw {'open' if self.claw_open else 'closed'}"

subclassing-2023's People

Contributors

mbardoe avatar

Watchers

 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.