943,994 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 846
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 25th, 2009
0

Is this an OO program?

Expand Post »
Okay, I wrote a simple calculator, not in GUI form, but a calculator for simple stuff. So I'm wondering, would this layout be considered OO? I think this is how it works. Now I do know that I didn't use an object to handle the class since it just wasn't necessary. Now I know that making a class wasn't totally necessary for this program, but I'm just curious if this would be the general layout. So here's the code:

Python Syntax (Toggle Plain Text)
  1. import os
  2. import math
  3. import fractions
  4.  
  5. if os.name == "nt":
  6. os.system("color 48")
  7. os.system("title Calculator")
  8. def clear():
  9. os.system("cls")
  10.  
  11. else:
  12.  
  13. def clear():
  14. os.system("clear")
  15.  
  16. class Calc:
  17.  
  18. def menu():
  19. clear()
  20. print ("Welcome to calculator!")
  21. print ("----------------------")
  22. print ("1) Exponents ")
  23. print ("2) Division ")
  24. print ("3) Multiplication ")
  25. print ("4) Addition ")
  26. print ("5) Subtraction ")
  27. print ("6) Square Root ")
  28. print ("7) Simplify Fractions ")
  29. print ("8) Factors ")
  30. print ("9) Exit ")
  31. print ("----------------------")
  32. menu = str(input("Choose one: "))
  33.  
  34. if ( menu in list("123456789") ) == False:
  35. clear()
  36. print ("Please type a valid option!")
  37. input ()
  38. Calc.menu()
  39.  
  40. methods[int(menu)-1]()
  41.  
  42. def exp():
  43. clear()
  44. print ("? to the - power equals -")
  45. p1 = int(input())
  46. print ()
  47. print (p1, "to the ? power equals -")
  48. p2 = int(input ())
  49. print ()
  50. p3 = pow(p1, p2)
  51. print (p1, "to the", p2, "power equals", p3)
  52. input ()
  53. Calc.menu()
  54.  
  55. def div():
  56. clear()
  57. print ("? divided by - equals -")
  58. d1 = int(input())
  59. print ()
  60. print (d1, "divided by ? equals -")
  61. d2 = int(input())
  62. print ()
  63. d3 = d1/d2
  64. if (d1 % d2) == 0:
  65. d3 = int(d3)
  66. print (d1, "divided by", d2, "equals", d3)
  67. input ()
  68. Calc.menu()
  69.  
  70. def times():
  71. clear()
  72. print ("? multiplied by - equals -")
  73. m1 = int(input())
  74. print ()
  75. print (m1, "multiplied by ? equals -")
  76. m2 = int(input())
  77. print ()
  78. m3 = m1*m2
  79. print (m1, "multiplied by", m2, "equals", m3)
  80. input ()
  81. Calc.menu()
  82.  
  83.  
  84. def add():
  85. clear()
  86. print ("? plus - equals -")
  87. a1 = int(input())
  88. print ()
  89. print (a1, "plus ? equals -")
  90. a2 = int(input())
  91. print ()
  92. a3 = a1+a2
  93. print (a1, "plus", a2, "equals" ,a3)
  94. input ()
  95. Calc.menu()
  96.  
  97. def minus():
  98. clear()
  99. print ("? minus - equals -")
  100. s1 = int(input())
  101. print ()
  102. print (s1, "minus ? equals -")
  103. s2 = int(input())
  104. print ()
  105. s3 = s1-s2
  106. print (s1, "minus", s2, "equals", s3)
  107. input ()
  108. Calc.menu()
  109.  
  110.  
  111. def square():
  112. clear()
  113. print ("The Square Root of ? is -")
  114. sqrt1 = int(input())
  115. sqrt2 = math.sqrt(sqrt1)
  116. if ( str(sqrt2).split(".")[1] ) == "0":
  117. sqrt2 = int(sqrt2)
  118.  
  119.  
  120. print ()
  121. print ("The Square Root of", sqrt1, "is", sqrt2)
  122. input ()
  123. Calc.menu()
  124.  
  125. def frac():
  126. clear()
  127. print ("?/- simplified is -/-")
  128. fr1 = int(input())
  129. fr11 = str(fr1)
  130. print ()
  131. print (fr11 + "/- simplified is -/-")
  132. fr2 = int(input())
  133. fr22 = str(fr2)
  134. print ()
  135. fr3 = fractions.Fraction(fr1, fr2)
  136. fr33 = str(fr3)
  137. print (fr11 + "/" + fr22, "simplified is", fr33)
  138. input ()
  139. Calc.menu()
  140.  
  141. def factors():
  142. clear()
  143. f = int(input("Type the number you would like to find the factors of: "))
  144. f2 = []
  145. for f1 in range(f + 1):
  146. if f1 != 0:
  147. if (f % f1) == 0:
  148. f2.append(str(f1))
  149. print ("The factors of", f, "are", ",".join(f2))
  150. input ()
  151. Calc.menu()
  152.  
  153. methods = [
  154. Calc.exp,
  155. Calc.div,
  156. Calc.times,
  157. Calc.add,
  158. Calc.minus,
  159. Calc.square,
  160. Calc.frac,
  161. Calc.factors
  162. ]
  163.  
  164. Calc.menu()
Last edited by AutoPython; Oct 25th, 2009 at 12:06 am.
Similar Threads
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Oct 25th, 2009
0
Re: Is this an OO program?
It's more object oriented than most code posted here, but there are more ways to use object oriented design and code in this program.

For instance, there could be a general class for a menu.
Reputation Points: 106
Solved Threads: 35
Posting Whiz in Training
lrh9 is offline Offline
238 posts
since Oct 2009
Oct 25th, 2009
0
Re: Is this an OO program?
Well, it's bad OOP.

Not that I actually think that everything has to be OOP mind you (some small programs are just better off without it), but take for example your methods list. Why is your class directly manipulating an external object which it does not own (or was not given)?

What does displaying a menu have to do with a calculator? I don't necessarily agree that you need a class for something so small though. Perhaps a function that returns the index of the function selected, or better yet the name (so you can just do getattr(c, name)() )
Last edited by scru; Oct 25th, 2009 at 10:30 am.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Oct 25th, 2009
0
Re: Is this an OO program?
You have used a class to group functions together. This is the most simple form of class encapsulation. They are all functions that belong together, so it makes sense.

BTW, nice code that uses some features of Python3.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 25th, 2009
0
Re: Is this an OO program?
Quote ...
(some small programs are just better off without it)
I already stated that in my original post.

Quote ...
Why is your class directly manipulating an external object which it does not own (or was not given)?
I had this weird problem. Because the list uses Methods from the Calc class, it wouldn't let me use the Calc class until it was totally defined. Which didn't make sense to me because a class does not use it's information while it's being defined.

Quote ...
What does displaying a menu have to do with a calculator?
Well, what would a program be without a menu? I put the Menu as part of the class because that went with the program.
Last edited by AutoPython; Oct 25th, 2009 at 11:15 am.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Oct 25th, 2009
1
Re: Is this an OO program?
What you have in your class right now are functions, not methods. You can turn your functions into methods with 'self'. This way you can also avoid globals outside the class, like your method list.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 25th, 2009
0
Re: Is this an OO program?
Click to Expand / Collapse  Quote originally posted by vegaseat ...
What you have in your class right now are functions, not methods. You can turn your functions into methods with 'self'. This way you can also avoid globals outside the class, like your method list.
Whoa hey I did not even notice that! Yeah dude, to make methods in python Classes, set self as the first argument of every routine. To call a method from any method within the same class, do self.methodname() (note that you don't put self back in, when calling methods, python automatically sends in the object instance, ergo self, as the first argument.)
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Oct 25th, 2009
0
Re: Is this an OO program?
Oh, I didn't know that they're only called methods when the class is assigned an object.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Oct 25th, 2009
-1
Re: Is this an OO program?
This code had been better without a class,and this is not the OOP way of thinking.
One function for Addition/Division....... can be shorten by eval()

Python Syntax (Toggle Plain Text)
  1. >>> a = eval('5+8886/45*4554545')
  2. >>> a
  3. 899370824.3333334

Here an example with class.

Python Syntax (Toggle Plain Text)
  1. import math
  2. class Calculator(object):
  3. '''Doc_string info about class'''
  4. def calculate(self, expression):
  5. self.value = eval(expression)
  6.  
  7. def square(self, sqrt1):
  8. '''This look like a function,but inside a class is called a method'''
  9. self.b = math.sqrt(sqrt1)
  10.  
  11. def __str__(self):
  12. return self.value
  13. return self.b
  14.  
  15. #here a make the method of class short
  16. #and i do user_input and printing outside of the class(you can of course make a menu but not in the class)
  17. a = Calculator()
  18. print('Calculate +-*/ and Square Root')
  19. b = input('values')
  20. a.calculate(b)
  21. print('The sum of %s is %d' % (b, a.value))
  22.  
  23. c = int(input('Enter Square Root value'))
  24. a.square(c)
  25. print('The square Root of %d is %d' % (c, a.b))
  26.  
  27. ''' my output-->
  28. Calculate +-*/ and Square Root
  29. The sum of 45+78*4545/56 is 6375
  30. The square Root of 25 is 5
  31. '''
Last edited by snippsat; Oct 25th, 2009 at 5:47 pm.
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is offline Offline
771 posts
since Aug 2008
Oct 25th, 2009
0
Re: Is this an OO program?
Quote ...
This code had been better without a class
This is really starting to annoy me, in the first post I already stated that the program didn't need a class, but I added it anyway just see.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Check for Excel session
Next Thread in Python Forum Timeline: Making an official-looking window





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC