Program 4: Develop a JAVA program to create an abstract class Shape with abstract
methods calculate Area()
and calculate Perimeter(). Create subclasses Circle and Triangle that extend the
Shape class and
implement the respective methods to calculate the area and perimeter of each shape.
// Abstract class Shape
abstract class Shape {
abstract double area();
// Abstract methods
abstract double perimeter();
}
// Subclass Circle extends Shape
class Circle extends Shape {
private double radius;
//Instance variable radius
// Constructor to initialize radius
public Circle(double radius) {
[Link] = radius;
}
double area() {
//Area calculation
return [Link] * radius * radius;
}
double perimeter()
{ //Perimeter calculation
return 2 * [Link] * radius;
}
}
// Subclass Triangle extends Shape
class Triangle extends Shape {
private double a, b,
c; // Instance variables
sides and height of the triangle
private double height;
// Constructor to initialize sides and height
public Triangle(double a, double b, double c, double height) {
this.a = a;
this.b = b;
this.c = c;
[Link] = height;
}
double area() {
//Area calculation
return (b * height) / 2;
}
double perimeter() {
//Perimeter calculation
return a + b + c;
}
}
// Main class
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5.0);
//Creating an object "circle" with radius 5.0
[Link]("area of a Circle : " + [Link]());
[Link](" perimeter of a circle : " + [Link]());
Shape triangle = new Triangle(2.0, 4.0, 3.0, 5.0);
//Creating an object "circle" with sides 2.0, 4.0, 3.0 and height 5.0
[Link]("area of a triangle : " + [Link]());
[Link]("perimeter of a triangle : " + [Link]());
}
}
5. Develop a JAVA program to create an interface Resizable with methods resize
Width(int width) and resize Height(int height) that allow an object to be resized.
Create a class Rectangle that implements the Resizable interface and implements the
resize methods
// Interface Resizable
// Declare the Resizable interface
interface Resizable {
// Declare the abstract method "resizeWidth" to resize the width
void resizeWidth(int width);
// Declare the abstract method "resizeHeight" to resize the height
void resizeHeight(int height);
}
// Declare the Rectangle class, which implements the Resizable interface
class Rectangle implements Resizable {
// Declare private instance variables to store width and height
private int width;
private int height;
// Constructor for initializing the width and height
public Rectangle(int width, int height) {
[Link] = width;
[Link] = height;
}
// Implement the "resizeWidth" method to resize the width
public void resizeWidth(int width) {
[Link] = width;
}
// Implement the "resizeHeight" method to resize the height
public void resizeHeight(int height) {
[Link] = height;
}
// Method to print the current width and height of the rectangle
public void printSize() {
[Link]("Width: " + width + ", Height: " + height);
}
}
// Declare the Main class
public class Main {
public static void main(String[] args) {
// Create an instance of the Rectangle class with an initial size
Rectangle rectangle = new Rectangle(100, 150);
// Print the initial size of the rectangle
[Link]();
// Resize the rectangle by changing its width and height
[Link](150);
[Link](200);
// Print the updated size of the rectangle
[Link]();
}
}