Please use JAVA PROGRAMMING double length double width int n
Please use JAVA PROGRAMMING
<Ractangle>
double length
double width
int numRectangles
Rectangle()
Rectangle(double length, double width)
getters
setters
double calaArea()
double calaperimeter()
boolean isSquare()
string toString() ->should print
________________________________________________
<Circle>
double radius
int numCircles
double calcArea ()
double calcPerimeter ()
double Diameter ()
double getRadius ()-getter
void setRadius ()-setter
Circle ()
Circle (radius, double)
int get NumCircles
angle double iena num Rech tanal techn Cal Area calc meter fun SinnSolution
Shape.java
public abstract class Shape {
public abstract double calArea();
public abstract double calPerimeter();
}
______________
Rectangle.java
public class Rectangle extends Shape {
// Declaring instance variables
private double length;
private double width;
// parameterized constructor
public Rectangle(double length, double width) {
super();
this.length = length;
this.width = width;
}
// Setters and getters
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
// This method is used to calculate the area of the Rectangle
@Override
public double calArea() {
return getLength() * getWidth();
}
// This method is used to calculate the perimeter of the Rectangle
@Override
public double calPerimeter() {
return 2 * (getLength() + getWidth());
}
// This method will check whether the Rectangle is square or not
public boolean isSquare() {
if (getLength() == getWidth())
return true;
else
return false;
}
// This method is used to display the contents of an object inside it
@Override
public String toString() {
return \"length=\" + length + \", width=\" + width;
}
}
____________________
Circle.java
public class Circle extends Shape {
//Declaring instance variables
private double radius;
private int numCircles;
//parameterized constructor
public Circle(double radius, int numCircles) {
super();
this.radius = radius;
this.numCircles = numCircles;
}
//Setters and getters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public int getNumCircles() {
return numCircles;
}
public void setNumCircles(int numCircles) {
this.numCircles = numCircles;
}
//This method will calculate the diameter of the cirlce
public double diameter()
{
return 2*getRadius();
}
//This method is used to calculate the area of the circle
@Override
public double calArea() {
return Math.PI*getRadius()*getRadius();
}
//This method is used to calculate the perimeter of the circle
@Override
public double calPerimeter() {
return 2*Math.PI*getRadius();
}
//This method is used to display the contents of an object inside it
@Override
public String toString() {
return \"radius=\" + radius + \", numCircles=\" + numCircles;
}
}
_______________
Test.java
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args) {
//DecimalFormat class is used to format the output
DecimalFormat df=new DecimalFormat(\"#.##\");
//Creating the Circle class obect
Shape c=new Circle(5.5, 2);
System.out.println(\"Circle#\");
System.out.println(c.toString());
//Displaying the Diameter of the circle
System.out.println(\"Diameter of the Circle :\"+((Circle) c).diameter());
//Displaying the Area of the circle
System.out.println(\"Area of the Circle :\"+df.format(c.calArea()));
//Displaying the Perimeter of the circle
System.out.println(\"Perimeter of the Circle :\"+df.format(c.calPerimeter()));
//Creating the Rectangle class object
Shape rect=new Rectangle(4,4);
System.out.println(\"\ Rectangle#\");
System.out.println(rect.toString());
//displaying the area of the rectangle
System.out.println(\"Area of the Rectangle :\"+df.format(rect.calArea()));
//Displaying the perimeter of the Rectangle
System.out.println(\"Perimeter of the Rectangle :\"+df.format(rect.calPerimeter()));
boolean bool=((Rectangle)rect).isSquare();
if(bool)
{
System.out.println(\"The Rectangle is Square\");
}
else
System.out.println(\"The Rectangle is not Square\");
}
}
_________________
Output:
Circle#
radius=5.5, numCircles=2
Diameter of the Circle :11.0
Area of the Circle :95.03
Perimeter of the Circle :34.56
Rectangle#
length=4.0, width=4.0
Area of the Rectangle :16
Perimeter of the Rectangle :16
The Rectangle is Square
_____________Thank You



