Question

In: Computer Science

In C++, Design and implement an ADT that represents a triangle. The data for the ADT...

In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also should include operations that look at the values of the ADT’s data; change the values of the ADT’s data; compute the triangle’s area; and determine whether the triangle is a right triangle, an equilateral triangle, or an isosceles triangle.

Solutions

Expert Solution

Code

triangle.h

#ifndef TRIANGLE_H
#define TRIANGLE_H
/** Header file for the class Triangle. */

#include <cmath>
#include <iostream>
using namespace std;

template<class T>
class Triangle
{
private:
T side1,side2,side3;
T angle1,angle2,angle3;

public:
Triangle();
Triangle(T, T, T, T, T, T);
virtual void setSides(T newSide1, T newSide2, T newSide3,T ang1, T ang2, T ang3);
virtual T getArea() const;
virtual T getPerimeter() const;
virtual bool isRightTriangle() const;
virtual bool isEquilateral() const;
virtual bool isIsosceles() const;
};

template<class T>
Triangle<T>::Triangle()
{

}

template<class T>
Triangle<T>::Triangle(T side1, T side2, T side3,T ang1, T ang2, T ang3)
{
Triangle<T>::setSides(side1, side2, side3,ang1,ang2,ang3);
}

template<class T>
void Triangle<T>::setSides(T newSide1, T newSide2, T newSide3,T ang1, T ang2, T ang3)
{
side1 = newSide1;
side2 = newSide2;
side3 = newSide3;
   angle1=ang1;
   angle2=ang2;
   angle3=ang3;
}


template<class T>
T Triangle<T>::getArea() const
{
double p = Triangle<T>::getPerimeter() / 2;
T area = sqrt(p*(p-side1)*(p-side2)*(p-side2));
return area;
}

template<class T>
T Triangle<T>::getPerimeter() const
{
T perimeter = side1+side2+side3;
return perimeter;
}

template<class T>
bool Triangle<T>::isRightTriangle() const
{
if(side1*side1 == (side2*side2) + (side2*side2))
return true;
if(side3*side3 == (side1*side1) + (side2*side2))
return true;
if(side2*side2 == (side1*side1) + (side3*side3))
return true;
else
return false;
}

template<class T>
bool Triangle<T>::isEquilateral() const
{
if(side1 == side2 && side3 == side2)
return true;
else
return false;
}

template<class T>
bool Triangle<T>::isIsosceles() const
{
if(side1 == side2 || side2 == side3 || side1 == side3)
return true;
else
return false;
}


#endif

main.cpp

#include <iostream>

#include "triangle.h"

using namespace std;

int main() {

Triangle<float> t1;
Triangle<float> t2(4.0, 5.0, 3.0,90,45,45);
Triangle<int> t3(2, 3, 400,60,45,75);
  
   cout<<"Area of trinable is :"<<t2.getArea()<<endl;
   if(t2.isEquilateral())
       cout<<"Its Equilateral"<<endl;
   if(t2.isIsosceles())
       cout<<"Its Isosceles"<<endl;
   if(t2.isRightTriangle())
       cout<<"Its Right angle"<<endl;

return 0;
}

ouput

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS:...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS: Not allowed to use ANY built-in Python data structures and their methods. You must solve by importing the DynamicArray class and using class methods to write solution. Also not allowed to directly access any variables of the DynamicArray class (like self.size, self.capacity and self.data in part 1). All work must be done by only using class methods. Below is the Bag ADT starter code...
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made...
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made with the fundamental functionalities: Visit - Description: It will display each of the data stored in the BST depending on the input parameter:Preorder Inorder Bidder Level by level Input - An integer (1-4) Exit - Nothing Precondition - A valid BST Postcondition - Nothing Height - Description:Will return the height of the BST Entry - Nothing Output - An integer with which to indicate...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using the adjacency matrix structure. LANGUAGE IS IN JAVA Comment for any questions Data structures and algorithms
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x^5 + 7 x^3 – x^2 + 9 ). For this problem, consider only polynomials whose exponents are non-negative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions: One...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
Car Rental Management System in C++ The aim of this project is to design and implement...
Car Rental Management System in C++ The aim of this project is to design and implement a computerized Car Rental Management System for a company called COEN244Cars. The company rents two types of cars: standard and luxury cars. A car is identified by a car identification number (int), a type (string), and a flag that indicates whether the car is currently available or not. The company distinguishes between three types of customers: regular customers, corporate customers, and VIPs (Very Important...
Design and implement a C++ program that performs the following steps:Ask the user to enter a...
Design and implement a C++ program that performs the following steps:Ask the user to enter a positive integer number N; Your program may need to prompt the user to enter many times until it reads in a positive number;Let user to enter N (obtained in the previous step) floating point numbers, and count how many positive ones there are in the sequence and sum up these positive numbers; (Hint: negative numbers or 0 are ignored).Display result.You can and should use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT