In: Computer Science
Given the following UML class diagram, implement the class as
described by the model. Use your best judgment when implementing
the code inside of each method.
+---------------------------------------------------+
| Polygon |
+---------------------------------------------------+
| - side_count : int = 3 |
| - side_length : double = 1.0 |
+---------------------------------------------------+
| + Polygon() |
| + Polygon(side_count : int) |
| + Polygon(side_count : int, side_length : double) |
| + getSides() : int |
| + setSides(side_count : int) |
| + getSideLength() : double |
| + setSideLength(side_length : double) |
| + perimeter() : double |
+---------------------------------------------------+
The following information might also be useful:
- The perimeter of a polygon is the sum of it's side lengths.
- Side counts of less than 3 are meaningless for a polygon, and
should not be accepted.
- Side lengths of less than 0 are meaningless for a polygon, and
should not be accepted.
class Polygon {
private int side_count;
private double side_length;
public Polygon() {
side_count = 3;
side_length = 1;
}
public Polygon(int s) {
//checking if values are
valid
if (s >= 3)
side_count =
s;
side_length = 1;
}
public Polygon(int s, double l) {
//checking if values are
valid
if (s >= 3 && l >= 1)
{
side_count =
s;
side_length =
l;
}
}
public int getSides() {
return side_count;
}
public void setSides(int s) {
//checking if values are
valid
if (s >= 3)
side_count =
s;
}
public double getSideLength() {
return side_length;
}
public void setSideLength(double l) {
//checking if values are
valid
if (l >= 1)
side_length =
l;
}
//sides * length
public double perimeter() {
return side_count *
side_length;
}
}
public class TestPolygon {
public static void main(String[] args) {
Polygon p= new Polygon(5,
15);
System.out.println("Perimeter :
"+p.perimeter());
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me