Class name: ellipse.java Description: Implement the class ellipse.java, which in
ID: 3731563 • Letter: C
Question
Class name: ellipse.java
Description:
Implement the class ellipse.java, which inherits from GeometricObject given in class. The ellipse class should have:
double a, b properties
Constructor receiving a and b values as parameters and setting them.
Implementation of methods:
getters and setters for a and b
getArea, getPerimeter and toString
Your submission should include ALL classes, not just the ellipse class. Those classes are:
GeometricObject, Circle, Rectangle, Ellipse, GeometricObject tester.
InstructionsClass name: ellipse.java
Description:
Implement the class ellipse.java, which inherits from GeometricObject given in class. The ellipse class should have:
double a, b properties
Constructor receiving a and b values as parameters and setting them.
Implementation of methods:
getters and setters for a and b
getArea, getPerimeter and toString
Your submission should include ALL classes, not just the ellipse class. Those classes are:
GeometricObject, Circle, Rectangle, Ellipse, GeometricObject tester.
A sample run is shown. Please make sure your program prints similar results.
> run TestGeometricObject The circle was created on Thu Mar 02 17:59:16 EST 2017 The color is Blue Is filled? true The area is 78.540 The perimeter 31.416 The radius is 5.0 The diameter is 10.0 The rectangle was created on Thu Mar 02 17:59: 16 EST 2017 The color is Lime Is filled? true The area is 35.000 The perimeter 24.000 The ellipse was created on Thu Mar 02 17: 59 16 EST 2017 The color is Golcd Is filled? false The area is 37. 699 The perimeter 22.103 a is 3.0 b is 4.0 Equal area for circle and rectangle? false Equal area for circle and ellipse? false Equal area for rectangle and ellipse? falseExplanation / Answer
import java.util.*;
import java.lang.*;
class GeometricObject{
private String color = "white";
private boolean filled;
protected java.util.Date dateCreated;
/** Construct a default geometric object */
public GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with the specified color
* and filled value */
public GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
its getter method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
public double getArea(){
return 0;
}
public double getPerimeter() {
return 0;
}
/** Return a string representation of this object */
public String toString() {
return " color: " + color + " and filled: " + filled;
}
}
class Circle extends GeometricObject
{
private double radius;
public Circle()
{
radius=0.0;
}
public Circle(double radius,String color,boolean filled)
{
super(color,filled);
this.radius = radius;
}
//The accessor(getters and setters) methods for radius
public void setRadius(double radius)
{
this.radius = radius;
}
public double getRadius()
{
return radius;
}
//A method named getArea() that returns the area of this triangle.
public double getArea()
{
return 3.1412*radius*radius;
}
//A method named getPerimeter() that returns the perimeter of this triangle.
public double getPerimeter()
{
return 2*3.1412*radius;
}
//A method named toString() that returns a string description for the triangle.
public String toString()
{
return "The circle was created on "+dateCreated
+" The color is "+getColor()
+" Is Filled ?"+isFilled()
+" Area of Circle : "+getArea()
+" Perimeter of Circle : "+getPerimeter()
+" Radius : "+getRadius()
+" Diameter : "+ (2*getRadius()) ;
}
}
class Rectangle extends GeometricObject
{
private double length,width;
public Rectangle()
{
length=0.0;
width = 0.0;
}
public Rectangle(double length,double width,String color,boolean filled)
{
super(color,filled);
this.length = length;
this.width = width;
}
//The accessor(getters and setters) methods for radius
public void setLength(double length)
{
this.length = length;
}
public double getLength()
{
return length;
}
public void setWidth(double width)
{
this.width = width;
}
public double getWidth()
{
return width;
}
//A method named getArea() that returns the area of this triangle.
public double getArea()
{
return length*width;
}
//A method named getPerimeter() that returns the perimeter of this triangle.
public double getPerimeter()
{
return 2*(length + width);
}
//A method named toString() that returns a string description for the triangle.
public String toString()
{
return "The rectangle was created on "+dateCreated
+" The color is "+getColor()
+" Is Filled ?"+isFilled()
+" Area of Rectangle: "+getArea()
+" Perimeter of Rectangle : "+getPerimeter();
}
}
class Ellipse extends GeometricObject
{
private double a,b;
public Ellipse()
{
a=0.0; // major axis
b = 0.0;//minor axis
}
public Ellipse(double a,double b,String color,boolean filled)
{
super(color,filled);
this.a = a;
this.b = b;
}
//The accessor(getters and setters) methods for major and minor axis of ellipse
public void setA(double a)
{
this.a = a;
}
public double getA()
{
return a;
}
public void setB(double b)
{
this.b = b;
}
public double getB()
{
return b;
}
//A method named getArea() that returns the area of this ellipse
public double getArea()
{
return 3.1412*a*b;
}
//A method named getPerimeter() that returns the perimeter of this ellipse.
public double getPerimeter()
{
return 3.1412*(3*(a+b)-Math.sqrt((3*a+b)*(a+3*b)));
}
//A method named toString() that returns a string description for the ellipse
public String toString()
{
return "The ellipse was created on "+dateCreated
+" The color is "+getColor()
+" Is Filled ?"+isFilled()
+" Area of ellipse : "+getArea()
+" Perimeter of ellipse : "+getPerimeter();
}
}
class GeometricTest
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the radius of the circle : ");
double radius = input.nextDouble();
System.out.println("Enter the color of the circle : ");
String color = input.next();
System.out.println("Enter the boolean value to indicate whether the circle is filled or not : ");
boolean filled = input.nextBoolean();
Circle c = new Circle(radius,color,filled);
System.out.println(c);
System.out.println("Enter the length of the rectangle : ");
double length = input.nextDouble();
System.out.println("Enter the width of the rectangle : ");
double width = input.nextDouble();
System.out.println("Enter the color of the rectangle : ");
color = input.next();
System.out.println("Enter the boolean value to indicate whether the rectangle is filled or not : ");
filled = input.nextBoolean();
Rectangle r = new Rectangle(length,width,color,filled);
System.out.println(r);
System.out.println("Enter the a axis of the ellipse : ");
double a = input.nextDouble();
System.out.println("Enter the b axis of the ellipse : ");
double b = input.nextDouble();
System.out.println("Enter the color of the ellipse : ");
color = input.next();
System.out.println("Enter the boolean value to indicate whether the ellipse is filled or not : ");
filled = input.nextBoolean();
Ellipse e = new Ellipse(a,b,color,filled);
System.out.println(e);
//comparison of areas
System.out.print("Equal area for circle and rectangle : ");
if(c.getArea() == r.getArea())
System.out.print("True");
else
System.out.print("False");
System.out.print(" Equal area for circle and ellipse: ");
if(c.getArea() == e.getArea())
System.out.print("True");
else
System.out.print("False");
System.out.print(" Equal area for ellipse and rectangle : ");
if(e.getArea() == r.getArea())
System.out.print("True");
else
System.out.print("False");
}
}
output:
Enter the radius of the circle :5.0
Enter the color of the circle :Blue
Enter the boolean value to indicate whether the circle is filled or not :true
The circle was created on Sat Mar 17 19:03:41 GMT 2018
The color is Blue
Is Filled ?true
Area of Circle : 78.53
Perimeter of Circle : 31.412
Radius : 5.0
Diameter : 10.0
Enter the length of the rectangle :7.0
Enter the width of the rectangle :5.0
Enter the color of the rectangle :Lime
Enter the boolean value to indicate whether the rectangle is filled or not :true
The rectangle was created on Sat Mar 17 19:03:41 GMT 2018
The color is Lime
Is Filled ?true
Area of Rectangle: 35.0
Perimeter of Rectangle : 24.0
Enter the a axis of the ellipse :3.0
Enter the b axis of the ellipse :4.0
Enter the color of the ellipse :Gold
Enter the boolean value to indicate whether the ellipse is filled or not :false
The ellipse was created on Sat Mar 17 19:03:41 GMT 2018
The color is Gold
Is Filled ?false
Area of ellipse : 37.6944
Perimeter of ellipse : 22.100729174513003
Equal area for circle and rectangle : False
Equal area for circle and ellipse: False
Equal area for ellipse and rectangle : False
Do ask if any query. Please upvote if the answer is helpful.