Question

Python Write a class bug that models a bug moving along a horizontal line. The bug...

Python

Write a class bug that models a bug moving along a horizontal line. The bug moves either to the right to left. Initially, the bug moves to the right, but it can turn to change its direction. In each move, its position changes by one unit in the current direction. Provide a constructor

def__init__(self, initial position)

and methods

. def turn(self)

. def move(self

. def getposition(self)

Sample usage:

bugsy = bug(10)

bugsy.move() # Now the position is 11

bugsy.turn()

bugsy.move() # Now the position is 10

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Bug:
    def __init__(self, initialPos):
        self.position = initialPos
        self.direction = 1

    def turn(self):
        self.direction *= -1

    def move(self):
        self.position += self.direction
        print("Now the position is " + str(self.position))
        return self.position

    def getPosition(self):
        return self.position


bugsy = Bug(10)
bugsy.move()  # Now the position is 11
bugsy.turn()
bugsy.move()  # Now the position is 10
Add a comment
Know the answer?
Add Answer to:
Python Write a class bug that models a bug moving along a horizontal line. The bug...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ** MUST BE PROGRAMMED IN PYTHON** Thank you. 1. Write a class bug that models a...

    ** MUST BE PROGRAMMED IN PYTHON** Thank you. 1. Write a class bug that models a bug moving along a horizontal line. The bug moves either to the right to left. Initially, the bug moves to the right, but it can turn to change its direction. In each move, its position changes by one unit in the current direction. Provide a constructor def__init__(self, initial position) and methods . def turn(self) . def move(self . def getposition(self) Sample usage: bugsy =...

  • C++ IJU U Construct a user-defined object class named Bug that models a bug moving along...

    C++ IJU U Construct a user-defined object class named Bug that models a bug moving along a horizon- tal line. The bug moves either to the right or left. Initially, the bug moves to the right, but it can turn to change its direction. In each move, its position changes by one unit in the current direction. The Bug class will have data members position (type int) for the bug's position on the horizontal line and dir (type int) for...

  • please answer "class playerexception(exception):" in python Notes Two players will face each other. They each decide...

    please answer "class playerexception(exception):" in python Notes Two players will face each other. They each decide independently to "cooperate" or "cheat". If they both cooperated, they each win two points. If they both cheated, nobody wins anything. one cheats, the cheater gets +3 and the cooperator loses a point. That wasn't very kind! One turn is defined as each player making a choice, and winning or losing some points as a result. Shared history against this player is available to...

  • 4. The following graph represents the position of an object, moving along a horizontal line as...

    4. The following graph represents the position of an object, moving along a horizontal line as a function of time. The positive direction is East and negative direction is West. Express all answers to two significant figures. (5 marks) Position Vs. Time 15 10 6 8 10 12 14 16 18 -10 -15 Time, t, (s) a) Over what time interval(s), if any, is the object stopped? b) At what time(s), if any, does the object change direction? c) Determine...

  • I want to make a really simple maze game in Python. I need to use tkinter and GUI to make this ha...

    I want to make a really simple maze game in Python. I need to use tkinter and GUI to make this happened. Under you can see the description of the task, but i need help to getting startet. If there is an other easier way I'm just happy for the help!! task Description: You have to create a maze game. The goal of the game is to get out of the maze. The game should read The maze from a...

  • In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains,...

    In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains, insert, and normal_list methods. You may use default arguments and/or helper functions. The file must be named: LinkedList.py Here is what I have for my code so far. The methods I need the recursive implementations for will be bolded: class Node: """ Represents a node in a linked list (parent class) """ def __init__(self, data): self.data = data self.next = None class LinkedList: """...

  • Python Write the class SodaMachine that will represent a typical soda vending machine (product name, price)....

    Python Write the class SodaMachine that will represent a typical soda vending machine (product name, price). An instance of SodaMachine has access to three methods, purchase, deposit and restock that will describe its interaction with the user returning strings. Tip: use the string format method Here is the base code===== class SodaMachine: ''' Creates instances of the class SodaMachine. Takes a string and an integer ''' def __init__(self, product, price): #-- start code here --- #-- ends here --- def...

  • 10p Python For the following question, refer to the Python module on the right, as well...

    10p Python For the following question, refer to the Python module on the right, as well as the code below. 1) What is the output of the following code? #dodgeable.py #main dodgeable.py import dodgeable class Locatable: def NUM_CARS = 3 init (self,x,y): NUM TRUCKS = 2 self.mX = X WIDTH = 60 self.mY = y def setx (self,x): def main (): self.mX = x object_list = [] def sety (self, y): self.mY = y for i in range (NUM_CARS) :...

  • 9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name,...

    9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name, age) + getName + getAge0 def init (self, name, age): self.mName = name self.mAge = age Dog Cat def getName(self): return self.mName mSize mColor + __init__(name, age,size) + getSize() + momCommento + getDog Years() +_init__(name, age,color) + getColor + pretty Factor + getCatYears def getAge(self): return self.mAge #dog.py import pet #cat.py import pet class Dog (pet. Pet): class Cat (pet. Pet): def init (self,...

  • It's my python homework. Not Java. Design type 1: # Creating an object of the class...

    It's my python homework. Not Java. Design type 1: # Creating an object of the class "DeAnzaBurger" theOrder = DeAnzaBurger() # calling the main method theOrder.main() # And the class is like: class DeAnzaBurger: # You may need to have a constructor def __init__(self): self._orderDict = {} self._priceBeforeTax = 0 self._priceAfterTax = 0 # you may have the tax rate also as an instance variable. But as I mentioned, you can use your # own deign.    .... # That...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT