Question
Modify GeometricShape.java to have two more instance data representing the (x,y) location the shape is drawn at. Create a constructor that sets all four properties
Modify GeometricShape.java to have two more instance data representing the (x,y) location the shape is drawn at. Create a constructor that sets all four properties of the GeometricShape. Create getters and setters for the new values. Modify the type of the color instance data to be Color from the java.awt package.
Modify the constructors of Circle and Octagon to accept values for X and Y, and change the type of the color parameter to be Color.
Create the method public void draw(Graphic2D g2) in circle and octagon. This method should draw the shape at the location given by the instance data x and y. The shape should be drawn in the color specified by the color instance data, and if the shape should be filled with the color, the draw method should fill the shape. ***Hint: We know one point on our Octagon, the x and y that are passed as parameters, as well as the side length d. We can use Pythagoras to find out the length a, which will allow us to figure out all the points on our octagon using addition and subtraction. Given a right triangle with two equal sides, we know that a*2 + a*2 = d*2 then 2 * a*2= d*2 So we must solve for a.
Create a class called ShowShapes with a main method. In the main method create a Frame that is 500 by 500. Set the usual properties of the frame.
Create a ShapeComponent class that extends JComponent. Create the paintComponent method and make it draw a row of Octagons that alternate between being filled and being empty. Then draw a row of colored circles that alternate between being filled and being empty. Add a shapeComponent to the frame created in the previous exercise.
Exercise 2 Create a new subclass of GeometricShape called Star. Implement the usual members for the class. Create a draw method that draws a 5 sided star at the location given by the x and y instance data. The star should be drawn in the color specified by the color instance data, and if the star should be filled with the color, the draw method should fill the star. Then add a row of stars to the ShapeComponent. The stars should be randomly colored and alternate between being filled and unfilled.
abstract class GeometricShape implements Comparable
private String color; private boolean isFilled;
public GeometricShape() { this.color = "white"; this.isFilled = true; }
/** * This method is used to accepts two GeometricShapes as parameters and returns * the larger of the two objects. * */ public static GeometricShape max(GeometricShape s1, GeometricShape s2) {
if (s1.getArea() == s2.getArea()) return s1;
else if (s1.getArea() > s2.getArea()) return s1;
else return s2; }
/** * This method is used to find the total sum of the areas of an array of * GeometricShapes. * */ public static double sumArea(GeometricShape[] shapes) {
double sum = 0;
for (int i = 0; i < shapes.length; i++) sum += shapes[i].getArea();
return sum; }
/** * This method is used to compare two different shapes by using area. * */ public int compareTo(GeometricShape a) {
if (this.getArea() <= a.getArea()) return -1;
return 1; }
/** * Color of the GeometricShape. * * @return the current value of this color. */ public String getColor() { return color; }
/** * Color of the GeometricShape. * * @param give New value for this GeometricShape's color. */ public void setColor(String color) { this.color = color; }
/** * * @return isFilled if the shape is filled. */ public boolean isFilled() { return isFilled; }
/** * This method is used to returns a string summary of the GeometricShape object. * */ @Override public String toString() { return ("color= " + color + ", isFilled=" + isFilled + ", Area: " + getArea() + "Perimeter: " + getPerimeter() + " "); }
public abstract double getArea();
public abstract double getPerimeter();
}
-------------------------------------------------------------------------------------------------
public abstract class Circle extends GeometricShape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
/** * Radius of the Circle. * * @return the current value of this radius. */ public double getRadius() {
return radius;
}
/** * Radius of the Circle. * * @param give New value for this shape's radius. */ public void setRadius(double radius) {
this.radius = radius;
}
/** * This method is used to returns a double of the Circle's area. * */ @Override public double getArea() {
return Math.PI * radius * radius;
}
/** * This method is used to returns a double of the Circle's perimeter. * */ @Override public double getPerimeter() {
return 2 * Math.PI * radius;
}
public String toString() {
return "Shape: Circle" + "Radius: " + radius + "Area: " + getArea() + "Perimeter: " + getPerimeter() + " ";
} }
------------------------------------------------------------------------------------------
public abstract class Octagon extends GeometricShape {
private double side;
public Octagon(double side) {
this.side = side;
}
/** * Side of the Octagon. * * @return the current value of this side. */ public double getSide() {
return side;
}
/** * Side of the Octagon. * * @param give New value for this Octagon's side. */ public void setSide(double side) {
this.side = side;
}
/** * This method is used to returns a double of the Octagon's area. * */ @Override public double getArea() {
return 2 * (1 + Math.sqrt(2)) * side * side;
}
/** * This method is used to returns a double of the Octagon's perimeter. * */ @Override public double getPerimeter() {
return 8 * side;
}
public String toString() {
return "Shape: Octagon" + "Side: " + side + "Area: " + getArea() + "Perimeter: " + getPerimeter() + " ";
}
------------------------------------------------------------------------------------------------------------------
import java.util.Arrays; import java.util.Comparator;
public abstract class FunWithShapes {
public static void main(String[] args) {
Circle first = new Circle(200);
Circle second = new Circle(100);
Circle third = new Circle(50);
Octagon fourth = new Octagon(5);
Octagon fifth = new Octagon(15);
Octagon sixth = new Octagon(20);
GeometricShape shapes[] = new GeometricShape[6];
shapes[0] = first; shapes[1] = second; shapes[2] = third; shapes[3] = fourth; shapes[4] = fifth; shapes[5] = sixth;
Arrays.sort(shapes);
System.out.println("Shapes in area(with order): ");
for (int i = 0; i < shapes.length; i++) { System.out.println(shapes[i]); }
System.out.println("Index 1 and 5 of max the shapes: " + GeometricShape.max(shapes[5], shapes[5])); System.out.println("Sum of area: " + GeometricShape.sumArea(shapes)); System.out.println("Compare To index 2 and 4: " + shapes[2].compareTo(shapes[4]));
Comparator
public int compare(GeometricShape s1, GeometricShape s2) { if (s1.getPerimeter() >= s2.getPerimeter()) return 1;
else return 2; } };
Arrays.sort(shapes, compareAll);
System.out.println("Sorted by perimeter: ");
for (int i = 0; i < shapes.length; i++)
System.out.println(shapes[i]); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started