tags:

views:

84

answers:

2

Hi! There is task to implement some triangle class with interface like this

public interface Triangle  {
    void moveApex(Point from, Point to);
    List<Point> getApexes();
    void rotate(double angle);
    void setLocation(Point p);
    Point getLocation();
    void setSize(Dimension d);
    Dimension getSize();
}

Where Point and Dimension are some pairs of integers. In what way would you implement it? Would you creates some fields with Apexes or you will operate with 2 angles and one base line or smt else? Thanks.

+2  A: 

Points can - as you mentioned - be represented as a pair of points. Let's call such a pair a 2-dimensional vector. A vector can be interpreted as an arrow from the origin (of your coordinate system) to a point or just as an arrow starting anywhere indication a direction or movement. I.e. a vector is a representation for both a single point and a movement.

Thus, you can define a Triangle using vectors. Vectors can be transformed via 2x2-dimensioal arrays of numbers called matrices. With the help of these matrices, arbitrary transformations can be expressed nicely.

Here are some links that could help you and give more detailed information.

Vectors: http://en.wikipedia.org/wiki/Euclidean_vector

Triangle overview: http://en.wikipedia.org/wiki/Triangle

Matrix overview: http://en.wikipedia.org/wiki/Matrix_multiplication

Rotation via matrices: http://en.wikipedia.org/wiki/Rotation_matrix

  • setLocation requires vector movement (i.e. vector addition)
  • setSize might require vector multiplication
  • rotate Implement matrix rotation

So please read the above links, and just convert the maths to Java. The maths itself isn't that complicated so it should be possible to do so without much difficulty.

Of course there are supposedly plenty of implementations around.

phimuemue
+2  A: 

Your interface is not valid. Indeed, you have two methods called getLocation() that return different objects. Maybe you will have to choose different names, such as:

Point getLocationAsPoint();
Dimension getLocationAsDimension();

Now, if you use Eclipse, you can simply create a new class, indicate that this class implements the Triangle interface, and it will create the following skeleton:

public class MyTriangle implements Triangle {

    @Override
    public List<Point> getApexes() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void moveApex(Point from, Point to) {
        // TODO Auto-generated method stub
    }

    @Override
    public void rotate(double angle) {
        // TODO Auto-generated method stub
    }

    @Override
    public void setLocation(Point p) {
        // TODO Auto-generated method stub
    }

    @Override
    public void setSize(Dimension d) {
        // TODO Auto-generated method stub
    }

    @Override
    public Dimension getLocationAsDimension() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Point getLocationAsPoint() {
        // TODO Auto-generated method stub
        return null;
    }

}

Then, simply write the code for each method. I strongly recommend you to go for TDD in order to complete your task correctly.

Eclipse can create a new JUnit test class that will be your starting point: Create a new JUnit Test Case, and select MyTriangle in the Class under test option in the class creation wizard.

romaintaz
about getLocation() it was mistake. Thanks. I've edit it.
Stas