0% found this document useful (0 votes)
15 views

Program 4 5

Uploaded by

Deeksha Sudheer
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Program 4 5

Uploaded by

Deeksha Sudheer
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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) {
this.radius = radius;
}

double area() {
//Area calculation
return Math.PI * radius * radius;
}

double perimeter()
{ //Perimeter calculation
return 2 * Math.PI * 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;
this.height = 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

System.out.println("area of a Circle : " + circle.area());


System.out.println(" perimeter of a circle : " + circle.perimeter());

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

System.out.println("area of a triangle : " + triangle.area());


System.out.println("perimeter of a triangle : " + triangle.perimeter());
}
}

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) {
this.width = width;
this.height = height;
}

// Implement the "resizeWidth" method to resize the width


public void resizeWidth(int width) {
this.width = width;
}

// Implement the "resizeHeight" method to resize the height


public void resizeHeight(int height) {
this.height = height;
}

// Method to print the current width and height of the rectangle


public void printSize() {
System.out.println("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


rectangle.printSize();

// Resize the rectangle by changing its width and height


rectangle.resizeWidth(150);
rectangle.resizeHeight(200);

// Print the updated size of the rectangle


rectangle.printSize();
}
}

You might also like