defining class for physic problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    #1

    defining class for physic problem

    I'd like to create a physic simulation of shooting a arrow as the picture below

    please help me step-by-step with specific details, how to defining this class.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Although this seems like a homework assignment and this site has rules about such post, I'm willing to give the syntax for class creation.[CODE=python]class Arrow(object):
    # initial states go into the contructor method
    def __init__(self, x, y, ivX, ivY):
    self._x = x
    self._y = y
    self._ivX = ivX
    self._ivY = ivY

    def getX(self):
    return self._x

    def getY(self):
    return self._y

    arrow = Arrow(0, 0, 0, 0)
    print arrow.getX()[/CODE]You may get help with other parts when you show that you have done some work and made a real effort to work the problem on your own.

    The homework rule is the 6th on the list in our Posting Guidelines
    Thanks for joining.

    Comment

    • python101
      New Member
      • Sep 2007
      • 90

      #3
      Thank you very much.

      It is not my homework, I will have to take a programming next semester in C++. Since I did not have any experiment in programming, I decided to learn an easy programming language, as my friends and some other 2nd-year students said I should learn Python before learning C++. I read the book along with out any assistant so there are many things, even easy ones, I can't understand why.

      Comment

      • python101
        New Member
        • Sep 2007
        • 90

        #4
        [code=python]
        >>a= Arrow(0,0,10,10 )
        >>a
        >><__main__.Arr ow instance at ....>
        #however I would like to have the output
        >>[0.0, 10,10]
        # if enter
        >>a
        [/code]
        How can I do this?

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by python101
          [code=python]
          >>a= Arrow(0,0,10,10 )
          >>a
          >><__main__.Arr ow instance at ....>
          #however I would like to have the output
          >>[0.0, 10,10]
          # if enter
          >>a
          [/code]
          How can I do this?
          Operator overloading. Add this method to your class definition:[code=Python]#operator overloading
          def __str__(self):
          return '[%s.%s %s,%s]' % (self._x, self._y, self._ivX, self._ivY)[/code]Prints:
          >>> [0.0 10,10]
          >>>

          Comment

          • python101
            New Member
            • Sep 2007
            • 90

            #6
            Originally posted by bvdet
            Operator overloading. Add this method to your class definition:[code=Python]#operator overloading
            def __str__(self):
            return '[%s.%s %s,%s]' % (self._x, self._y, self._ivX, self._ivY)[/code]Prints:
            >>> [0.0 10,10]
            >>>
            thank you very much.

            Comment

            • python101
              New Member
              • Sep 2007
              • 90

              #7
              [CODE=python]class Arrow(object):
              # initial states go into the contructor method
              def __init__(self, x, y, ivX, ivY, giventime):
              self._x = x
              self._y = y
              self._ivX = ivX
              self._ivY = ivY
              self._t = giventime

              def getX(self):
              return self._x

              def getY(self):
              return self._y

              def getX(self):
              return self._ivX

              def getX(self):
              return self._ivY
              # thank for the helping code

              #from here, the code is just a predicted code to show what I want the output to be print out (I think it is the best way to express my idea)

              def getResult(self) :
              return currentX = self._x + self._iniV*self ._t
              currentY = self._y + (self._ivY*self ._t) - 5*(self._t*self ._t)
              currentVx = self._ivX
              currentVy = self._ivY* - 10* self._t


              def update (self, newVx, newVy, newTime)
              return FinalX = currentX + newVx*t
              FinalY = currentY + newVy*newTime -newTime*newTime *5
              FinalVx = newVx
              FinalVy = newVy - 10*newTime

              #If I use the update(), it will update the position x and y based on the current one
              [/CODE]

              How can I write the code for def getResult and def update correctly?

              Comment

              • python101
                New Member
                • Sep 2007
                • 90

                #8
                How to return several values in one time

                I can only make the def return only value, however, I want to have several return values. For intance,

                [code=python]
                def getResult(self) :
                return currentX = self._x + self._iniV*self ._t
                currentY = self._y + (self._ivY*self ._t) - 5*(self._t*self ._t)
                currentVx = self._ivX
                currentVy = self._ivY* - 10* self._t

                #assuming that everything related has been defined correcly.
                [/code]

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by python101
                  I can only make the def return only value, however, I want to have several return values. For intance,

                  [code=python]
                  def getResult(self) :
                  return currentX = self._x + self._iniV*self ._t
                  currentY = self._y + (self._ivY*self ._t) - 5*(self._t*self ._t)
                  currentVx = self._ivX
                  currentVy = self._ivY* - 10* self._t

                  #assuming that everything related has been defined correcly.
                  [/code]
                  something like this:
                  [CODE=python]def update (self, newVx, newVy, newTime)
                  currentX = self._x + self._iniV*self ._t
                  currentY = self._y + (self._ivY*self ._t) - 5*(self._t*self ._t)
                  currentVx = self._ivX
                  currentVy = self._ivY* - 10* self._t

                  def getResult(self) :
                  return FinalX, FinalY, FinalVx, FinalVy[/CODE]

                  Comment

                  • python101
                    New Member
                    • Sep 2007
                    • 90

                    #10
                    Thank you very much. I'm working on this program right now, hopefully I will finish it soon without asking any more question

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by python101
                      Thank you very much. I'm working on this program right now, hopefully I will finish it soon without asking any more question
                      Please feel free to ask all the questions that you need to.
                      I would encourage you, however, to check for simple spelling, etc. errors before posting.

                      Comment

                      • python101
                        New Member
                        • Sep 2007
                        • 90

                        #12
                        Thank you very much, I finished the work. However, I would like to make it be better.


                        [CODE=python]class Arrow(object):
                        def __init__(self, x, y, ivX, ivY):
                        self._x = x
                        self._y = y
                        self._ivX = ivX
                        self._ivY = ivY
                        # I want to print to the screen if the user enter negative values for x, y, ivX and ivY, like
                        a=Arrow(-1,-2,-4,-6)
                        >>>invalid
                        [/code]

                        Comment

                        • bartonc
                          Recognized Expert Expert
                          • Sep 2006
                          • 6478

                          #13
                          Originally posted by python101
                          Thank you very much, I finished the work. However, I would like to make it be better.


                          [CODE=python]class Arrow(object):
                          def __init__(self, x, y, ivX, ivY):
                          self._x = x
                          self._y = y
                          self._ivX = ivX
                          self._ivY = ivY
                          # I want to print to the screen if the user enter negative values for x, y, ivX and ivY, like
                          a=Arrow(-1,-2,-4,-6)
                          >>>invalid
                          [/code]
                          An Exception() ought to do the trick very nicely:[CODE=python]
                          class ArrowError(Exce ption):
                          pass

                          class Arrow(object):
                          def __init__(self, *args):
                          if len(args) != 4:
                          raise ArrowError("Wro ng number of args")
                          for arg in args:
                          if arg < 0:
                          raise ArrowError("Neg ative numbers are not allowed")
                          x, y, ivX, ivY = args
                          self._x = x
                          self._y = y
                          self._ivX = ivX
                          self._ivY = ivY

                          a = Arrow()[/CODE]

                          Comment

                          • bartonc
                            Recognized Expert Expert
                            • Sep 2006
                            • 6478

                            #14
                            Originally posted by bartonc
                            An Exception() ought to do the trick very nicely:[CODE=python]
                            class ArrowError(Exce ption):
                            pass

                            class Arrow(object):
                            def __init__(self, *args):
                            if len(args) != 4:
                            raise ArrowError("Wro ng number of args")
                            for arg in args:
                            if arg < 0:
                            raise ArrowError("Neg ative numbers are not allowed")
                            x, y, ivX, ivY = args
                            self._x = x
                            self._y = y
                            self._ivX = ivX
                            self._ivY = ivY

                            a = Arrow()[/CODE]
                            After that, if you don't want the actual error to show, you can:[CODE=python]
                            try:
                            a = Arrow(1, 2, 3, -4)
                            except ArrowError:
                            print "bad argurment"[/CODE]

                            Comment

                            • python101
                              New Member
                              • Sep 2007
                              • 90

                              #15
                              ^_^ thank you very much

                              Comment

                              Working...