Lab 3: Java Interfaces and Shapes
Due Date and Submission Requirements
- Due Date: Thursday, September 14th at 11:59 p.m.
- Partner Information: This is an individual assignment. You are allowed to collaborate with other students, but each student must submit their individual, independent solution.
- Submission Instructions: Upload your solutions (.java file), entitled RegularPolygon.java, Square.java, EquilateralTriangle.java,RegularPentagon.java, and RegularHexagon.java to the BrightSpace(D2L) Lab 1 Dropbox.
The goal of this lab is:
- Gain experience using Java interfaces
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:
- EquilateralTriangle
- Square
- RegularPentagon
- RegularHexagon
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)
- 8 points - EquilateralTriangle.java, Square.java, RegularPentagon.java, and RegularHexagon.java are correctly defined
- 1 points - RegularPolygon interface is correctly defined
- 1 point- Your program matches the output exactly
Deductions
-5 points if interface is not used
bonus video if you need to review your shapes and polygons (nightmare fuel warning)