1) Specify, design and implement abase class called Shape. This is an abstract c
ID: 3610982 • Letter: 1
Question
1) Specify, design and implement abase class called Shape. This is an abstract class with thefollowing functions:
a. float area()
b. float perimeter()
c. String color()
The methods called area() andperimeter() are abstract as till now we do notknow the shape itself.
Specify, design and implement the following derived classes
a) Circle
b) Sqaure
c) Rectangle
These classes should define the area and perimeter. Writeconstructor with your choice. Write a main program to take inputand show the output.
Explanation / Answer
import java.io.*; import java.util.*; import java.lang.*; abstract class Shape { String color="red"; abstract float area(); abstract float perimeter(); String color() { return color; } } class Circle extends Shape { public float radius; public Circle(float r) { this.radius=r; } public Circle() { this.radius=2; } public float area() { float area= (22 * radius *radius)/7; return area; } public float perimeter() { float perim= (2 *22 * radius )/7; return perim; } }//end of circle class class Square extends Shape { public float length; public Square(float l) { this.length=l; } public Square() { this.length=2; } public float area() { float area= (length * length); return area; } public float perimeter() { float perim= (4 * length); return perim; } }//end of Square class class Rectangle extends Shape { public float length; public float breadth; public Rectangle(float l, float b) { this.length=l; this. breadth=b; } public Rectangle() { this.length=2; this. breadth=2; } public float area() { float area= (length * breadth); return area; } public float perimeter() { float perim= 2* ( breadth +length); return perim; } }//end of Rectangle class public class ShapeMain { public static void main(String[] args){ Circle c = new Circle (1); System.out.println(c.area()); System.out.println(c.perimeter()); System.out.println(); Square s = new Square(1); System.out.println(s.area()); System.out.println(s.perimeter()); System.out.println(); Rectangle r = new Rectangle(4,2); System.out.println(r.area()); System.out.println(r.perimeter()); System.out.println(); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.