View the step-by-step solution to:

Java data structures: abstraction and design.Page 469, Project 8 A comple te implementation of this program is left as a programming project. we show...

Java

data structures: abstraction and design.Page 469, Project 8


A complete implementation of this program is left as a programming project. we show the coding of the sort algorithm in listing 8.12.



 public void sort() {  int red = 0;

    int white = height - 1;

    int blue = height - 1;

    // Invariant:

    // 0 <= i < red   ==> threads[i].getColor() == Color.RED

    // red <= i <= white ==> threads[i].getColor() is unknown

    // white < i < blue ==> threads[i].getColor() == Color.WHITE

    // blue < i < height ==> threads[i].getColor() == Color.BLUE

    while (red <= white) {

      if (threads[white].getColor() == Color.WHITE) {

        --white;

      } else if (threads[white].getColor() == Color.RED) {

        swap(red, white);

        ++red;

      } else { // threads[white].getColor() == Color.BLUE

        swap(white, blue);

        --white;

        --blue;

      }

      repaint();

    }

    // red == white so unknown region is now empty

  }

Complete the Dutch National Flag case study. You will need to develop the following classes:

a. A main class that extends JFrame to contain the flag and a control button (Sort).

b. A class to represent the flag; an extension of JPanel is suggested. This class will contain the array of threads and the sort method.

c. A class to represent a thread. Each thread should have a color and a method to draw the thread.

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question