Free University of Bozen-Bolzano
Bachelor in Production Engineering
Introduction to Programming - A.A. 2005/2006

Exam exercise (adapted from Exam of 20/9/2005)
Class Polygon

We want to design a Java class for representing polygons on the plane, where each polygon is constituted by an ordered sequence of colored vertices.

Part 1. (4 points) For each Vertex object, the information of interest is:

Realize a Java class Vertex, to represent Vertex objects, that exports the following functionalities:

Solution: class Vertex

Part 2. (16 points) Realize a Java class Polygon, to represent Polygon objects. Each polygon is constituted by an ordered sequence of colored vertices, where each vertex is an instance of the class Vertex and has a position in the sequence. For a polygon with n vertices, the positions go from 0 to n-1. A polygon has also a size, which determines the maximum number of vertices it can have. The class Polygon should export the following functionalities:

Solution: representation of the objects, skeleton of the class, class Polygon

Part 3. (6 points) Realize a static method coordinatesOfColor(), client of the class Polygon (i.e., external to such a class) that, given a Polygon object pol, and a string c representing a color, writes to standard output the x and y coordinates of all those vertices of pol that have color c, one pair of coordinates per line.

Solution

Additional files to test the solution: class Main, using input file vertices.txt

Part 4. (4 points) Discuss the different types of loop constructs that are present in Java, their similarities and differences, and the relationship between each other. Discuss also in which situation which one should be preferred over the others.