Knowee
Questions
Features
Study Tools

Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by creating a class named 'Triangle' with a function to print the area and perimeter.

Question

Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by creating a class named 'Triangle' with a function to print the area and perimeter.

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

Solution

Sure, here is a Python program that creates a class named 'Triangle' and includes a method to calculate and print the area and perimeter of a triangle.

import math

class Triangle:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def area(self):
        # calculate the semi-perimeter
        s = (self.a + self.b + self.c) / 2
        # calculate the area
        area = math.sqrt(s*(s-self.a)*(s-self.b)*(s-self.c))
        return area

    def perimeter(self):
        # calculate the perimeter
        perimeter = self.a + self.b + self.c
        return perimeter

# create a triangle object
t = Triangle(3, 4, 5)

# print the area and perimeter of the triangle
print("Area of the triangle: ", t.area())
print("Perimeter of the triangle: ", t.perimeter())

In this program, we first import the math module to use the sqrt function for calculating the area. We then define the Triangle class with the init method to initialize the sides of the triangle. The area and perimeter methods calculate and return the area and perimeter of the triangle, respectively. Finally, we create a Triangle object with sides of 3, 4, and 5 units, and print its area and perimeter.

This problem has been solved

Similar Questions

#include <iostream>#include <string>#include <cmath>using namespace std;class Triangle{public: void print_area(int s1, int s2, int s3) { double s = (s1+s2+s3)/2.0; cout << s << endl; cout << "Perimeter is " << (s1+s2+s3) << endl; }};int main(){ Triangle t; t.print_area(3,4,5); return 0;}

Write a program to print the area of a rectangle by creating a class named 'Area' having two methods. First method named as 'setDim' takes length and breadth of rectangle as parameters and the second method named as 'getArea' returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard.

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)

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

Write a program to calculate the area of circle, triangle, rectangle and square using function overloading.

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.