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...

  • need help with ANALYZE PART A AND B and FINALIZE EXPLORE A car is moving with constant acceleration along a straight road (A) Determine how the velocity and the displacement of the car vary with t...

    need help with ANALYZE PART A AND B and FINALIZE EXPLORE A car is moving with constant acceleration along a straight road (A) Determine how the velocity and the displacement of the car vary with time if the car is initialy moving to the right with a speed of 9.0 m/s and has an acceleration to the right of 1.7 m/s (B) Determine the velocity as a function of time when the velocity is instead initially to the right and...

  • 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...

  • 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,...

  • Score: E-peild Imagine a proton moving initially at 2.1x 10 m/s in the positive direction of...

    Score: E-peild Imagine a proton moving initially at 2.1x 10 m/s in the positive direction of the x-axis. It moves in a field-free space until it passes by the origin, x-0, where it encounters an electric field directed along the x-axis. (The y and z components of the field are zero everywhere.) The electric potential, vto), associated with this field is shown i the left graph for 0x70 cm. 250 200 E 150 100 8 50 0 10 20 30...

  • PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None   

    PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None        self.__size = 0    # Return the head element in the list    def getFirst(self):        if self.__size == 0:            return None        else:            return self.__head.element        # Return the last element in the list    def getLast(self):        if self.__size == 0:            return None        else:            return self.__tail.element    # Add an element to the beginning of the list    def addFirst(self, e):        newNode = Node(e) # Create a new node        newNode.next = self.__head # link...

  • // ====== FILE: Point.java ========= // package hw3; /** * A class to that models a...

    // ====== FILE: Point.java ========= // package hw3; /** * A class to that models a 2D point. */ public class Point { private double x; private double y; /** * Construct the point (<code>x</code>, <code>y</code>). * @param x the <code>Point</code>'s x coordinate * @param y the <code>Point</code>'s y coordinate */ public Point(double x, double y) { this.x = x; this.y = y; } /** * Move the point to (<code>newX</code>, <code>newY</code>). * @param newX the new x coordinate for...

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