Lab 3: Java Interfaces and Shapes

Due Date and Submission Requirements


The goal of this lab is:


Directions

Create an interface called RegularPolygon with method signatures for area() (returns a double) and perimeter() (also returns a double). Create the following classes. These four classes MUST implement the RegularPolygon interface: All of those classes will have different ways to calculate the area and perimeter given the length on one side. Google “area of an equilateral triangle” to see a nice formula – all of the other polygons have similar Google entries. Note: In Java, you can use Math.sqrt(3) to compute the square root of 3, for example.

Each class should have a side_length instance field. You will also need to define the constructor for each of those 4 classes. Each constructor will just accept one double as an argument (the side length). Use Lab3Demo.Java to test your code. The user inputs a side length (ie 5) when the program starts, and then the Lab3Demo class uses the classes that you define to compute the perimeter and area of a Triangle, Square, pentagon, and hexagon given the side length (remember that these are all regular polygons). The RegularPolygon interface gives us a consistent way to treat all the different types of regular polygons there could be, even though they’re all a bit different under the hood. A RegularPolygon can take many forms; that’s polymorphism!

Rules

You are NOT allowed to modify Lab3Demo..java. If you modify Lab3Demo.Java in any way

Starting Code

Required Output

When your program is run, your output should look exactlythe same as seen in this screenshot .

Grading (10 points)



Deductions
  • -5 points if interface is not used



  • bonus video if you need to review your shapes and polygons (nightmare fuel warning)