Question:
A. Write an Octagon class that inherits from GeometricObject and implements the Comparable interface, comparing the area of the shape. You can assume that all the sides are equal in length.
B. Implement the Comparable interface in the Circle and Rectangle from Question2 comparing the area of each shape. (use the same classes in Question 2)
C. Also add the overloaded toString() method to each class that prints a summary of the object. Then, in another class, create an ArrayList of at least one of each Circle , Rectangle and Octagon .
D. Write 2 methods: i. A sumArea() method that takes an ArrayList of GeometricObjects and returns the sum for the entire list
ii. A printSorted() method that takes an ArrayList, sorts it by area (using the compareTo() method) and then prints the sorted list using the overloaded toString() method.
Thank you so much!
Java Code:
GeometricObject.java
package main;
//GeometricObject Class
public class GeometricObject implements
Comparable<GeometricObject> {
protected double length; //variables
protected double width;
protected double area;
@Override
public int compareTo(GeometricObject arg0) {//Compare
to method
return (int) (this.area-arg0.area)
; //comparing areas
}
}
Octagon.java
package main;
//Octagon Class
public class Octagon extends GeometricObject {
Octagon(double length){ //constructor
this.area =
(1+Math.sqrt(2))*2*length;
this.length = length;
}
@Override
public String toString() { //toString Method
return "Area of Octagon of side
length " + this.length + " is " + this.area;
}
}
Circle.java
package main;
//Circle Class
public class Circle extends GeometricObject {
Circle(double radius){ //constructor
this.area = Math.PI*2*radius;
this.length = radius;
}
@Override
public String toString() { //toString Method
return "Area of Circle of radius "
+ this.length + " is " + this.area;
}
}
Rectangle.java
package main;
//Rectangle Class
public class Rectangle extends GeometricObject {
Rectangle(double length, double width){
//constructor
this.area = length * width;
this.length = length;
this.width = width;
}
@Override
public String toString() { //toString Method
return "Area of Rectangle with
dimensions " + this.length+ " x " + this.width + " is " +
this.area;
}
}
Main.java
package main;
//imports
import java.util.*;
//Main Class
public class Main {
//main method
public static void main(String[] args) {
//creating objects of
GeometricObject
GeometricObject circle = new
Circle(4);
GeometricObject rectangle = new
Rectangle(4, 7);
GeometricObject octagon = new
Octagon(4);
//Creating array list to store
objects
List<GeometricObject> geoObj
= new ArrayList<GeometricObject>();
geoObj.add(circle);
geoObj.add(rectangle);
geoObj.add(octagon);
System.out.println("Sum of the area
is: " + sumArea(geoObj)); //printing sum
printSorted(geoObj); //printing
sorted array
}
static double sumArea(List<GeometricObject>
geoObj) {
double sum=0;
for(GeometricObject geo : geoObj)
{
sum+=geo.area;
//iterating and calculating sum
}
return sum;
}
static void printSorted(List<GeometricObject>
geoObj) {
System.out.println();
//printing list before
sorting
System.out.println("Before sorting
the List is:");
for(GeometricObject geo : geoObj)
{
System.out.println(geo.toString());
}
System.out.println();
Collections.sort(geoObj); //sorting
array list
System.out.println("Sorted List
is:"); //printing list after sorting
for(GeometricObject geo : geoObj)
{
System.out.println(geo.toString());
}
}
}
Sample Output:
Sum of the area is: 72.44644972770311
Before sorting the List is:
Area of Circle of radius 4.0 is 25.132741228718345
Area of Rectangle with dimensions 4.0 x 7.0 is 28.0
Area of Octagon of side length 4.0 is 19.31370849898476
Sorted List is:
Area of Octagon of side length 4.0 is 19.31370849898476
Area of Circle of radius 4.0 is 25.132741228718345
Area of Rectangle with dimensions 4.0 x 7.0 is 28.0
Question: A. Write an Octagon class that inherits from GeometricObject and implements the Comparable interface, comparing...
Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color;...
I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side; //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); } //setter and getter methods for Square public void setSide(double side) { this.side = side; } public double getSide() { return...
Create a data class named Automobile that implements the Comparable interface. Give the class data fields for make, model, year, and price. Then add a constructor, all getters, a toString method that shows all attribute values, and implement Comparable by using the year as the criterion for comparing instances. Write a program named TestAutos that creates an ArrayList of five or six Automobiles. Use a for loop to display the elements in the ArrayList. Sort the Arraylist of autos by...
Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...
Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...
Please write in Java Language Write an abstract class Shape with an attribute for the name of the shape (you'll use this later to identify a particular shape). Include a constructor to set the name of the shape and an abstract calculateArea method that has no parameters and returns the area of the shape. Optionally, include a constructor that returns the name of the shape and its area (by calling the calculateArea method). Write a Rectangle class that inherits from...
Design an interface named Colorable with a void method named howToColor(). Every class of a colorable object must implement the Colorable interface. Design a class named Square that extends GeometricObjectand implements Colorable. Design another class named Trianglethat extends GeometricObjectand implements Colorable. Implement howToColor inSquareto display the message Color all four sides. ImplementhowToColor inTriangleto display the message Color all three sides. Draw a UML diagram that involves Colorable, Square, Triangle, and GeometricObject. Write a test program that creates an array of...
Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...
this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...
q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have a public constructor that takes a JTextArea and a JLabel as parameters and stores these in instance variables. Override the mouseEntered method to . display the text from the JTextArea on the JLabel in all upper case letters. Then, override the mouseReleased method to display the text from the JTextArea on the JLabel in all lower case letters. The other three methods from the...