Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q. Need help with question number 1 as to how to build factory class for charts according to the fragments of code. here is the

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Q. Need help with question number 1 as to how to build factory class for charts according to the fragments of code.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedhere is the updated code.

There are two re-engineering tasks to accomplish: 1. The various charts are sub-classes of DataComponent. In the present implementation the main application creates and manages the instances of the chart objects itself. Change this so that all references to the concrete implementations of the charts are confined to a single factory class and so that new chart types can be incorporated solely by making changes to the factory class. There is an additional DataComponent class called SquareChart in the datadisplay package: once you have implemented your factory class you should add the SquareChart to the application without modifying any code except in the factory class itself. 2. The present application is almost entirely contained in a single class that extends JFrame. You are required to refactor this into (at least) three classes that represent the model, view and controller architecture, or an adaptation of it. You will need to decide what functionality belongs in each of these three layers. There is no right answer to this but you must justify your decisions and contrast them with alternatives you could have chosen. You are permitted to be flexible in how you apply the MVC architecture as long as you describe what you do and why (for example, you might think that an MVA architecture is more appropriate, or you might choose not to follow the "view -> controller -> model-> view" cycle of method calls rigidly). public class Dashboard extends JFrame { private static final long serialVersionUID = IL; private final Control Panel control Panel; private enum ChartType { BAR, PIE, LINE, SQUARE } // map of DataComponent objects ready to be displayed private final Map(); // list of currently displayed DataComponent objects (some of the objects in dataComponentobjects). private final LinkedHashSet dataComponents = new LinkedHashSet(); private final JPanel dataComponentPanel; private final TreeMap map = new TreeMap(); public Dashboard() { setDefaultCloseOperation (WindowConstants. DISPOSE_ON_CLOSE) ; // create the panel that has the controls on this.control Panel = new Control Panel (this); final JPanel mainPanel = new JPanel ( new BorderLayout()); mainPanel.add(this.controlpanel, BorderLayout. NORTH); // add a menu to the window final JMenuBar menuBar = new JMenuBar(); final JMenu chartMenu = new JMenu ("Charts"); // add a menu item for each type of chart as defined in the ChartType enum for( final Chart Type type : ChartType.values()) { final JMenuItem item = new JMenuItem( type.toString()); item.addActionListener( (ev) ->doMenu (type)); chartMenu.add( item); } menuBar.add(chartMenu); set JMenuBar (menuBar); DataTable.java x Line Graph.java x ...va ET Dashboard.java x AbstractChart.java x BarChart.java x DataComponent.java x Source History KE D 75 } 76 menuBar.add (chartMenu); 77 set JMenuBar (menuBar); 78 79 this.dataComponentPanel = new JPanel (new GridLayout(0,1)); 80 DataTable dataTable = new DataTable(); 81 this.dataComponents.add(dataTable); 82 83 final JSplit Pane horizontalPanel = new JSplitPane( JSplitPane. HORIZONTAL SPLIT); mainPanel.add (horizontalPanel, BorderLayout. CENTER); 84 85 86 horizontal Panel.add (dataTable); horizontalPanel.add(this.dataComponent Panel); 87 88 89 90 91 // construct an instance of each type of chart and store them in a map // these objects are only added to the UI on demand when the user selects them in the menu this.dataComponentobjects.put(ChartType.BAR, new Barchart()); this.dataComponentobjects.put(ChartType. PIE, new Piechart()); this.dataComponentobjects.put(ChartType. LINE, new LineGraph()); this.dataComponentobjects.put(ChartType. SQUARE, new SquareChart()); 92 93 94 95 96 97 mainPanel.add (horizontalPanel, BorderLayout. CENTER); add (main Panel); pack(); setVisible(true); 98 99 100 private void domenu (final ChartType type) { final DataComponent dc = this.dataComponentobjects.get (type); if ( !this.dataComponents.remove (dc)) { this.dataComponentPanel.add(dc); this.dataComponents.add(dc); } else { this.dataComponentPanel.remove (dc); } updateData(); this.revalidate(); this.repaint(); } Start Page x Control Panel.java x Dashboard.java x AbstractChart.java x BarChart.java x | Datacomp Source History Q25EL 9 import javax.swing.BorderFactory; 10 import javax.swing.ButtonGroup; 11 import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JPanel; 14 import javax.swing.JRadioButton; 15 import javax.swing.JTextField; 16 17 public class Control Panel extends JPanel { 12 13 18 19 private static final long serialversionUID = 1L; 20 21 22 . 23 private final JButton browseButton = new JButton( "Choose folder"); private final JTextField folderField = new JTextField( 60); private final JRadioButton byLabelRadioButton = new JRadioButton ("by name"); private final JRadioButton byvalueRadioButton = new JRadioButton ("by value"); 24 25 26 private final JFileChooser chooser = new JFileChooser(); 27 28 29 public Control Panel (Dashboard dashboard) { super( new BorderLayout()); 30 31 32 final JPanel folder Panel = new JPanel( new BorderLayout()); folderPanel.setBorder (BorderFactory.createTitledBorder("Folder")); folderPanel.add(this. folderField, BorderLayout.CENTER); folderPanel.add(this.browseButton, BorderLayout. EAST); 33 3 3 34 35 public Control Panel (Dashboard dashboard) { super( new BorderLayout()); final JPanel folder Panel = new JPanel( new BorderLayout()); folderPanel.setBorder (BorderFactory.createTitledBorder("Folder")); folderPanel.add(this.folderField, BorderLayout. CENTER); folderPanel.add(this.browseButton, BorderLayout. EAST); final JPanel sortPanel = new JPanel( new GridLayout( 0, 2)); sortPanel.setBorder (BorderFactory.createTitledBorder("Sort")); sortPanel.add(this.byLabelRadioButton); sortPanel.add(this.byValueRadioButton); final ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(this.byLabelRadioButton); buttonGroup.add(this.byValueRadioButton); add ( folder Panel, BorderLayout. CENTER); add( sortPanel, BorderLayout. EAST); this.chooser.setFileSelectionMode (JFileChooser.DIRECTORIES ONLY); this.chooser.setMultiselectionEnabled(false); this browseButton.addActionListener(new ActionListener() { @Override @Override public void actionPerformed (final ActionEvent e) { final int choice = Control Panel.this.chooser.showOpenDialog (Controlpanel.this); if (choice == JFileChooser. APPROVE OPTION) { dashboard.setFolder (Controlpanel.this.chooser.getSelectedFile()); }}); this.byLabelRadioButton.setSelected(true); this.byLabelRadioButton.addActionListener((ev) -> dashboard. updateData()); this.byValueRadioButton.addActionListener( (ev) -> dashboard. updateData()); this.folderField.addActionListener((ev) -> dashboard. setFolder (new File( this. folderField.getText()))); } /** Returns true if the "by name" radio button is selected. */ public boolean sortedonLabels() return this.byLabelRadioButton.isSelected(); /** Updates the text in the folderField when a new folder has been selected in the chooser. */ public void setFolder( final File folder) { this. folder Field.setText( folder.getPath()); } 02122.Control Panel sortedOnlabels public abstract class DataComponent extends JComponent { private static final long serialVersionUID = 1L; protected String[] labels = new String[0]; protected double[] data = new double[0]; protected double[] sumtoone = new double[0]; protected double[] norm = new double [0]; protected Palette colors; ] public DataComponent() { setPreferredsize (new Dimension (200, 200)); } ] public void setData( final String[] labels, final double[] data) { this. labels = labels; this.data = data; double total = 0; for (int i = 0; i max) { max = this.sumToone[i]; } } 38 39 40 41 42 43 44 45 46 47 this.norm = new double(this.data.length]; for (int i = 0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

Why is effective goal-setting important to performance management?

Answered: 1 week ago