Exercise 4

In the exercise for week 4, we will work with the following tasks.

  1. Define a class Point, whose object represents points in the plane, i.e. their coordinates (x, y), both of which are of the type int. The only methods available are int getX() and int getY() to get the two coordinates. Choose your appropriate parameters to the constructor.
  2. The following class represents the beginning of a library for points. Define the three functions described:
    public class PointLib {
    
         public static double distance(Point p, Point q) {
            // Fill in here so that the method gives
            // the distance between p and q
         }
    
         public static Point mirrorX(Point p) {
            // The method should return a new point,
            // which is the mirror image of p in x-axis
         }
    
         public static Point mirrorY(Point p) {
            // returns the mirror image in the y-axis
         }
    }
    
    Note here that in the previous task we defined a class Point used for creating objects, representing points in the plane. Here we write a library of static methods for working with such points.   
  3. Define a class Path which represents a path through a number of points. See the figure below.   

         

    The class has the following instance variables:

        private Point[] pts;
        private int count;
    
    The pts stores the points that describe the path; the variable count specifies how many points the path contains.

    First, define a constructor

    public Path(int max)
    
    where the parameter max specifies the maximum number of points the new path can consist of. The newly created path should be empty, i.e. it should not contain any points.

    Then define a method:

    public void addPoint(Point p)
    
    which adds the point p at the end of the path.

    So far, we can create a path and we can add points. It will be useful if our class also had methods that provide information. We therefore add two methods:

    public int getCount()
    public Point getPoint(int i)
    
    The first indicates how many points the path contains and the other gives the ith point (where i must be between 0 and getCount()-1).
  4. Extend the class PointLib with a method:
    public static double pathLength(Path path)
    
    which calculates the length of the path; length is the sum of the lengths of the line pieces that the path consists of.
  5. We consider a program that can plot paths:

    When the program starts, the road is empty. Every time the user clicks with the mouse in the window this adds a new point to the path.

    In addition to the classes that you have already defined, the program consists of two more classes. The first is the PathPanel which takes care of the actual drawing of the response to the mouse events:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class PathPanel extends JPanel {
    
        private Path path;
    
        public PathPanel(Path path) {
            this.path = path;
            setPreferredSize(new Dimension(400,400));
            setBackground(Color.WHITE);
            setOpaque(true);
            addMouseListener(new MyListener());
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            for (int i=0; i<path.getCount()-1; i++) {
                Point p = path.getPoint(i);
                Point q = path.getPoint(i+1);
                g.drawLine(p.getX(),p.getY(),
                           q.getX(),q.getY());
            }
        }
    
        private class MyListener extends MouseAdapter {
            public void mouseClicked(MouseEvent e) {
                path.addPoint(new Point(e.getX(),e.getY()));
                repaint();
            }
        }
    }
    
    The second contains the main method:
    import javax.swing.*;
    
    public class Main {
    
       public static void main(String[] args) {
          JFrame f = new JFrame("Path");
          f.add(new PathPanel(new Path(100)));
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.pack();
          f.setVisible(true);
       }
    }
    
    Cut and paste these two classes to files PathPanel.java and Main.java. Compile and test the program.

    We will now expand the PathPanel so that it looks like this:   

         

    We will mark each joint point with a small black circle (six pixels in diameter). At the bottom of the panel we will display the length of the path with red color.

    Extend the method paintComponent in PathPanel.java so that you get this new look. You will need two methods from the class Graphics (see crib):

    public void fillOval(int x, int y,
                         int width, int height)
    public void drawString(String str, int x, int y)
    
    Remember that in the window the coordinate y = 0 is at the top!
  6. Add a static method:
    public static boolean isSymmetric(int[][] a)
    
    that determines whether the parameter a is a symmetric matrix. (A symmetric matrix, as defined in linear algebra, is a square matrix A such that Aij = Aji for all i and j.)