Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / One of the more general two-dimensional shapes is a polygon - any closed shape composed of straight, non-overlapping lines such as a triangle, rectangle, or hexagon

One of the more general two-dimensional shapes is a polygon - any closed shape composed of straight, non-overlapping lines such as a triangle, rectangle, or hexagon

Computer Science

One of the more general two-dimensional shapes is a polygon - any closed shape composed of straight, non-overlapping lines such as a triangle, rectangle, or hexagon. If all of the sides are the same length, we call it a regular polygon. The distance from the center of a regular polygon to the center of any of its sides is called the apothem (see diagram by Wrtlprnft - CC BY-SA 3.0 Unported). Doesn't a regular polygon look like an object around which we could orient our programming? 

 

  An apothem.

 

Let's write your first class and call it Polygon. The full credit, bonus, and extreme bonus version of Polygon (see below and in the PDF) differ in ways that will allow you to explore some aspects of writing a class, so don't spend much time worrying whether the requirements result in an optimum practical implementation. 

 

Add, commit, and push each of your solution(s) to your cse1325 GitHub repository in subdirectories of P02 for grading by the deadline. It's a GREAT idea to commit and push work in progress for safe-keeping - we'll only check your final commit, I promise! Nothing needs to be submitted to Canvas - we already know where your repository is from the first assignment. If you want to ensure your push arrived on GitHub, use the web interface.

 

  In P02/full_credit, write a Polygon class in a file named (as required by Java) Polygon.java. Keep track of the number of sides in a field called sides, and its perimeter in a field called perimeter (isn't object-oriented programming easy?). Use an addSide(length) method to add its parameter to the perimeter and increment sides. Provide getters named getSides (that returns sides) and getPerimeter (that return perimeter). Then write a static main method that creates a Polygon object, adds some sides interactively, and then reports the number of sides entered and the perimeter. Do NOT keep track of the number of sides or the perimeter in your main method - that's what your Polygon object is doing!

 

 In P02/bonus, copy your full credit solution and create an array named lengths to store the lengths of each side. Now your getPerimeter method will calculate the perimeter each time it is called by iterating over the lengths array. Also add a getArea(double apothem), returning the area of the polygon. (This calculation is only valid for a regular polygon, and you may but are NOT required to enforce that yet

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE