COMP204-07B Assignment 2

Your task is to implement a simple interactive application which fits a regression line to data-points that ones enters by just clicking inside a window. The regression line must be updated after every newly added point.

Due date: Friday, 3rd August

What to submit:

One java file containing all you code.
Document your code well.
Use the javadoc @author tag to give your name and ID!

Specification:



Your application should consist of one graphical pane, where the user can add points by just clicking.

A fitting line is easily computed by the following regression equation:

y = a + b*x

where a and b are computed as follows for n points:

a = s1 / d

b = s2 / d

s1 = (Sum y_i)*(Sum x_i*x_i) - (Sum x_i)*(Sum x_i*y_i)

s2 = n*(Sum x_i*y_i) - (Sum x_i)*(Sum y_i)

d = n*(Sum x_i*x_i) - (Sum x_i)*(Sum x_i)
You need at least TWO points to be able to fit a regression line. Therefore you should wait until you have two or more points before computing and drawing the regression line.

Assessment will be based on the following criteria:

  1. Functionality
  2. Reasonable clarity and style of programming.
  3. Reasonable documentation;

Here are a few hints:

  1. You can directly draw on a JFrame
  2. Class Graphics (the argument of your frame's paint method) has methods to draw lines and circles, and also to clear rectangular areas, among other methods.
  3. You will need some list to keep track of all the points.
  4. Class MouseEvent (the argument of your MouseListener's mousePressed method) has getX() and getY() methods
  5. The best way to draw your regression line is to simply draw from x:0/y:f(x) to x:width/y:f(width), where width is the width of your frame (see method getWidth).
  6. Frames can be formatted by method setPreferredSize(Dimension d), lookup class Dimension
  7. Closing your application window should quit your application (look at code examples)