Is this an OO program?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster

Is this an OO program?

 
0
  #1
Oct 25th, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 69
Reputation: lrh9 is an unknown quantity at this point 
Solved Threads: 9
lrh9 lrh9 is offline Offline
Junior Poster in Training
 
0
  #2
Oct 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,606
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso
 
0
  #3
Oct 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,042
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 933
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #4
Oct 25th, 2009
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #5
Oct 25th, 2009
(some small programs are just better off without it)
I already stated that in my original post.

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.

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,042
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 933
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
1
  #6
Oct 25th, 2009
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,606
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso
 
0
  #7
Oct 25th, 2009
Originally Posted by vegaseat View Post
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.)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #8
Oct 25th, 2009
Oh, I didn't know that they're only called methods when the class is assigned an object.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 160
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 48
snippsat snippsat is offline Offline
Junior Poster
 
-1
  #9
Oct 25th, 2009
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()

  1. >>> a = eval('5+8886/45*4554545')
  2. >>> a
  3. 899370824.3333334

Here an example with class.

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #10
Oct 25th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC