Create an abstract class named shape that contains two integers and an empty method named printarea().Provide three classes named rectangle, triangle and circle such that each one of the classes extends the class Shape. Each of the inherited class from shape class should provide the implementation for the method printarea(). Get the input and calculate the area of rectangle, circle and triangle.In the Main class, create the objects for the three inherited classes and invoke the methods and display the area values of the different shapes.2(length of rectangle)3(breadth)5(triangle breadth)6(Triangle height)4(circle radius)
Question
Create an abstract class named shape that contains two integers and an empty method named printarea().Provide three classes named rectangle, triangle and circle such that each one of the classes extends the class Shape. Each of the inherited class from shape class should provide the implementation for the method printarea(). Get the input and calculate the area of rectangle, circle and triangle.In the Main class, create the objects for the three inherited classes and invoke the methods and display the area values of the different shapes.2(length of rectangle)3(breadth)5(triangle breadth)6(Triangle height)4(circle radius)
Solution
Here is a Java solution for your problem:
abstract class Shape {
int a;
int b;
abstract void printArea();
}
class Rectangle extends Shape {
Rectangle(int a, int b) {
this.a = a;
this.b = b;
}
void printArea() {
System.out.println("Area of Rectangle: " + (a * b));
}
}
class Triangle extends Shape {
Triangle(int a, int b) {
this.a = a;
this.b = b;
}
void printArea() {
System.out.println("Area of Triangle: " + (0.5 * a * b));
}
}
class Circle extends Shape {
Circle(int a) {
this.a = a;
}
void printArea() {
System.out.println("Area of Circle: " + (3.14 * a * a));
}
}
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(2, 3);
rectangle.printArea();
Triangle triangle = new Triangle(5, 6);
triangle.printArea();
Circle circle = new Circle(4);
circle.printArea();
}
}
In this code, Shape is an abstract class with two integers a and b and an abstract method printArea(). Rectangle, Triangle, and Circle are classes that extend Shape and provide their own implementations of printArea(). In the main method, we create objects of Rectangle, Triangle, and Circle and call their printArea() methods to display the areas of the shapes.
Similar Questions
Consider the base class "Shape" with methods draw() [prints message "Draw"]. Create three subclasses: Circle, Square, and Triangle. Override the draw() method in each subclass to draw the respective shape, and override the calculateArea() method to calculate and return the area of each shape. Implement the above scenario with suitable pseudocode and appropiate java application.User Choice may be "C/S/T". a.) C (Circle), then must read "Radius"b.) S (Square), then must read "Side"c.) T (Triange), then must read both "Height and Base".Identify the suitable data members for the above derived classes and ensure then all are private.
Implement the abstract class Shape with the abstract method calculateArea.Create the derived classes Circle and Rectangle that extend Shape and implement the calculateArea method according to the formulas for each shapeWrite a main program that reads inputs for creating a circle and a rectangle, calculates their areas, and prints the results.Input2 //number of objectsC 10 //Circle with radousR 10 10 //Rectangle with width and height....Output2 lines display the respective area and circumference314.0 62.8100 40Input3C 100R 5 5R 10 10oUTPUT31400.0 62825 20100 40uSE iNHERITANCE WITH ABSTRACT CLASS SHAPE// Find area and Find circumference must be abstract methods Circle and Rectangle should be sub classes
Create a class named ‘Shape’ which has methods ‘getArea’, ‘getPerimeter’and ‘getSide’.Make three subclasses for three different shapes - ‘Circle’, ‘Triangle’ and‘Rectangle’. These subclasses inherit the ‘Shape’ class and they have theirown ‘getArea’, ‘getPerimeter’, and ‘getSide’ methods definition.Each of these classes has its own data members likeThe circle will have a Radius,Triangle will have three sides,A rectangle will have width and heightWrite a program for the above scenario and display the solution.
Create an abstract class SHAPE. Define member functions get_data() and display_area(). Derive five sub classes namely square, triangle, rectangle, circle and ellipse. Compute the area of rectangle, square, triangle, circle and ellipse. Display the result using display_area().Override the display_area() in all the sub classes. Use suitable mechanism to avoid the duplication and link the function display_area() at run time.
Select the correct answerWhat will be the output of the following Java program? class shape { int breadth; int height; int area; void area() { area = 1/2 * breadth * height; } void area(int x) { area = x; } } class triangle { public static void main(String args[]) { shape obj = new shape(); obj.breadth = 4; obj.height = 4; obj.area(4); System.out.println(obj.area); } }Options04816
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.