Related questions with answers
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
VerifiedWe'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 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
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(
(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•ISBN: 9780124077263David A. Patterson, John L. Hennessy

Fundamentals of Database Systems
7th Edition•ISBN: 9780133970777 (2 more)Ramez Elmasri, Shamkant B. Navathe
Introduction to Algorithms
3rd Edition•ISBN: 9780262033848 (5 more)Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. CormenMore related questions
1/4
1/7