Assignment #2: Kaleidoscope
This time you learn how to write software cooperatively, the software development team is you and somebody
who has written another part of the software (it might have been me, but that's unimportant as long as you don't
want to sell the software). You have to create a class Triangle, which will be specified below.
The class is part of a program, that takes the triangle and draws something. If you follow the specification,
you will be rewarded by the output of this (or a similar) picture:
The big picture:
The software should create the image above, by producing different triangles, rotating and drawing them using
different angles and colors. The part of rotating and drawing is already done. Your task is, to create a class
that actually provides the triangle itself, which is not more than 3 vertices - well, it is a bit more:
The triangles need different sizes, and, since we are professional, the data structure containing the 3 points
must be hidden. The API will tell you exactly what to do.
API of class Triangle
Fields: something that contains the 3 vertices. Since the class 'Kaleidoscope' will only use the data through
the method 'getData', a further specification is not needed - it's up to you how to define the data.
Methods:
- Constructor: public Triangle(Kaleidoscope kal, int y2)
kal: the Kaleidoscope object is needed to 'register' the Triangle to the Kaleidoscope object, using the Kaleidoscope
method 'registerTriangle(Triangle t)'. As soon as a Triangle object is registered with the Kaleidoscope object kal,
it will be displayed.
y2Determines the height of the triangle. The constructor should always create a triangle with 3 vertices, the
coordinates being: (0,0), (20,0), (20,y2)
- public int[ ][ ] getData()
This method returns the 3 vertices of the triangle as a 3x2 array. a[0][0]=x0, a[0][1]=y0, a[1][0]=x1,...,a[2][2]=y2.
The Kaleidoscope object needs this method in the actual drawing routine.
The class 'Kaleidoscope'
You don't need any knowledge about 'Kaleidoscope', except that:
- It contains the 'main' method, i.e. it is the entrance point of the project
- It contains a method public void registerTriangle(Triangle), which must be called by your Triangle class
- It instantiates multiple objects of your class Triangle, using the constructor with different parameters y2
- It supposes, that a Triangle object registers itself, using 'registerTriangle'.
Remarks
This assignment is about information hiding and the parameter 'this'. Please have that in mind. The information
hiding here can be critical, e.g. if you define the vertices as a 2D array private int vertices[][] (of course private !),
but then simply return a reference to vertices in the method 'getData()'. We will talk about that mistake in class.
The whole software design of the project is totally screwed up. A class calling another that again registers with
the first one etc. - it's a mess for such a simple program. But it's a good example how to write one class just
knowing the interface definition, and it was screwed up on purpose such that you have to use the 'this' parameter !
The code:
Class 1: Kaleidoscope.java
Class 2: Triangle.java (template)