Fresh features from the #1 AI-enhanced learning platform.Try it free
Fresh features from the #1 AI-enhanced learning platformCrush your year with the magic of personalized studying.Try it free

Related questions with answers

Revise BounceBalls.py to add two buttons—Faster and Slowerto speed up or slow down the ball movements.

BounceBalls.py
1   from tkinter import * # Import all definitions from tkinter
2   from random import randint
3
4   # Return a random color string in the form #RRGGBB
5   def getRandomColor():
6       color = "#"
7       for j in range(6):
8           color += toHexChar(randint(0, 15)) # Add a random digit
9       return color
10
11  # Convert an integer to a single hex digit in a character
12  def toHexChar(hexValue):
13      if 0 <= hexValue <= 9:
14          return chr(hexValue + ord('0'))
15      else: # 10 <= hexValue <= 15
16          return chr(hexValue - 10 + ord('A'))
17
18  # Define a Ball class
19  class Ball:
20      def __init__(self):
21          self.x = 0 # Starting center position
22          self.y = 0
23          self.dx = 2 # Move right by default
24          self.dy = 2 # Move down by default
25          self.radius = 3 # The radius is fixed
26          self.color = getRandomColor() # Get random color
27
28  class BounceBalls:
29      def __init__(self):
30          self.ballList = [] # Create a list for balls
31
32          window = Tk() # Create a window
33          window.title("Bouncing Balls") # Set a title
34
35          self.width = 350 # Width of the self.canvas
36          self.height = 150 # Height of the self.canvas
37          self.canvas = Canvas(window, bg = "white",
38              width = self.width, height = self.height)
39          self.canvas.pack()
40
41          frame = Frame(window)
42          frame.pack()
43          btStop = Button(frame, text = "Stop", command = self.stop)
44          btStop.pack(side = LEFT)
45          btResume = Button(frame, text = "Resume",
46              command = self.resume)
47          btResume.pack(side = LEFT)
48          btAdd = Button(frame, text = "+", command = self.add)
49          btAdd.pack(side = LEFT)
50          btRemove = Button(frame, text = "-", command = self.remove)
51          btRemove.pack(side = LEFT)
52
53          self.sleepTime = 100 # Set a sleep time
54          self.isStopped = False
55          self.animate()
56
57          window.mainloop() # Create an event loop
58
59      def stop(self): # Stop animation
60          self.isStopped = True
61
62      def resume(self): # Resume animation
63          self.isStopped = False
64          self.animate()
65
66      def add(self): # Add a new ball
67          self.ballList.append(Ball())
68      
69      def remove(self): # Remove the last ball
70          self.ballList.pop()
71
72      def animate(self): # Animate ball movements
73          while not self.isStopped:
74              self.canvas.after(self.sleepTime) # Sleep
75              self.canvas.update() # Update self.canvas
76              self.canvas.delete("ball")
77
78              for ball in self.ballList:
79                  self.redisplayBall(ball)
80
81      def redisplayBall(self, ball):
82          if ball.x > self.width or ball.x < 0:
83              ball.dx = -ball.dx
84
85          if ball.y > self.height or ball.y < 0:
86              ball.dy = -ball.dy
87
88          ball.x += ball.dx
89          ball.y += ball.dy
90          self.canvas.create_oval(ball.x - ball.radius,
91              ball.y - ball.radius, ball.x + ball.radius,
92              ball.y + ball.radius, fill = ball.color, tags = "ball")
93
94  BounceBalls() # Create GUI
Question

Write a program that animates the selection-sort algorithm. Create a list that consists of 20 distinct numbers from 1 to 20 in a random order. The elements are displayed in a histogram. Clicking the Step button causes the program to perform an iteration of the outer loop in the algorithm and repaints the histogram for the new list. Color the last bar in the sorted sublist. When the algorithm is finished, display a dialog box to inform the user. Clicking the Reset button creates a new random list for a new start.

Solution

Verified
Step 1
1 of 12

We've been using a function called \verb|drawHistogram| to draw the histogram onto a Canvas object. But this time, I've created a new class called Histogram that we'll create. It's a subclass of the Canvas class. Subclasses will be covered in more detail in Chapter 12. Until then, all you need to know is that

  • you can create classes that inherit\textit{inherit} all of the methods and data attributes of an existing class, plus any extra that you decide to add. That type of class is called a subclass.
  • subclasses are created with the header

class ():

instead of the usual \verb|class | syntax. Example: In my code below we create the subclass \verb|Histogram| with the header

class Histogram(Canvas):

  • a subclass has access to all of the methods and data attributes of its parent class. Example: to access the canvas method \verb|create_rectangle| from a Histogram object named \verb|hist|, you can just write \verb|hist.create_rectangle()|, as you would expect.
  • a subclass can overwrite a method from the base class (see the constructor for my Histogram class below).
  • if a subclass has overwritten a method from the base class, you can still call the base class's version of the method using the syntax

super(, self).()

(again, see the constructor for my Histogram subclass).

Create an account to view solutions

Create an account to view solutions

Recommended textbook solutions

Computer Organization and Design MIPS Edition: The Hardware/Software Interface 5th Edition by David A. Patterson, John L. Hennessy

Computer Organization and Design MIPS Edition: The Hardware/Software Interface

5th EditionISBN: 9780124077263David A. Patterson, John L. Hennessy
226 solutions
Introduction to Programming Using Python 1st Edition by Y. Daniel Liang

Introduction to Programming Using Python

1st EditionISBN: 9780132747189Y. Daniel Liang
773 solutions
Fundamentals of Database Systems 7th Edition by Ramez Elmasri, Shamkant B. Navathe

Fundamentals of Database Systems

7th EditionISBN: 9780133970777 (2 more)Ramez Elmasri, Shamkant B. Navathe
866 solutions
Introduction to Algorithms 3rd Edition by Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen

Introduction to Algorithms

3rd EditionISBN: 9780262033848 (5 more)Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
849 solutions

More related questions

1/4

1/7