Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

accept that a polygon is fully described by: Its size: that is, its number of vertices; The 1D array of its vertices, ordered adjacently (contiguous

accept that a polygon is fully described by:

  • Its size: that is, its number of vertices;
  • The 1D array of its vertices, ordered adjacently (contiguous vertices in the array are adjacent vertices in the polygon); and
  • A name, which describes what type of polygon we are handling

So for instance, a square is a polygon with 4 vertices that is named a square, and whose list of vertices contains the 4 vertices that make up the square at hand.

You are given a file called Polygon.java that allows you to create and handle polygons. In particular, it comes with:

  • A method called perimeter, which computes the perimeter of the polygon;
  • A method called area, which aims to compute the surface defined by the polygon; and
  • A method toString, which aims to nicely print the vertices of the polygon in the order in which they appear in the array of vertices.

Please make yourselves familiar with it.

You will notice that this java file is missing its getters and setters. Your task, with this file, is to complete it with one getter and one setter per attribute of your type Polygon.

Now, each vertex is basically a point in space. Each point is fully described by:

  • Its x coordinate; and
  • Its y coordinate.

So for instance, point (3,2) has 3 as the value for x and 2 as the value for y. You are given a file called Point.java that allows you to create and handle points. In particular, it comes with:

  • A method distance to compute the distance between the current point and another point; and
  • A method toString to nicely print out the coordinates of the current point.

Please make yourself familiar with it.

You have nothing to add to this file J

There are many types of polygons: a square is a polygon, a triangle is a polygon, a hexagon is a polygon, etc. All of these is a relationships are a good indication of an inheritance relationship. If we were to define a type Rectangle, it should definitely inherit from the type Polygon (because a Rectangle is a Polygon).

We provide you with one more file: Rectangle.java. This new type inherits from Polygon. We say that Rectangle and Triangle are subclasses of Polygon, and reversely, Polygon is the superclass of Rectangle.

  • Attributes: A rectangle has no more attributes than a polygon. Therefore, you can see in the file Rectangle.java that no additional attribute is defined.
  • Methods:
    • The perimeter of a rectangle is calculated the same way a polygons is. Therefore there is no perimeter method within Rectangle.java.
    • However, the area of a rectangle is easily computed and a new version is available to you in Rectangle.java.

What are you asked to do?

You are asked to do the following:

  • Within java file that is given to you:
    • Write one setter and one getter per attribute of a polygon
    • Write a method distanceMin:
      • This method applies to a current polygon P1 and takes another one P2 as a parameter;
      • It identifies the pair of points (p1,p2) where p1 is a vertex of P1, and p2 is a vertex of P2, that are closest in distance (using the method distance from class Point), and returns their distance;
      • Hint: you will use the Point.java method distance to implement this method.
    • Answer the following question: How many steps, in terms of the respective sizes of polygons P1 and P2, does method distanceMin execute? Justify your answer?
  • Create a new class Triangle (a triangle is a polygon) that inherits from Polygon.
    • You have to create a new file java.
    • This class has no more attributes than a Polygon.
    • It should have its own version of the area method: you do not have to figure out the formula for a triangles area. Your method simply has to be different from that of its superclass and include this printout Area of a triangle."
  • Create a new class Square (a square is some sort of a rectangle) that inherits from Rectangle.
    • You have to create a new file java.
    • This class has no more attributes than a Rectangle, and for that matter, than a Polygon.
    • It should have its own version of the area method: the area method of the square class should use the area method of Rectangle (it should clearly call it) but also print out This is the area method of class Square."

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions