top of page
Click here to go to the home page of AskTheCode.

Java program to find perimeter using abstract class Shape

Team ATC

Problem solving in java | Java Programming | AskTheCode

 

Problem:

Create an abstract class Shape with abstract method public abstract Double calculatePerimeter().

Create a class Circle that extends Shape with the attribute radius of Float datatype.

Create a class Rectangle that extends Shape with the attributes length & breadths, of Float datatype.

Create a class Square that extends Shape with the attribute side of Float datatype.

Implement the method calculatePerimeter() in all the child classes to calculate appropriate perimeters.

Note: Use 3.14 for Pi value, and display 2 digits after decimal in the answer.


Input Format:

The first line of the input consists of the choice, 1 for circle, 2 for rectangle, 3 for square.

Next input is based on the choice. Radius for the circle, length and breadth for the rectangle, side for the square.


Output Format:

The output prints the perimeter of the selected shape.


Sample testcases:

Input 1:

1

2.34

Output 1:

14.70


Input 2:

2

12

3

Output 2:

30.00


Input 3:

3

13

Output 3:

52.00

 

Code:

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

abstract class Shape{
	public abstract Double calculatePerimeter();
}

class Circle extends Shape {
	private static DecimalFormat df = new DecimalFormat("0.00");
	public Double calculatePerimeter(){
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter radius of the circle: ");
		float radius = sc.nextFloat();
		double result = 2 * 3.14 * radius;
		System.out.println(df.format(result));
		return result;
	}
}

class Rectangle extends Shape {
	private static DecimalFormat df = new DecimalFormat("0.00");
	public Double calculatePerimeter(){
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter length and breadth of the rectangle: ");
		float length = sc.nextFloat();
		float breadth = sc.nextFloat();
		double result = 2 * (len + breadth);
		System.out.println(df.format(result));
		return result;
	}
}

class Square extends Shape {
	private static DecimalFormat df = new DecimalFormat("0.00");
	public Double calculatePerimeter(){
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter side of the square: ");
		float side = sc.nextFloat();
		double result = 4 * side;
		System.out.println(df.format(result));
		return result;
	}
}

public class AbstractClass_Perimeter{
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		System.out.println("Select the shape whose perimeter you want to calculate:");
		System.out.println("1 for circle\t2 for rectangle \t3 for square: ");
		int choice = Integer.parseInt(br.readLine());
		switch(choice){
			case 1:
			Shape c = new Circle();
			c.calculatePerimeter();
			break;

			case 2:
			Shape r = new Rectangle();
			r.calculatePerimeter();
			break;

			case 3:
			Shape s = new Square();
			s.calculatePerimeter();
			break;

			default:
			System.out.println("Wrong Selection!!!\nExited the program.");
		}
	}
}

Recent Posts

See All

Comments


bottom of page