Question

** 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 = bug(10)

bugsy.move() # Now the position is 11

bugsy.turn()

bugsy.move() # Now the position is 10

your driver program should construct a bug, make it move and turn a few times, and print the actual and expected positions.

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:
** MUST BE PROGRAMMED IN PYTHON** Thank you. 1. Write a class bug that models a...
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
  • 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...

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

  • a derived class from Organism. You must complete the constructors, and the method definitions that were...

    a derived class from Organism. You must complete the constructors, and the method definitions that were abstract methods in Organism. /** Organism.java * Definition for the Organism base class. * Each organism has a reference back to the World object so it can move * itself about in the world. */ public abstract class Organism {    protected int x, y;       // Position in the world    protected boolean moved;   // boolean to indicate if moved this turn   ...

  • This is my assignment in Python. Please help me thank you Design type 1: # Creating an object of ...

    This is my assignment in Python. Please help me thank you Design type 1: # Creating an object of the class "Burger" theOrder = Burger() # calling the main method theOrder.main() # And the class is like: class Burger: # 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...

  • IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand...

    IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....

  • please help me with this python code thank you 1. Write a Student class that stores...

    please help me with this python code thank you 1. Write a Student class that stores information for a Rutgers student. The class should include the following instance variables: (10 points) o id, an integer identifier for the student o lastName, a string for the student's last name o credits, an integer representing the number of course-credits the student has earned o courseLoad, an integer representing the current number of credits in progress Write the following methods for your Student...

  • python / visual studio Problem 1: Random Walk A random walk is a stochastic process. A...

    python / visual studio Problem 1: Random Walk A random walk is a stochastic process. A stochastic process is a series of values that are not determined functionally, but probabilistically. The random walk is supposed to describe an inebriated person who, starting from the bar, intends to walk home, but because of intoxication instead randomly takes single steps either forward or backward, left or right. The person has no memory of any steps taken, so theoretically, the person shouldn't move...

  • 1 Overview For this assignment you are required to write a Java program that plays (n,...

    1 Overview For this assignment you are required to write a Java program that plays (n, k)-tic-tac-toe; (n, k)-tic- tac-toe is played on a board of size n x n and to win the game a player needs to put k symbols on adjacent positions of the same row, column, or diagonal. The program will play against a human opponent. You will be given code for displaying the gameboard on the screen. 2 The Algorithm for Playing (n, k)-Tic-Tac-Toe The...

  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end show the exact Output that's shown in the Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int...

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