Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the Dutch National Flag case study. You will need to develop the follow

ID: 3829586 • Letter: C

Question

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

a. A main class that extends JFrame to control 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.

The sort method for part b is as follows:

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, g);

red++;

}

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

{

swap(white, blue, g);

white--;

blue--;

}

}

//assert: red == white so unknown region is now empty.

}

Explanation / Answer

a.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener
{
    private JLabel label11;
    private JTextField fields;
    public Test()
    {
        super("title");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(400, 90));
        ((JPanel) getContentPane()).setBorder(new EmptyBorder(13, 13, 13, 13) );
        setLayout(new FlowLayout());
        JButton butn = new JButton("The Change");
        butn.setActionCommand("myButton");
        butn.addActionListener(this);
        label11 = new JLabel("flag");
        fields = new JTextField(5);
        add(fields);
        add(butn);
        add(label11);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("myButton"))
        {
            label11.setText(fields.getText());
        }
    }
    public static void main(String[] args)
    {
        new Test();
    }
}

B.

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;

public class AmericanFlag extends JPanel
{  
   private double h;
   private double w;
   public AmericanFlag(double tempW, double tempH)
   {
       w = tempW;
       h = tempH;  
   }
  
   public void paintComponent(Graphics g)
   {
      
       Graphics2D g21 = (Graphics2D) g;
       double rectW = .6*w;
       double xCord = .4*w;
       g21.setPaint(Color.BLUE);
       g21.fill(new Rectangle2D.Double(0,0,.4*w,.53846*h));      
       for(int i=0; i<13; i++)
       {
           if(i==7)
           {
               xCord = 0;
               rectW = w;  
           }
          
           if(i%2==0)
               g21.setPaint(Color.RED);
           else
               g21.setPaint(Color.WHITE);
          
           g21.fill(new Rectangle2D.Double(xCord,i*.0769*h,rectW,.0769*h));  
       }
      
  
       double x[] = {.0002105*w, .0126*w, .0163*w, .02*w, .0324*w,
                      .0217*w, .02574*w, .0163*w, .0069*w, .0109*w};
        double y[] = {.02*h, .02*h, 0, .02*h, .02*h, .03*h,
                      .05*h, .04*h, .05*h, .03*h};
      
       GeneralPath stars = new GeneralPath();
                 stars.moveTo((float)x[0],(float)y[0]);
              for(int i=1; i<x.length; i++)
        {
           stars.lineTo((float)x[i],(float)y[i]);
        }
            stars.closePath();
        g21.translate(.017*w,.0232*h);
        g21.setPaint(Color.WHITE);
        g21.fill(stars);
           for(int i=0; i<5; i++)
        {
           g21.translate(.066316*w,0);
            g21.fill(stars);
        }
      
   
        for(int i=0; i<4; i++)
        {
          
           g21.translate(-.03316*w,.054*h);
            g21.fill(stars);
        
           for(int j=0; j<4; j++)
            {
               g21.translate(-.066316*w,0);
                g21.fill(stars);
            }
        
            g21.translate(-.03316*w,.054*h);
            g21.fill(stars);
      
            for(int k=0; k<5; k++)
            {
               g21.translate(.066316*w,0);
                g21.fill(stars);
            }
        }    
   }
}

C.
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
public class Flag extends JPanel {
    private Thread[] threads11;
    private int w;
    private int h;
    public Flag(int w, int h) {
        this.w = w;
        this.h = h;
        threads11 = new Thread[h];
        for (int i = 0; i != h / 3; ++i) {
            threads11[i] = new Thread(Color.RED);
        }
        for (int i = h / 3; i != 2 * h / 3; ++i) {
            threads11[i] = new Thread(Color.WHITE);
        }
        for (int i = 2 * h / 3; i != h; ++i) {
            threads11[i] = new Thread(Color.BLUE);
        }
        randomize();
    }
    public void randomize() {
        for (int i = 0; i != h; ++i) {
            int j = (int) (Math.random() * h);
            Thread temp = threads11[i];
            threads11[i] = threads11[j];
            threads11[j] = temp;
        }
    }
       public void sort() {
        int redcol = 0;
        int whitecol = h - 1;
        int bluecol = h - 1;
               while (redcol <= whitecol) {
            if (threads11[whitecol].getColor() == Color.WHITE) {
                --whitecol;
            } else if (threads[whitecol].getColor() == Color.RED) {
                swap(redcol, whitecol);
                ++redcol;
            } else {
                swap(whitecol, bluecol);
                --whitecol;
                --bluecol;
            }
            repaint();
        }
            }
       private void swap(int i, int j) {
        Thread temp = threads11[i];
        threads11[i] = threads11[j];
        threads11[j] = temp;
    }

     @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i != h; ++i) {
            threads11[i].draw(i, w, g);
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote