75.1k views
5 votes
WAP in Java Create a class Data with data members: height and breadth of object given by the user and member functions get_data() to read the values and put_data() to display the values. Create another class Rectangle that inherits class Data and implement its methods areaRectangle () and perimeterRectangle() that computes the area and perimeter of a rectangle. Create another class Triangle that inherits class Data and implement its methods areaTriangle() and perimeterTriangle() that computes the area and perimeter of a triangle

1 Answer

7 votes

Final answer:

A student needs to write a program in Java with a class Data that has members height and breadth, and methods get_data() and put_data(). Inherited classes Rectangle and Triangle will compute area and perimeter of a rectangle and triangle respectively.

Step-by-step explanation:

To answer the question, we'll create a class Data in Java with two data members: height and breadth, which will be provided by the user. The class will also have two member functions: get_data() to read the values, and put_data() to display the values. We'll then create a class Rectangle that inherits from Data and implements methods areaRectangle() and perimeterRectangle() to compute the area and perimeter of a rectangle, respectively.

Similarly, we'll create another class Triangle that also inherits from Data and implements methods areaTriangle() and perimeterTriangle() to calculate the area and perimeter of a triangle. The area of a triangle can be computed using Heron's formula, which requires the lengths of all three sides. Since the question only provides height and breadth, we will assume these to correspond to a right-angled triangle's base and height for simplicity.

Example Implementation

Class Data:
// Implement the get_data and put_data methods

Class Rectangle:
// Calculate the area by multiplying height by breadth
// Calculate the perimeter by adding double the height and breadth

Class Triangle:
// Calculate the area using 0.5 * base * height
// Calculate the perimeter by adding the base, height, and hypotenuse (calculated using the Pythagorean theorem)

User Directedition
by
7.6k points