Design a general class GeometricObject can be used to model all geometric object
ID: 2247182 • Letter: D
Question
Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and plied 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 side1, side2, and side3 with default values 2.0 to denote three sides of the triangle. Also contains: A no-arg constructor that creates a default triangle. A constructor that creates a triangle with the specified side1, side2, and side3. The accessor methods for all three data fields. A method named getArea() that returns the area of this triangle. A method named getPerimeter() that returns the perimeter of this triangle. A method named toString() that returns a string description for the triangle implemented as follows: return 'Triangle: side1 =" + side1 + 'side2 ="side2 + "side3 = " + side3: The Circle class inherits all accessible data fields and methods from the GeometricObject class. In addition, it has a new data field, radius, and its associated get and set methods. The Circle class also contains the getArea(), getPerimeter(), and getDiameter() methods for returning the area, perimeter, and diameter of the circle. The Rectangle class inherits all accessible data fields and methods from the GeometricObject class. In addition, it has the data fields width and height and their associated get and set methods. It also contains the getArea() and getPerimeter() methods for returning the area and perimeter of the rectangle. Design a Test class which creates objects of Triangle, Circle and Rectangle and invokes the methods on these objects. The toString() method is inherited from the GeometricObject class and is invoked from a Triangle object, Circle object and a Rectangle objectExplanation / Answer
**GeometricObject.java
public class GeometricObject {
public String color, filled;
public GeometricObject(){
}
public GeometricObject(double side1, double side2, double side3){
}
public GeometricObject(String color, String filled){
this.color = color;
this.filled = filled;
}
public String getColor(){
return this.color;
}
public String getFilled(){
return this.filled;
}
public String toString(){
//overriding the toString() method
return "The geometric object has the following features " + this.color + " and " + this.filled;
}
}
**Triangle.java
public class Triangle extends GeometricObject {
public double side1, side2, side3;
public Triangle(){
this.side1 = 2.0;
this.side2 = 2.0;
this.side3 = 2.0;
}
public Triangle(double side1, double side2, double side3){
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public Triangle(String color, String filled) {
super(color, filled);
// TODO Auto-generated constructor stub
}
public double getSide1(){
return this.side1;
}
public double getSide2(){
return this.side2;
}
public double getSide3(){
return this.side3;
}
public double getArea(){
double s = (this.side1 + this.side2 + this.side3)/2;
double area = Math.sqrt(s * (s - this.side1) * (s - this.side2) * (s - this.side3));
return area;
}
public double getPerimeter(){
return this.side1 + this.side2 + this.side3;
}
public String toString(){
//overriding the toString() method
return "Triangle:side1 ="+ this.side1 + " side2=" + this.side2 + " side3 =" + this.side3;
}
}
**Circle.java
public class Circle extends GeometricObject{
public double radius;
public void setRadius(double radius){
this.radius = radius;
}
public double getRadius(){
return this.radius;
}
public double getArea(){
double area = Math.PI * this.radius * this.radius;
return area;
}
public double getPerimeter(){
double perimeter = 2 * Math.PI * this.radius;
return perimeter;
}
public double getDiameter(){
double diameter = 2 * this.radius;
return diameter;
}
public String toString(){
//overriding the toString() method
return "Circle is of radius "+ this.radius + " and diameter = " + 2 * this.radius;
}
}
**Rectangle.java
public class Rectangle extends GeometricObject{
double width, height;
public void setWidth(double width){
this.width = width;
}
public void setHeight(double height){
this.height = height;
}
public double getWidth(){
return this.width;
}
public double getHeight(){
return this.height;
}
public double getArea(){
double area = this.width * this.height;
return area;
}
public double getPerimeter(){
double perimeter = 2 * (this.width + this.height);
return perimeter;
}
public String toString(){
//overriding the toString() method
return "Rectangle is of width "+ this.width + " and height = " + this.height;
}
}
**Driver.java
import java.io.*;
// Java program to print largest contiguous array sum
import java.util.*;
class Driver
{
public static void main (String[] args)
{
//Triangle object with default sides of length 2.0
Triangle triangle = new Triangle();
System.out.println("Area of the triangle is " + triangle.getArea());
System.out.println("Perimeter of the triangle is " + triangle.getPerimeter());
System.out.println(triangle);
System.out.println();
//Triangle object without default sides of length 2.0
Triangle triangle2 = new Triangle(4, 5, 6);
System.out.println("Area of the triangle is " + triangle2.getArea());
System.out.println("Perimeter of the triangle is " + triangle2.getPerimeter());
System.out.println(triangle2);
System.out.println();
//Circle with radius 5
Circle circle = new Circle();
circle.setRadius(5);
System.out.println("Radius of the circle is " + circle.getRadius());
System.out.println("Area of the circle is " + circle.getArea());
System.out.println("Perimeter of the circle is " + circle.getPerimeter());
System.out.println("Diameter of the circle is " + circle.getDiameter());
System.out.println(circle);
System.out.println();
//Rectangle of width 5 and height 10
Rectangle rect = new Rectangle();
rect.setHeight(10);
rect.setWidth(5);
System.out.println("Rectangle has width = " + rect.getWidth() + " and height = " + rect.getHeight());
System.out.println("Area of the Rectangle is " + rect.getArea());
System.out.println("Perimeter of the Rectangle is " + rect.getPerimeter());
System.out.println(rect);
System.out.println();
}
}
**Output:
Area of the triangle is 1.7320508075688772
Perimeter of the triangle is 6.0
Triangle:side1 =2.0 side2=2.0 side3 =2.0
Area of the triangle is 9.921567416492215
Perimeter of the triangle is 15.0
Triangle:side1 =4.0 side2=5.0 side3 =6.0
Radius of the circle is 5.0
Area of the circle is 78.53981633974483
Perimeter of the circle is 31.41592653589793
Diameter of the circle is 10.0
Circle is of radius 5.0 and diameter = 10.0
Rectangle has width = 5.0 and height = 10.0
Area of the Rectangle is 50.0
Perimeter of the Rectangle is 30.0
Rectangle is of width 5.0 and height = 10.
**Comment below for any queries
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.