project - f01.justanswer.com

25
Project Object Oriented Programming April 7, 2021 This assignment has two components. The first part is writing a series of functions that will assist in making a math problem game. The second part involves writing functions involving numbers and an interface to test things. Parts 1 and 2 will be weighed somewhat more heavily than part 2, so make sure to allocate sufficient time and effort accordingly! 1

Upload: others

Post on 27-Jan-2022

9 views

Category:

Documents


0 download

TRANSCRIPT

Project

Object Oriented Programming

April 7, 2021

This assignment has two components. The first part is writing a series of functions thatwill assist in making a math problem game. The second part involves writing functionsinvolving numbers and an interface to test things. Parts 1 and 2 will be weighedsomewhat more heavily than part 2, so make sure to allocate sufficient time andeffort accordingly!

1

1. Part 1)For part 1) of this assignment you will be writing a series of programs that build onone another, so it’s important to progress through this part in order. You only need tosubmit the final version of your code (no need to submit the intermediate steps thatyou took in order to reach the final program).

(a) For this step you will be writing three functions:

1 # function: horizontal_line

2 # input: a width value (integer) and a single character (string)

3 # processing: generates a single horizontal line of the desired size

4 # output: returns the generated pattern (string)

5

6 # function: vertical_line

7 # input: a shift value and a height value (both integers)

8 # and a single character (string)

9 # processing: generates a single vertical line of the desired height.

10 # the line is offset from the left side of the screen

11 # using the shift value

12 # output: returns the generated pattern (string)

13

14 # function: two_vertical_lines

15 # input: a width value and a height value (both integers)

16 # and a single character (string)

17 # processing: generates two vertical lines. the first line is along the left side of

18 # the screen. the second line is offset using the "width" value supplied

19 # output: returns the generated pattern (string)

Hint: Strings can be used as accumulator variables just like the numeric datatypes.

Note that your function must RETURN the generated pattern (as a string) - thesefunctions SHOULD NOT print the pattern being generated. Printing happens inthe ’main’ program when the function is called. Remember, return != print() !!!!!!

Here is a sample program that you can use to test your functions. The expectedoutput is printed below the sample code:

1 print("Horizontal line, width = 5:")

2 temp = horizontal_line(5, "*")

3 print(temp)

4 print()

5

6 print("Horizontal line, width = 10:")

7 temp = horizontal_line(10, "+")

8 print(temp)

2

9 print()

10

11 print("Horizontal line, width = 15:")

12 temp = horizontal_line(15, "z")

13 print(temp)

14 print()

15

16 print("Vertical Line, shift=0 height=3:")

17 temp = vertical_line(0, 3, "!")

18 print(temp)

19 print()

20

21 print("Vertical Line, shift=3 height=3:")

22 temp = vertical_line(3, 3, "&")

23 print(temp)

24 print()

25

26 print("Vertical Line, shift=6 height=5:")

27 temp = vertical_line(6, 5, '$')

28 print(temp)

29 print()

30

31 print("Two Vertical Lines, width=3 height=3:")

32 temp = two_vertical_lines(3, 3, '^')

33 print(temp)

34 print()

35

36 print("Two Vertical Lines, width=4 height=5:")

37 temp = two_vertical_lines(4, 5, '@')

38 print(temp)

39 print()

40

41 print("Two Vertical Lines, width=5 height=2:")

42 temp = two_vertical_lines(5, 2, '#')

43 print(temp)

44 print()

Expected Output:

1 Horizontal line, width = 5:

2 *****

3

4 Horizontal line, width = 10:

5 ++++++++++

3

6

7 Horizontal line, width = 15:

8 zzzzzzzzzzzzzzz

9

10 Vertical Line, shift=0 height=3:

11 !

12 !

13 !

14

15

16 Vertical Line, shift=3 height=3:

17 &

18 &

19 &

20

21

22 Vertical Line, shift=6 height=5:

23 $

24 $

25 $

26 $

27 $

28

29

30 Two Vertical Lines, width=3 height=3:

31 ^ ^

32 ^ ^

33 ^ ^

34

35

36 Two Vertical Lines, width=4 height=5:

37 @ @

38 @ @

39 @ @

40 @ @

41 @ @

42

43

44 Two Vertical Lines, width=5 height=2:

45 # #

46 # #

(b) Next, write 10 new functions that generate the digits 0-9 using your three linefunctions. The goal here is to render the digits as they would appear on a digitaldisplay:

4

Each function should accept a ”width” argument to control how wide the numbershould be as well as a single character. You can assume numbers will always beprinted with a height of 5. For example, here is the function for the number 1:

1 """

2 function: number_1

3 input: a width value (integer) and a single character (string)

4 processing: generates the number 1 as it would appear on a digital display

5 using the supplied width value

6 output: returns the generated pattern (string)

7 """

8 def number_1(width, character):

9 pattern = vertical_line(width-1, 5, character)

10 return pattern

And here’s a sample program that calls the function a few times:

1 print("Number 1, width=5: ")

2 temp = number_1(5, '*')

3 print(temp)

4 print()

5

6 print ("Number 1, width=10: ")

7 temp = number_1(10, '*')

8 print(temp)

9 print()

10

11 print("Number 1, width=2: ")

12 temp = number_1(2, '*')

13 print (temp)

14 print ()

Expected Output:

1 Number 1, width=5:

2 *

3 *

4 *

5 *

6 *

7

8

9 Number 1, width=10:

10 *

5

11 *

12 *

13 *

14 *

15

16

17 Number 1, width=2:

18 *

19 *

20 *

21 *

22 *

Here’s a sample program that prints all of the numbers (0-9).

1 temp = number_0(5, '*')

2 print(temp)

3 print()

4

5 temp = number_1(5, '*')

6 print(temp)

7 print()

8

9 temp = number_2(5, '*')

10 print(temp)

11 print()

12

13 temp = number_3(5, '*')

14 print(temp)

15 print()

16

17 temp = number_4(5, '*')

18 print(temp)

19 print()

20

21 temp = number_5(5, '*')

22 print(temp)

23 print()

24

25 temp = number_6(5, '*')

26 print(temp)

27 print()

28

29 temp = number_7(5, '*')

6

30 print(temp)

31 print()

32

33 temp = number_8(5, '*')

34 print(temp)

35 print()

36

37 temp = number_9(5, '*')

38 print(temp)

39 print()

Expected Output:

1 *****

2 * *

3 * *

4 * *

5 *****

6

7

8 *

9 *

10 *

11 *

12 *

13

14

15 *****

16 *

17 *****

18 *

19 *****

20

21

22 *****

23 *

24 *****

25 *

26 *****

27

28

29 * *

30 * *

31 *****

7

32 *

33 *

34

35

36 *****

37 *

38 *****

39 *

40 *****

41

42

43 *****

44 *

45 *****

46 * *

47 *****

48

49

50 *****

51 *

52 *

53 *

54 *

55

56

57 *****

58 * *

59 *****

60 * *

61 *****

62

63

64 *****

65 * *

66 *****

67 *

68 *

(c) Write a function called ’print number’ that prints out any desired number to thescreen. Here’s some comments for this function:

1 """

2 function: print_number

3 input: a number to print (integer), a width value (integer) and a single character (string)

8

4 processing: prints the desired number to the screen using the supplied width value

5 output: does not return anything

6 """

And here’s some sample code that you can use to test your function:

1 print_number(0, 5, '*')

2 print_number(1, 6, '*')

3 print_number(2, 7, '*')

4 print_number(3, 8, '*')

5 print_number(4, 9, '*')

6 print_number(5, 10, '*')

7 print_number(6, 11, '*')

8 print_number(7, 12, '*')

9 print_number(8, 13, '*')

10 print_number(9, 14, '*')

And here’s the expected output:

1 *****

2 * *

3 * *

4 * *

5 *****

6

7 *

8 *

9 *

10 *

11 *

12

13 *******

14 *

15 *******

16 *

17 *******

18

19 ********

20 *

21 ********

22 *

23 ********

24

25 * *

9

26 * *

27 *********

28 *

29 *

30

31 **********

32 *

33 **********

34 *

35 **********

36

37 ***********

38 *

39 ***********

40 * *

41 ***********

42

43 ************

44 *

45 *

46 *

47 *

48

49 *************

50 * *

51 *************

52 * *

53 *************

54

55 **************

56 * *

57 **************

58 *

59 *

(d) Write two new functions that simulate the addition and subtraction operators.Each of these functions should accept a width value as an argument (integer)and a single character (string) – the function should then return the generatedpattern. You can assume the operators will always be 5 units high. Here’s somesample code:

1 temp = plus(5, '*')

2 print(temp)

3 print()

10

4

5 temp = minus(5, '*')

6 print(temp)

Which will generate ...

1 *

2 *

3 *****

4 *

5 *

6

7

8

9 *****

Note that your ”plus” sign may look odd if it is rendered using an even size value- for example:

1 # rendered using a width of 6

2 *

3 *

4 ******

5 *

6 *

To fix this you should double up the vertical line in the center for even sizes, likethis:

1 # rendered using a width of 6

2 **

3 **

4 ******

5 **

6 **

(e) Write a function called ”check answer” which will determine if a given addition orsubtraction problem was solved correctly. Here’s some comments for the function:

1 # function: check_answer

2 # input: two numbers (number1 & number2, both integers) an answer (an integer)

3 # and an operator (+ or -, expressed as a String)

4 # processing: determines if the supplied expression is correct.

5 # for example, if the operator is "+", number1 = 1, number2 = 2

11

6 # and answer = 3 then the expression is correct

7 # (1 + 2 = 3).

8 # output: returns True if the expression is correct, False if it is not correct

Here’s a sample program that you can use to test your function:

1 answer1 = check_answer(1, 2, 3, "+")

2 print (answer1)

3 answer2 = check_answer(1, 2, -1, "-")

4 print (answer2)

5 answer3 = check_answer(9, 5, 3, "+")

6 print (answer3)

7 answer4 = check_answer(8, 2, 4, "-")

8 print (answer4)

And here’s the expected output:

1 True

2 True

3 False

4 False

(f) Move all of your functions into an external file called Myfunctions. You can thenuse the functions in Myfunctions inside other files if you import.

(g) Put everything together and write a program that lets the user practice a series ofrandom addition and subtraction problems. Begin by asking the user for a numberof problems (only accept positive values) and a size for their numbers (only acceptnumbers between 5 and 10). Also prompt them for a single character to be usedto generate their patterns - only accept single character strings (i.e. ’a’ is OK,but ’apple’ is not). The generate a series of random addition and subtractionproblems - display the numbers to the user with your digital display functions.Then prompt the user for an answer and check the answer using your check answerfunction. Your program should also keep track of how many correct questions theuser answered during their game. The functions you defined should be . Here’s asample running of the program:

1 How many problems would you like to attempt? -5

2 Invalid number, try again

3

4 How many problems would you like to attempt? 5

5 How wide do you want your digits to be? 5-10: 3

6 Invalid width, try again

7

8 How wide do you want your digits to be? 5-10: 5

12

9

10 What character would you like to use? foo

11 String too long, try again

12 What character would you like to use? *

13

14 Here we go!

15

16 What is .....

17

18 *****

19 *

20 *****

21 *

22 *****

23

24 *

25 *

26 *****

27 *

28 *

29

30 *

31 *

32 *

33 *

34 *

35

36 = 4

37 Correct!

38

39 What is .....

40

41 *****

42 *

43 *****

44 *

45 *****

46

47

48

49 *****

50

51

52

13

53 *****

54 *

55 *

56 *

57 *

58

59 = -5

60 Correct!

61

62 What is .....

63

64 *

65 *

66 *

67 *

68 *

69

70

71

72 *****

73

74

75

76 *****

77 *

78 *****

79 *

80 *****

81

82 = 0

83 Sorry, that's not correct.

84

85 What is .....

86

87 *****

88 *

89 *****

90 *

91 *****

92

93 *

94 *

95 *****

96 *

14

97 *

98

99 *

100 *

101 *

102 *

103 *

104

105 = 3

106 Correct!

107

108 What is .....

109

110 *****

111 *

112 *****

113 *

114 *****

115

116 *

117 *

118 *****

119 *

120 *

121

122 *****

123 *

124 *****

125 *

126 *****

127

128 = 4

129 Correct!

130

131 You got 4 out of 5 correct!

(h) Add multiplication problems to the game. You will have to update your check answerfunction as well as add a new operator function to display the multiplication sign.Note that the visual representation of your multiplication sign does not need tobe ”perfect” - try and come up with a function that somewhat looks like a ”X”or ”*” character.

(i) Add division problems to the game. You will have to update your check answerfunction as well as add a new operator function to display the division sign. Fordivision problems you need to ensure that the result of the problem you present

15

is a whole number. For example, the following would be valid division problemsfor this game:

1 2 / 2 = 1

2 4 / 2 = 2

3 9 / 3 = 3

However, the following would NOT be valid since the answers are not wholenumbers:

1 5 / 2 = 2.5

2 9 / 8 = 1.125

3 8 / 3 = 2.6666666 (repeating)

Ensure that the division problems you supply to your players always yield a wholenumber result. You may need to generate a few different numbers in order to dothis (hint: while loop!).

(j) Add in a ”drill” mode to your game. If this mode is activated by the user theywill be re-prompted to solve a problem that they got incorrect. Points are turnedoff in ”drill” mode since the user can attempt a problem multiple times. Here’san example:

1 How many problems would you like to attempt? 2

2 How wide do you want your digits to be? 5-10: 5

3 Would you like to activate 'drill' mode? yes or no: yes

4

5 What is .....

6

7 *****

8 * *

9 * *

10 * *

11 *****

12

13 *

14 *

15 *****

16 *

17 *

18

19 *****

20 *

21 *****

22 *

16

23 *****

24

25 = 1

26 Sorry, that's not correct.

27 = 2

28 Sorry, that's not correct.

29 = 3

30 Correct!

31

32 What is .....

33

34 *****

35 * *

36 *****

37 * *

38 *****

39

40 *

41 *

42 *****

43 *

44 *

45

46 *****

47 *

48 *****

49 *

50 *****

51

52 = 13

53 Correct!

54

55 Thanks for playing!

(k) Keep track of statistics by problem type. For example, at the end of your programyou could display a display like the following that summarizes the performanceof the player:

1 Total addition problems presented: 5

2 Correct addition problems: 4 (80.0%)

3

4 Total subtraction problems presented: 4

5 Correct subtraction problems: 3 (75.0%)

6

17

7 No multiplication problems presented

8

9 Total division problems presented: 1

10 Correct division problems: 0 (0.0%)

After implementing ”drill” mode you should modify your statistics display toinclude information about how many ”extra” attempts it took so solve thoseproblems. For example:

1 Total addition problems presented: 5

2 # of extra attempts needed: 0 (perfect!)

3

4 Total subtraction problems presented: 4

5 # of extra attempts needed: 1

6

7 No multiplication problems presented

8

9 Total division problems presented: 1

10 # of extra attempts needed: 5

The main program should be named as follows: arithmetic game.py and the file withall the functions you implemented should be my functions.py.

18

2. NumbersWrite a series of five functions that determine if a given positive integer is EVEN,ODD, PRIME, PERFECT and ABUNDANT. Each of these functions should accepta single integer as an argument and return whether the integer meets the criteria forthat classification. Here’s some comments to get you started:

1 # function: is_even

2 # input: a positive integer

3 # processing: determines if the supplied number is even

4 # output: boolean

5

6 # function: is_odd

7 # input: a positive integer

8 # processing: determines if the supplied number is odd

9 # output: boolean

1 # function: is_prime

2 # input: a positive integer

3 # processing: determines if the supplied number is prime

4 # output: boolean

1 # function: is_perfect

2 # input: a positive integer

3 # processing: determines if the supplied number is perfect. a perfect number

4 # is a number that is equal to the sum of its factors (i.e. the

5 # number 6 is perfect because 6 = 1 + 2 + 3)

6 # output: boolean

1 # function: is_abundant

2 # input: a positive integer

3 # processing: determines if the supplied number is abundant. an abundant number

4 # is a number that is less than the sum of its factors (i.e. the

5 # number 12 is abundant because 12 < 1 + 2 + 3 + 4 + 6)

6 # output: boolean

Next, write a program that prompts the allows the user to analyze numbers withina given range for numbers that fit the above criteria. The program should continueto execute as long as the user wishes to keep going. Here’s a sample running of theprogram:

19

1 Main Menu

2

3 1 - Find all prime numbers within a given range

4 2 - Find all perfect numbers within a given range

5 3 - Find all abundant numbers within a given range

6 4 - Chart all even, odd, prime, perfect and abundant numbers within a given range

7 5 - Quit

8

9

10 Your choice: apple

11 I don't understand that ...

12

13

14 Main Menu

15

16 1 - Find all prime numbers within a given range

17 2 - Find all perfect numbers within a given range

18 3 - Find all abundant numbers within a given range

19 4 - Chart all even, odd, prime, perfect and abundant numbers within a given range

20 5 - Quit

21

22

23 Your choice: 1

24 Enter starting number (positive only): -5

25 Invalid, try again

26 Enter starting number (positive only): 5

27 Enter ending number: 3

28 Invalid, try again

29 Enter ending number: 20

30

31 All prime numbers between 5 and 20

32 ##############

33 5

34 7

35 11

36 13

37 17

38 19

39 ##############

40

41 Main Menu

42

43 1 - Find all prime numbers within a given range

20

44 2 - Find all perfect numbers within a given range

45 3 - Find all abundant numbers within a given range

46 4 - Chart all even, odd, prime, perfect and abundant numbers within a given range

47 5 - Quit

48

49

50 Your choice: 2

51 Enter starting number (positive only): 1

52 Enter ending number: 1000

53

54 All perfect numbers between 1 and 1000

55 ##############

56 6

57 28

58 496

59 ##############

60

61 Main Menu

62

63 1 - Find all prime numbers within a given range

64 2 - Find all perfect numbers within a given range

65 3 - Find all abundant numbers within a given range

66 4 - Chart all even, odd, prime, perfect and abundant numbers within a given range

67 5 - Quit

68

69

70 Your choice: 3

71 Enter starting number (positive only): 1

72 Enter ending number: 100

73

74 All abundant numbers between 1 and 100

75 ##############

76 12

77 18

78 20

79 24

80 30

81 36

82 40

83 42

84 48

85 54

86 56

87 60

21

88 66

89 70

90 72

91 78

92 80

93 84

94 88

95 90

96 96

97 100

98 ##############

99

100 Main Menu

101

102 1 - Find all prime numbers within a given range

103 2 - Find all perfect numbers within a given range

104 3 - Find all abundant numbers within a given range

105 4 - Chart all even, odd, prime, perfect and abundant numbers within a given range

106 5 - Quit

107

108

109 Your choice: 4

110 Enter starting number (positive only): 1

111 Enter ending number: 100

112

113 Number Even Odd Prime Perfect Abundant

114 1 x

115 2 x x

116 3 x x

117 4 x

118 5 x x

119 6 x x

120 7 x x

121 8 x

122 9 x

123 10 x

124 11 x x

125 12 x x

126 13 x x

127 14 x

128 15 x

129 16 x

130 17 x x

131 18 x x

22

132 19 x x

133 20 x x

134 21 x

135 22 x

136 23 x x

137 24 x x

138 25 x

139 26 x

140 27 x

141 28 x x

142 29 x x

143 30 x x

144 31 x x

145 32 x

146 33 x

147 34 x

148 35 x

149 36 x x

150 37 x x

151 38 x

152 39 x

153 40 x x

154 41 x x

155 42 x x

156 43 x x

157 44 x

158 45 x

159 46 x

160 47 x x

161 48 x x

162 49 x

163 50 x

164 51 x

165 52 x

166 53 x x

167 54 x x

168 55 x

169 56 x x

170 57 x

171 58 x

172 59 x x

173 60 x x

174 61 x x

175 62 x

23

176 63 x

177 64 x

178 65 x

179 66 x x

180 67 x x

181 68 x

182 69 x

183 70 x x

184 71 x x

185 72 x x

186 73 x x

187 74 x

188 75 x

189 76 x

190 77 x

191 78 x x

192 79 x x

193 80 x x

194 81 x

195 82 x

196 83 x x

197 84 x x

198 85 x

199 86 x

200 87 x

201 88 x x

202 89 x x

203 90 x x

204 91 x

205 92 x

206 93 x

207 94 x

208 95 x

209 96 x x

210 97 x x

211 98 x

212 99 x

213 100 x x

214 Main Menu

215

216 1 - Find all prime numbers within a given range

217 2 - Find all perfect numbers within a given range

218 3 - Find all abundant numbers within a given range

219 4 - Chart all even, odd, prime, perfect and abundant numbers within a given range

24

220 5 - Quit

221

222

223 Your choice: 5

224

225 Goodbye!

Note that you might want to write some additional ”helper” functions - for example,the options above all require the user to supply a range of numbers to analyze. Youcan probably write a function that does this so you don’t have to continually copy andpaste the same code over and over again.

Your program should be called: numbers.py.

25