Exercise A printout shouwing the problem, solution method, codes developed, and
ID: 3908856 • Letter: E
Question
Exercise A printout shouwing the problem, solution method, codes developed, and outputs for the tests indicated is due during and before the end of the class on The deadline is strictly observed. Create a hierarchy of Java classes as follows: Polygon isa Shape Circle is a Shape. Class Shape Class Shape is the hierarchy's superclass and inherits Java class Object An implementation of the class defines a point (x, y) and the color of the shape. The class includes appropriate class con structors and methods that perform the following a. b. c. d. getX, gety, getColor- retums the point (x, y) and color for the Shape object; setX, setY, setColor-sets the point (x, y) and color for the Shape object; shiftXY-moves point (x.?) by (Ax, AI): toString -returns the object's description as a String. This method must overridden in each subclass in the hierarchy draw-This method must be overridden in cch subclass in the hierarchy. For the Shape object, it paints the drawing canvas in coor be e. Class Polygon Class Polygon inherits class Shape. The Polygon object is a regular polygon defined by the integer interior appropriate class constructors and methods that perform the following operations: N- the number of the polygon's equal side lengths and equal The Polygon object may be filled with a color. The class includes a. getArea-retums the area of the Polygon objeet; b. getPerimeter- retums the perimeter of the Polygon object; c. getAngle-retum the interior angle (in degrees) of the Polygon object; d. getSide -returns the side length of the Polygon object; e. toString retums a string representation of the Polygon object: side length, interior angle, perimeter, and area; dra draws a Polygon object whose center point (x, y) is defined in class Shape and inscribed in a circle of radius radius. lass Circle Class Cirele inherits class Shape. The Circle object is defined by its radius, radius, and center (x, y), and may be filled with a color. The class includes appropriate class constructors and methods that perform the following operations: a. getArea retums the area of the Circle object; b. getPerimeter-retums the c. getRadius returns the radius of the Circle object d. toSting retums a string representation of the Circle object: radius, perimeter, rof the Circle object; and area: is defined in class Shape. of alternating concentric pentagons and circles as illustrated below, subject to the e. draw-draws a Circle object of radius radius. The center point (x, )of the cicle Use the class hierarchy to draw a geometric configuration comprised of a sequence following additional requirements: a. b. The code is applicable to canvases of variable height and width; The dimensions of the shapes are proportional to the smallest dimension of the canvas; c. The pentagons and circles are filled with different colors of your choiceExplanation / Answer
The Below code is the needed Java code. Since it is an assignment not everything has been done.
Three classes Shape, Polygon and Circle have been created as instructed using the appropriate constructors
Setters and getters have been included.
Calculation of Area and Perimeter has been done.
Draw method has been given for circle as an example. Similarly it has to be written for polygon (To be Written to learn)
All the integration has been done. The Class at the last named consumer has the main method to work with the other classes.
import java.lang.Math;
import javax.swing.JPanel;
import java.awt.Graphics;
class Shape{
private double x;
private double y;
private String color;
x=0;
y=0;
// setting the coordinates to zero by default to be easy for draw method
//getters
public double getX() { return x; }
public double getY() { return y; }
public String getColor() { return color; }
//setters
public void setX(double v) { x = v; }
public void setY(double v) { y = v; }
public void setColor(String v) { color = v; }
public void shiftXY(double u, double v)
{
x=x+u;
y=y+v;
}
}
class Circle extends Shape{
double r;
double area;
double perimeter;
Circle(double r){
this.r = r;
}
Circle(double r, double x, double y)
{
this.r = r;
setX(x);
setY(y);
}
Circle(double r, double x, double y, String color)
{
this.r = r;
setX(x);
setY(y);
setColor(color);
}
public double getRadius(){
return this.r;
}
public double getArea(){
area= 3.14*r*r; // Area is pi r Square
return area;
}
public double getPerimeter(){
perimeter= 2*3.14*r; //Permimeter is 2 pi r
return perimeter;
}
public void draw()
{
JPanel pnlCircle = new JPanel() {
public void paintComponent(Graphics g) {
g.drawOval(x, y, r, r);
g.setColor(Color.BLUE); // could be changed based on the input
}
};
//you can change X,Y coordinates and radius what you want.
}
public String toString(){
return "Radius :" + getRadius() + " Area :" + getArea() + " Perimeter :" +getPerimeter();
}
}
class Polygon extends Shape{
int n;
double length;
double area;
double perimeter;
int angle;
Polygon(int n, double length){
this.n = n;
this.length = length;
}
Polygon(int n, double length, double x, double y)
{
this.n = n;
this.length = length;
setX(x);
setY(y);
}
Polygon(int n, double length, double x, double y, String color)
{
this.n = n;
this.length = length;
setX(x);
setY(y);
setColor(color);
}
public int getSide(){
return this.n;
}
public double getArea(){
area= (n*length*length)/(4*Math.tan(180/n)); // change zero to the formuala of regular polygon
return area;
}
public double getPerimeter(){
perimeter= length*n; // No.of. Sides * Length of each side
return perimeter;
}
public int getAngle(){
angle=(n-2)*180;
return angle;
}
public String toString(){
return "No Of Sides :" + getSide() + " Area :" + getArea() + " Perimeter :" +getPerimeter() +" Interior Angle :" +getAngle();
}
}
// This class is used to test the above written classes
public class Consumer{
public static void main(String args[])
{
double r=10;
Circle c= new Circle(r);
System.out.println(c); // Picks the toString method of Circle Object
Polygon p=new Polygon(5,r);
System.out.println(p);
p.draw();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.