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

Assignment List vs Set with GUI CSIS-1410 Learning Objectives : Use the internet

ID: 664913 • Letter: A

Question

Assignment List vs Set with GUI CSIS-1410 Learning Objectives : Use the internet to learn about Set, HashSet, and other topics Understand the 2 major differences between interface Set and interface List Create, initialize, and display a HashSet Practice implementing a GUI application with WindowBuilder Experience the benefit of separating functionality from display Description: Download the starter project and import it into Eclipse. Implement the classes ColoredSquare and ListVsSetDemo as described below. Use the console app provided to test your classes. If the program doesn’t work as expected change your code and not the console app. Then modify the gui Application as described below so that it demonstrates the differences between the interfaces List and Set. Ad ColoredSquare: Implement ColoredSquare as specified in the UML diagram. Do not add or remove any class members. Important: ColoredSquare should NOT include any print methods equals: 2 colored squares should be considered equal if they have the same size and color. Use Eclipse to auto-implement equals and hashCode toString: Eclipse can atuo-implement toString but in our case that would be a rather verbose description. Because of that I want you to write your own implementation that produces an output as shown on the right. e.g. side:14 #0000FF side:12 #FFFF00 etc. Notice: the toString method of Color displays the rgb values in decimal format, however, in our implementation we use a hexadecimal format like in HTML. Hint 1: To get the individual red, green, blue values check out class Color . Hint 2: Info on how to format an integer as a 2-digit hexadecimal value Console Output: List: side:14 #0000FF side:18 #FF0000 side:12 #FFFF00 side:18 #FF0000 side:16 #00FF00 Set: side:14 #0000FF side:12 #FFFF00 side:16 #00FF00 side:18 #FF0000 Adding a new element: List: side:14 #0000FF side:18 #FF0000 side:12 #FFFF00 side:18 #FF0000 side:16 #00FF00 side:10 #FFC800 Set: side:14 #0000FF side:12 #FFFF00 side:10 #FFC800 side:16 #00FF00 side:18 #FF0000 Adding a duplicate element: List: side:14 #0000FF side:18 #FF0000 side:12 #FFFF00 side:18 #FF0000 side:16 #00FF00 side:10 #FFC800 side:10 #FFC800 Set: side:14 #0000FF side:12 #FFFF00 side:10 #FFC800 side:16 #00FF00 side:18 #FF0000 Ad ListVsSetDemo: Implement ListVsSetDemo as specified in the UML diagram. Important: ListVsSetDemo should NOT include any print methods Notice: even though the parameter of the constructor is defined as an array of type ColoredSquare, I want you to implement it as varargs. This is a good review and it provides a convenient way to call the constructor. Here is a short video in case you’d like a refresher on varargs: constructor: The constructor uses the elements passed to initialize both the list and the set getListElements, getSetElements: return a formatted string containing all the elements of the collection - one element per line. (see Console Output) Hint: use StringBuilder to create that string addElement: Adds the element passed to both the list and the set Ad ListVsSetGuiApp: Just as the console app calls methods of class ListVsSetDemo I want you to call methods of ListVsSetDemo to generate the text that will be displayed in ListVsSetGui when the Demo tab is selected. The starter project includes two radio buttons: one to display the list elements the other to display the set elements. Add a third radio button that allows adding a new element. (Just like in the console app it needs to be an element that has not been included in the original list. Every time the third radio button is selected the same ‘new element’ is added). Make sure to let the user know that an element got added. If one of the first two radio buttons is selected after adding an element the updated list / set should be displayed. When the user selects the ListVsSet menu item the two main differences between the interfaces List and Set should be displayed. No control panel is needed in this situation. When the user selects the Exit menu item, the application should close. Turning in: Ensure to include your name on top of each java file that you modified. Then zip up all four java files and turn them in via Canvas.

Here is the code provided:

ListVsSetGui

package listVsSet;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.GridLayout;
import java.awt.SystemColor;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class ListVsSetGui extends JFrame {

private JPanel contentPane;

private JMenuBar menuBar;
private JMenuItem mntnListVsSet;
private JMenuItem mntmDemo;
private JMenuItem mntnExit;

private JTextArea listVsSetTextArea;

private JPanel demoControlPanel;
private JLabel lblYourChoice;
private JRadioButton rdbtnListELements;
private JRadioButton rdbtnSetElements;

private final ButtonGroup buttonGroup = new ButtonGroup();

private JTextArea demoTextArea;
private JPanel ListVsSetPanel;
private JLabel lblNewLabel;
private JLabel lblOnePanelToo;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
ListVsSetGui frame = new ListVsSetGui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public ListVsSetGui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50, 50, 700, 450);

createMenu();
createListVsSetTextArea();
createDemoControlPanel();
createDemoTextArea();

createContentPane();
}

private void createContentPane() {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.setFont(new Font("Verdana", Font.PLAIN, 26));
setContentPane(contentPane);

// contentPane.add(demoControlPanel, BorderLayout.WEST);
// contentPane.add(demoTextArea, BorderLayout.CENTER);

contentPane.add(listVsSetTextArea, BorderLayout.CENTER);

ListVsSetPanel = new JPanel();
ListVsSetPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
contentPane.add(ListVsSetPanel, BorderLayout.WEST);
ListVsSetPanel.setLayout(new GridLayout(7, 1, 0, 0));

lblOnePanelToo = new JLabel("One panel too many.");
ListVsSetPanel.add(lblOnePanelToo);

lblNewLabel = new JLabel("This panel needs to go");
ListVsSetPanel.add(lblNewLabel);
}

private void createDemoTextArea() {
demoTextArea = new JTextArea();
demoTextArea.setBackground(UIManager.getColor("TextField.light"));
demoTextArea.setFont(new Font("Consolas", Font.PLAIN, 16));
demoTextArea.setMargin(new Insets(25, 25, 25, 25));
}

private void createDemoControlPanel() {
demoControlPanel = new JPanel();
demoControlPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
demoControlPanel.setLayout(new GridLayout(12, 1, 0, 0));

lblYourChoice = new JLabel("Your Choice:");
lblYourChoice.setFont(new Font("Consolas", Font.BOLD, 14));
demoControlPanel.add(lblYourChoice);

rdbtnListELements = new JRadioButton("List Elements");
rdbtnListELements.setFont(new Font("Consolas", Font.PLAIN, 14));
buttonGroup.add(rdbtnListELements);
demoControlPanel.add(rdbtnListELements);

rdbtnSetElements = new JRadioButton("Set Elements");
rdbtnSetElements.setFont(new Font("Consolas", Font.PLAIN, 14));
buttonGroup.add(rdbtnSetElements);
demoControlPanel.add(rdbtnSetElements);

}

private void createListVsSetTextArea() {
listVsSetTextArea = new JTextArea(
"The 2 main differences between interface List and Set are: " +
"1. " +
"2. ");
listVsSetTextArea.setMargin(new Insets(25, 25, 25, 25));
listVsSetTextArea.setForeground(new Color(255, 255, 255));
listVsSetTextArea.setOpaque(true);
listVsSetTextArea.setBackground(SystemColor.inactiveCaptionText);
listVsSetTextArea.setFont(new Font("Verdana", Font.PLAIN, 18));
}

private void createMenu() {
menuBar = new JMenuBar();
setJMenuBar(menuBar);

mntmDemo = new JMenuItem("Demo");
mntmDemo.setHorizontalAlignment(SwingConstants.CENTER);
mntmDemo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
contentPane.removeAll();
contentPane.add(demoControlPanel, BorderLayout.WEST);
contentPane.add(demoTextArea, BorderLayout.CENTER);
revalidate();
repaint();
}
});
menuBar.add(mntmDemo);

mntnListVsSet = new JMenuItem("List vs Set");
mntnListVsSet.setHorizontalAlignment(SwingConstants.CENTER);
mntnListVsSet.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
contentPane.removeAll();
contentPane.add(listVsSetTextArea, BorderLayout.CENTER);
revalidate();
repaint();
}
});
menuBar.add(mntnListVsSet);

mntnExit = new JMenuItem("Exit");
mntnExit.setHorizontalAlignment(SwingConstants.CENTER);
menuBar.add(mntnExit);
}

}

ListVsSetDemo

package listVsSet;

public class ListVsSetDemo {

}

ListVsSetConsole

package listVsSet;

public class ListVsSetConsoleApp {

public static void main(String[] args) {

/*

   * uncomment the code to test ListVsSetDemo

   *

   *

   * // notice: I pass the individual elements, // because the constructor is implemented with vararg

   **/

   ListVsSetDemo demo = new ListVsSetDemo( new ColoredSquare(14, Color.BLUE), new ColoredSquare(18, Color.RED),

   new ColoredSquare(12, Color.YELLOW), new ColoredSquare(18, Color.RED), new ColoredSquare(16, Color.GREEN));

   System.out.println("List: " + demo.getListElements()); System.out.println("Set: " + demo.getSetElements());

  

   System.out.println(" Adding a new element:"); demo.addElement(new ColoredSquare(10, Color.ORANGE));

   System.out.println("List: " + demo.getListElements()); System.out.println("Set: " + demo.getSetElements());

   System.out.println(" Adding a duplicate element:"); demo.addElement(new ColoredSquare(10, Color.ORANGE));

   System.out.println("List: " + demo.getListElements()); System.out.println("Set: " + demo.getSetElements());

}

}

ColoredSquare

package listVsSet;

public class ColoredSquare {

}

Explanation / Answer

package gui;

import java.awt.Color;


public class ColoredSquare {
   private int i;
   private Color color;
   public ColoredSquare(int i, Color color) {
       this.i=i;
       this.color=color;
   }

   @Override
   public String toString() {
      
       return "i: "+i+" color: "+color;
   }
}

package gui;
import java.awt.Color;

public class ListVsSetConsoleApp {

    public static void main(String[] args) {

         ListVsSetDemo demo = new ListVsSetDemo( new ColoredSquare(14, Color.BLUE), new ColoredSquare(18, Color.RED),

         new ColoredSquare(12, Color.YELLOW), new ColoredSquare(18, Color.RED), new ColoredSquare(16, Color.GREEN));

         System.out.println("List: " + demo.getListElements()); System.out.println("Set: " + demo.getSetElements());         System.out.println(" Adding a new element:"); demo.addElement(new ColoredSquare(10, Color.ORANGE));

         System.out.println("List: " + demo.getListElements()); System.out.println("Set: " + demo.getSetElements());

                  System.out.println(" Adding a duplicate element:"); demo.addElement(new ColoredSquare(10, Color.ORANGE));

         System.out.println("List: " + demo.getListElements()); System.out.println("Set: " + demo.getSetElements());

             }

}

package gui;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ListVsSetDemo {

   List<ColoredSquare> coloredList=new ArrayList<ColoredSquare>();

   Set<ColoredSquare> coloredSet=new HashSet<ColoredSquare>();

   public ListVsSetDemo(ColoredSquare coloredSquare1,
           ColoredSquare coloredSquare2, ColoredSquare coloredSquare3,
           ColoredSquare coloredSquare4, ColoredSquare coloredSquare5) {
       addElement(coloredSquare1);
       coloredSet.add(coloredSquare1);
       addElement(coloredSquare2);
       coloredSet.add(coloredSquare2);
       addElement(coloredSquare3);
       coloredSet.add(coloredSquare3);
       addElement(coloredSquare4);
       coloredSet.add(coloredSquare4);
       addElement(coloredSquare5);
       coloredSet.add(coloredSquare5);
   }

   public List<ColoredSquare> getListElements() {
       // TODO Auto-generated method stub
       return coloredList;
   }

   public void addElement(ColoredSquare coloredSquare) {
       coloredList.add(coloredSquare);
   }

   public Set<ColoredSquare> getSetElements() {
       // TODO Auto-generated method stub
       return coloredSet;
   }


}

package gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.GridLayout;
import java.awt.SystemColor;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class ListVsSetGui extends JFrame {

    private JPanel contentPane;

    private JMenuBar menuBar;
    private JMenuItem mntnListVsSet;
    private JMenuItem mntmDemo;
    private JMenuItem mntnExit;

    private JTextArea listVsSetTextArea;

    private JPanel demoControlPanel;
    private JLabel lblYourChoice;
    private JRadioButton rdbtnListELements;
    private JRadioButton rdbtnSetElements;
    private JRadioButton newradioElements;

    private final ButtonGroup buttonGroup = new ButtonGroup();

    private JTextArea demoTextArea;
    private JPanel ListVsSetPanel;
    private JLabel lblNewLabel;
    private JLabel lblOnePanelToo;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    ListVsSetGui frame = new ListVsSetGui();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ListVsSetGui() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(50, 50, 700, 450);

        createMenu();
        createListVsSetTextArea();
        createDemoControlPanel();
        createDemoTextArea();

        createContentPane();
    }

    private void createContentPane() {
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        contentPane.setFont(new Font("Verdana", Font.PLAIN, 26));
        setContentPane(contentPane);

        // contentPane.add(demoControlPanel, BorderLayout.WEST);
        // contentPane.add(demoTextArea, BorderLayout.CENTER);

        contentPane.add(listVsSetTextArea, BorderLayout.CENTER);

        ListVsSetPanel = new JPanel();
        ListVsSetPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
        contentPane.add(ListVsSetPanel, BorderLayout.WEST);
        ListVsSetPanel.setLayout(new GridLayout(7, 1, 0, 0));

        lblOnePanelToo = new JLabel("One panel too many.");
        ListVsSetPanel.add(lblOnePanelToo);

        lblNewLabel = new JLabel("This panel needs to go");
        ListVsSetPanel.add(lblNewLabel);
    }

    private void createDemoTextArea() {
        demoTextArea = new JTextArea();
        demoTextArea.setBackground(UIManager.getColor("TextField.light"));
        demoTextArea.setFont(new Font("Consolas", Font.PLAIN, 16));
        demoTextArea.setMargin(new Insets(25, 25, 25, 25));
    }

    private void createDemoControlPanel() {
        demoControlPanel = new JPanel();
        demoControlPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
        demoControlPanel.setLayout(new GridLayout(12, 1, 0, 0));

        lblYourChoice = new JLabel("Your Choice:");
        lblYourChoice.setFont(new Font("Consolas", Font.BOLD, 14));
        demoControlPanel.add(lblYourChoice);

        rdbtnListELements = new JRadioButton("List Elements");
        rdbtnListELements.setFont(new Font("Consolas", Font.PLAIN, 14));
        buttonGroup.add(rdbtnListELements);
        demoControlPanel.add(rdbtnListELements);

      
        rdbtnSetElements = new JRadioButton("Set Elements");
        rdbtnSetElements.setFont(new Font("Consolas", Font.PLAIN, 14));
        buttonGroup.add(rdbtnSetElements);
        demoControlPanel.add(rdbtnSetElements);
      
        newradioElements= new JRadioButton("Add New Element");
        newradioElements.setFont(new Font("Consolas", Font.PLAIN, 14));
        buttonGroup.add(newradioElements);
        demoControlPanel.add(newradioElements);
         
    }

    private void createListVsSetTextArea() {
        listVsSetTextArea = new JTextArea(
                "The 2 main differences between interface List and Set are: " +
                        "1.List is an ordered collection (also known as a sequence). " +
                        "The user of this interface has precise control over where in " +
                        "the list each element is inserted.The user can access elements " +
                        " by their integer index (position in the list), and search for " +
                        " elements in the list. " +
                        "2.A set collection that contains no duplicate elements. " +
                        "More formally, sets contain no pair of elements e1 and e2 " +
                        "such that e1.equals(e2),and at most one null element. " +
                        "As implied by its name, this interface models the mathematical " +
                        "set abstraction. ");
        listVsSetTextArea.setMargin(new Insets(25, 25, 25, 25));
        listVsSetTextArea.setForeground(new Color(255, 255, 255));
        listVsSetTextArea.setOpaque(true);
        listVsSetTextArea.setBackground(SystemColor.inactiveCaptionText);
        listVsSetTextArea.setFont(new Font("Verdana", Font.PLAIN, 18));
    }

    private void createMenu() {
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        mntmDemo = new JMenuItem("Demo");
        mntmDemo.setHorizontalAlignment(SwingConstants.CENTER);
        mntmDemo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                contentPane.removeAll();
                contentPane.add(demoControlPanel, BorderLayout.WEST);
                contentPane.add(demoTextArea, BorderLayout.CENTER);
                revalidate();
                repaint();
            }
        });
        menuBar.add(mntmDemo);

        mntnListVsSet = new JMenuItem("List vs Set");
        mntnListVsSet.setHorizontalAlignment(SwingConstants.CENTER);
        mntnListVsSet.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                contentPane.removeAll();
                contentPane.add(listVsSetTextArea, BorderLayout.CENTER);
                revalidate();
                repaint();
            }
        });
        menuBar.add(mntnListVsSet);

        mntnExit = new JMenuItem("Exit");
        mntnExit.setHorizontalAlignment(SwingConstants.CENTER);
        mntnExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        menuBar.add(mntnExit);
      
    }

}

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