Knowee
Questions
Features
Study Tools

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.

Question

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.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is the pseudocode and Java code for the scenario you described:

Pseudocode:

Class Shape {
  Method draw() {
    print "Draw"
  }
  Method calculateArea() {
  }
}

Class Circle extends Shape {
  private Variable radius

  Method draw() {
    print "Draw Circle"
  }
  Method calculateArea() {
    return 3.14 * radius * radius
  }
}

Class Square extends Shape {
  private Variable side

  Method draw() {
    print "Draw Square"
  }
  Method calculateArea() {
    return side * side
  }
}

Class Triangle extends Shape {
  private Variable base
  private Variable height

  Method draw() {
    print "Draw Triangle"
  }
  Method calculateArea() {
    return 0.5 * base * height
  }
}

Java Code:

abstract class Shape {
  abstract void draw();
  abstract double calculateArea();
}

class Circle extends Shape {
  private double radius;

  Circle(double radius) {
    this.radius = radius;
  }

  void draw() {
    System.out.println("Draw Circle");
  }

  double calculateArea() {
    return Math.PI * radius * radius;
  }
}

class Square extends Shape {
  private double side;

  Square(double side) {
    this.side = side;
  }

  void draw() {
    System.out.println("Draw Square");
  }

  double calculateArea() {
    return side * side;
  }
}

class Triangle extends Shape {
  private double base;
  private double height;

  Triangle(double base, double height) {
    this.base = base;
    this.height = height;
  }

  void draw() {
    System.out.println("Draw Triangle");
  }

  double calculateArea() {
    return 0.5 * base * height;
  }
}

In the Java code, we have an abstract base class Shape with abstract methods draw() and calculateArea(). The subclasses Circle, Square, and Triangle each override these methods to provide their own implementations. The draw() method prints a message indicating the type of shape being drawn, and the calculateArea() method calculates and returns the area of the shape. The Circle, Square, and Triangle classes each have private data members to store the dimensions of the shape (radius for Circle, side for Square, and base and height for Triangle). These dimensions are set through the constructor of each class.

This problem has been solved

Similar Questions

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)

Create a 2 separate Classes CIRCLE  with private data members - Radius and Area, and RECTANGLE with private data members - Length, Breadth and Area. In each of these classes declare and define the member methods putData() [ To display Area ] and computeArea()[ To Compute Area ] are the private and getData() as public member method to read Radius or Length and Breadth based on the choice('C' or 'R'). As a programmer, Write a java program and subsequent pseudocode, to find the area of circle/rectangle using class CIRCLE/RECTANGLE which have following details:getData() to read either radius or lenght and breadth based on the choice 'C' or 'R'Calculate the area using computeArea()Display the Area using putData().Note :Radius is always +Ve, otherwise print "Invalid Radius"Length and Breadth are +Ve, otherwise print "Invalid Length(if Length is -VE) or Invalid Breadth(if Breadth is -VE) or "Invalid Length and Breadth"Input : First line Read the type ('C'  or 'R')Second line onwards read Radius(if Type = 'C') or Length and Breadth (if Type = 'R')Output :  print Area

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

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.

1/3

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.