Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java Language Please 11.6 Lab: Shapes In this lab you will create an interface, and then implement that interface in three derived classes. Your

In Java Language Please

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

11.6 Lab: Shapes In this lab you will create an interface, and then implement that interface in three derived classes. Your interface will be called Shape, and will define the following functions: Return Type Name Parameters Description double computes the area of the shape double perimeter none computes the perimeter of the shape Point20 center none computes the center of the shape area none You will then create 3 implementations of this interface: Rectangle, Circle, and Polygon. Each one will need to provide implementations of area, perimeter, and center. (I have provided an implementation of Square in case you need to reference it) Here are the signatures of the constructors for Rectangle, Circle, and Polygon // Create a rectangle with pl and p2 as opposite corners public Rectangle(Point2d p1, Point2d p2) // Create a circle with center 'c' and radius 'r' public Circle(Point2dc, double r) // Create a polygon with the given points. public Polygon (Point2d[1 pts) Your classes will need to delare and initialize the necessary instance data to store the necessary data for each class. Note that I have supplied a Point2d class for you. As you'll see below, this has some functions in it that you may find useful. I assume you know from HS geometry how to computer the area, perimeter, and center of a rectangle and a circle. It may help to know that the Java library defines pi as Math.pi. The Polygon class is a bit more complicated: . For the center of the polygon, you can simply average all the x coordinates, and all the y coordinates, then create a point with those averages as the x and y coordinates of the point. For the perimeter, just use the distance() function in Pointed to compute the length of each edge, and add them. . For the area, I've provided a function in the Point2d class that computes the area of a triangle, given the three points. You can use this N-1 A = area(Po, P-1, P.) function along with the formula to compute the area of the polygon. There is one more gotcha in the Polygon class. The Point2d class is immutable (that is, a Pointed object, once created, cannot be modified), but an array is not. One of the tests will fail if you do not take this into account. I would advise that you use incremental development here. I suggest doing things in this order: 1. Create the shape interface. You'll know the shape interface is correct when the first test passes. (None of the other test will even compile at this point.) 2. Create the Rectangle class. You'll know this is done correctly when the second test passes. (The other tests will still not even compile.) 3. Create the circle and Polygon classes, and have them just pass back dummy data. Once this is done correctly, all the tests should compile, though they will not pass. 4. Finish the circle class. 5. Finish the Polygon class. Downloadables Square.java Point2d.java Rectangle.java ShapeTest.java Download File is marked as read only Current file: Shape Test.java 1 import java.util.Scanner; 2 3 public class ShapeTest { 4 5 public static void main(String[args) { 6 7 Scanner scnr = new Scanner(System.in); 8 9 while (schr. hasNext() { Shape shape = null; 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 String shape Type = scnr.nextO; switch (shapeType) { case "Circle": { double x = scnr.nextDoubleO; double y = schr.nextDoubleO; double rad = scnr.nextDoubleO; shape = new Circle(new Point2d(x, y), rad); break; } 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 case "Polygon": { int nPoints - scnr.nextInto: Point2db pts = new Point2d[nPoints]; for (int i = 0; i

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

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago