import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Lab21 extends Applet implements MouseListener, MouseMotionListener, ActionListener { private point data; private int spotget, closest, points, outlier; private boolean hover, changed, firsttime; private float factor; private int [] counter, bins; public Button remove, trim, reset; public TextField entry; public Image hist, box; public Font font2; public void init (){ addMouseListener(this); addMouseMotionListener(this); font2 = new Font("Ariel", Font.PLAIN, 12); Panel p = new Panel(new FlowLayout(2)); reset = new Button("Reset"); reset.setSize(40, 25); p.add(reset); reset.addActionListener(this); Label nameLabel = new Label("Trim by "); p.add(nameLabel); entry = new TextField("0"); p.add(entry); nameLabel = new Label("(0-9) points."); p.add(nameLabel); trim = new Button("Trim"); trim.setSize(40, 25); p.add(trim); trim.addActionListener(this); nameLabel = new Label(" "); p.add(nameLabel); remove = new Button("Remove Outliers"); remove.setSize(40, 25); p.add(remove); remove.addActionListener(this); this.setLayout(new BorderLayout(10, 10)); this.add(p, BorderLayout.NORTH); DirectionPanel directions = new DirectionPanel(500,130); directions.addDirection("$title$ Step 1 $/title$ Looking above, you will see a box-and-whiskers plot of the auto theft rate in the 50 states."); directions.addDirection("$title$ Step 2 $/title$ Following the instructions given in the introduction, try to move data points on the plot. Once familiar with this process, press Reset, and proceed with the lab."); directions.addDirection("$title$ Step 3 $/title$ Print display and note summary statistics. See steps 3a-3d"); directions.addDirection("$title$ Step 3a $/title$ The mean is denoted by a triangle along the bottom of the histogram."); directions.addDirection("$title$ Step 3b $/title$ The quartiles are located above their corresponding position on the box-and-whiskers plot. The IQR can be computed as their difference."); directions.addDirection("$title$ Step 3c $/title$ The standard deviation is located on the right side of the applet."); directions.addDirection("$title$ Step 3d $/title$ Maximum and minimum values are located above the greatest and least values of the box-and-whiskers plot."); directions.addDirection("$title$ Step 4 $/title$ Do a 3 times trim of the data. To do this, input 3 in the textfield labeled Trim by and then press the Trim button."); directions.addDirection("$title$ Step 5 $/title$ Print display and note the new summary statistics produced. "); directions.addDirection("$title$ Step 6 $/title$ If you want to trim the set by more points, repeat steps 4 and 5. "); directions.addDirection("$title$ Step 7 $/title$ When done trimming points, press the Reset button to set the points back to their original values."); directions.addDirection("$title$ Step 8 $/title$ Let's see the effect of moving an outlier. Move the data point for Massachusetts to 2140. "); directions.addDirection("$title$ Step 9 $/title$ Print display and note the new summary statistics produced."); directions.addDirection("$title$ Step 10 $/title$ Set the data back to the original values."); directions.addDirection("$title$ Step 11 $/title$ The final task of this lab is to remove the outliers. This is done by simply pressing the Remove Outliers button."); directions.addDirection("$title$ Step 12 $/title$ Print display and note the new summary statistics produced."); this.add(directions, BorderLayout.SOUTH); data = new point(); hover = false; changed = true; firsttime = true; spotget = -1; points = 50; factor = (float)2.76447; counter = new int [10]; bins = new int [11]; outlier = points - 1; } public void paint(Graphics g){ if(firsttime){ setBackground(Color.white); insert_sort(); hist = createImage(800, 200); Graphics ghist = hist.getGraphics(); ghist.drawRect(5,0,765,180); for(int c=0;c<11;c++){ ghist.drawLine(5 + c * 76, 180, 5 + c * 76, 185); ghist.drawString("" + (int)(c * 76 * factor + 100), 5 + c * 76, 198); } box = createImage(800, 100); Graphics gbox = box.getGraphics(); gbox.drawLine(5, 60, 765, 60); for(int c=0;c<11;c++){ gbox.drawLine(5 + c * 76, 55, 5 + c * 76, 65); gbox.drawString("" + (int)(c * 76 * factor + 100), 5 + c * 76, 86); } firsttime = false; } Color frontcolor = new Color(127, 144, 175);//RGB Color shadow = new Color(79, 96, 127); Color light = new Color(191, 207, 208); g.setColor(light); g.drawLine(0, 360, 800, 360); g.setColor(frontcolor); g.drawLine(0, 361, 800, 361); g.drawLine(0, 362, 800, 362); g.setColor(shadow); g.drawLine(0, 363, 800, 363); g.setColor(Color.black); g.drawLine(0, 364, 800, 364); g.setColor(light); g.drawLine(0, 33, 800, 33); g.setColor(frontcolor); g.drawLine(0, 34, 800, 34); g.drawLine(0, 35, 800, 35); g.setColor(shadow); g.drawLine(0, 36, 800, 36); g.setColor(Color.black); g.drawLine(0, 37, 800, 37); g.drawImage(box,5,50,this); g.drawImage(hist,5,150,this); g.setFont(font2); if(changed){ insert_sort(); float temp = 0; for(int c=0;c data.value[(int)(points/4)] - 1.7 * (data.value[(int)(3*points/4)] - data.value[(int)(points/4)])){ g.drawLine((int)(data.value[c] / factor - 100 / factor + 5), 70, (int)(data.value[(int)(points/4)] / factor - 100 / factor + 5), 70); break; } for(int c=points - 1;c>(int)(3*points/4);c--) if(data.value[c] < data.value[(int)(3*points/4)] + 1.7 * (data.value[3*(int)(points/4)] - data.value[(int)(points/4)])){ g.drawLine((int)(data.value[c] / factor - 100 / factor + 5), 70, (int)(data.value[(int)(3*points/4)] / factor - 100 / factor + 5), 70); outlier = c; break; } //draw outliers for(int c=points - 1;c>outlier;c--) g.fillOval((int)(data.value[c] / factor - 100 / factor + 5) - 2, 68, 4,4); } public void mousePressed(MouseEvent event) { closest = 0; int xvalue = (int)((event.getX() - 5) * factor + 100); if(event.getY() > 60 && event.getY() < 80 && spotget == -1){ for(int c=1;c (data.value[c] - xvalue) * (data.value[c] - xvalue)) closest = c; if((data.value[closest] - xvalue) * (data.value[closest] - xvalue) < 200){ hover = true; spotget = closest; } else hover = false; } else hover = false; repaint(); } public void mouseReleased(MouseEvent event) { if(spotget > -1){ data.value[spotget] = (int)((event.getX() - 5) * factor + 100); changed = true; } spotget = -1; repaint(); } public void mouseDragged(MouseEvent event) { if(spotget > -1){ data.value[spotget] = (int)((event.getX() - 5) * factor + 100); Graphics g = getGraphics(); g.setFont(font2); g.drawImage(box,5,50,this); g.drawImage(hist,5,150,this); if(spotget < points - 1) if(data.value[spotget] > data.value[spotget + 1]){ int temp = data.value[spotget]; String temp2 = data.state[spotget]; data.value[spotget] = data.value[spotget + 1]; data.value[spotget + 1] = temp; data.state[spotget] = data.state[spotget + 1]; data.state[spotget + 1] = temp2; spotget++; } if(data.value[spotget] < data.value[spotget - 1]){ int temp = data.value[spotget]; String temp2 = data.state[spotget]; data.value[spotget] = data.value[spotget - 1]; data.value[spotget - 1] = temp; data.state[spotget] = data.state[spotget - 1]; data.state[spotget - 1] = temp2; spotget--; } g.setColor(Color.white); g.fillRect(600,168,150,20); g.setColor(Color.black); g.drawString(data.value[spotget] + " " + data.state[spotget] , 600, 180); g.setColor(Color.white); g.fillRect(0,38,800,16); g.fillRect(0,346,800,16); g.setColor(Color.black); float temp = 0; for(int c=0;c data.value[(int)(points/4)] - 1.7 * (data.value[(int)(3*points/4)] - data.value[(int)(points/4)])){ g.drawLine((int)(data.value[c] / factor - 100 / factor + 5), 70, (int)(data.value[(int)(points/4)] / factor - 100 / factor + 5), 70); break; } for(int c=points - 1;c>(int)(3*points/4);c--) if(data.value[c] < data.value[(int)(3*points/4)] + 1.7 * (data.value[3*(int)(points/4)] - data.value[(int)(points/4)])){ g.drawLine((int)(data.value[c] / factor - 100 / factor + 5), 70, (int)(data.value[(int)(3*points/4)] / factor - 100 / factor + 5), 70); outlier = c; break; } //draw outliers for(int c=points - 1;c>outlier;c--) g.fillOval((int)(data.value[c] / factor - 100 / factor + 5) - 2, 68, 4,4); } } public void mouseEntered(MouseEvent event) {} public void mouseMoved(MouseEvent event) { closest = 0; int xvalue = (int)((event.getX() - 5) * factor + 100); if(event.getY() > 60 && event.getY() < 80 && spotget < 0){ for(int c=1;c (data.value[c] - xvalue) * (data.value[c] - xvalue)) closest = c; if((data.value[closest] - xvalue) * (data.value[closest] - xvalue) < 200){ Graphics g = getGraphics(); g.setColor(Color.white); g.fillRect(600,168,150,20); g.setColor(Color.black); g.drawString(data.value[closest] + " " + data.state[closest] , 600, 180); } else { Graphics g = getGraphics(); g.setColor(Color.white); g.fillRect(600,168,150,20); } } else { Graphics g = getGraphics(); g.setColor(Color.white); g.fillRect(600,168,150,20); } } public void mouseClicked(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void actionPerformed(ActionEvent event){ if (event.getSource() == remove){ for(int c=points - 1;c>(int)(3*points/4);c--) if(data.value[c] < data.value[(int)(3*points/4)] + 1.7 * (data.value[3*(int)(points/4)] - data.value[(int)(points/4)])){ points = c + 1; break; } changed = true; repaint(); } if (event.getSource() == trim){ Float temp; temp = Float.valueOf(entry.getText()); if((int)temp.floatValue() > 0 && (int)temp.floatValue() < 10){ for(int c=0; c<(int)temp.floatValue();c++) data.value[c] = data.value[points - c - 1 - (int)temp.floatValue()]; points = points - 2 * (int)temp.floatValue() + 1; changed = true; repaint(); } } if (event.getSource() == reset){ data = new point(); points = 50; repaint(); } } private void insert_sort() { int index, position, key; String temp; // outer loop checks each position for(index = 1; index < points; index++) { key = data.value[index]; temp = data.state[index]; position = index; while(position > 0 && data.value[position-1]>key) { data.value[position] = data.value[position-1]; data.state[position] = data.state[position-1]; position--; } data.value[position] = key; data.state[position] = temp; } } // method insert_sort } class point{ public String[] state; public int[] value; public point (){ value = new int [50]; value[0] = 144; value[1] = 220; value[2] = 489; value[3] = 293; value[4] = 343; value[5] = 219; value[6] = 163; value[7] = 265; value[8] = 249; value[9] = 245; value[10] = 333; value[11] = 593; value[12] = 334; value[13] = 377; value[14] = 147; value[15] = 309; value[16] = 226; value[17] = 246; value[18] = 237; value[19] = 282; value[20] = 244; value[21] = 400; value[22] = 511; value[23] = 144; value[24] = 467; value[25] = 791; value[26] = 183; value[27] = 314; value[28] = 326; value[29] = 397; value[30] = 528; value[31] = 360; value[32] = 1140; value[33] = 378; value[34] = 297; value[35] = 545; value[36] = 280; value[37] = 753; value[38] = 388; value[39] = 477; value[40] = 439; value[41] = 192; value[42] = 745; value[43] = 337; value[44] = 259; value[45] = 559; value[46] = 663; value[47] = 428; value[48] = 351; value[49] = 245; state = new String [50]; state[0] = "North Dakota"; state[1] = "Wisconsin"; state[2] = "Hawaii"; state[3] = "New Hampshire"; state[4] = "Minnesota"; state[5] = "Iowa"; state[6] = "West Virgina"; state[7] = "Vermont"; state[8] = "Nebraska"; state[9] = "Kentucky"; state[10] = "Pennsylvania"; state[11] = "Connecticut"; state[12] = "Utah"; state[13] = "Indiana"; state[14] = "South Dakota"; state[15] = "Montana"; state[16] = "Virginia"; state[17] = "Maine"; state[18] = "Idaho"; state[19] = "Wyoming"; state[20] = "Kansas"; state[21] = "Ohio"; state[22] = "New Jersey"; state[23] = "Mississippi"; state[24] = "Delaware"; state[25] = "Rhode Island"; state[26] = "Arkansas"; state[27] = "Tennessee"; state[28] = "Oklahoma"; state[29] = "Texas"; state[30] = "Illinois"; state[31] = "Washington"; state[32] = "Massachusetts"; state[33] = "Missouri"; state[34] = "Georgia"; state[35] = "Michigan"; state[36] = "Alabama"; state[37] = "Alaska"; state[38] = "Oregon"; state[39] = "Colorado"; state[40] = "Arizona"; state[41] = "North Carolina"; state[42] = "New York"; state[43] = "Lousiana"; state[44] = "New Mexico"; state[45] = "Nevada"; state[46] = "California"; state[47] = "Maryland"; state[48] = "Florida"; state[49] = "South Carolina"; } }