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...
Overview: implement the ADT List in Java. This program is meant to the ADT List from...
Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList. Requirements You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will...
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...
In a single design, implement 3 wave generators by means of operational amplifiers: wave square, triangle...
In a single design, implement 3 wave generators by means of operational amplifiers: wave square, triangle wave, sine wave. IMPORTANT The wave generating equipment has to be activated by means of an external AC source, that being activated by a switch, allows the activation of the system. SIMULATION is required in the MULTISIM Software. Attach screenshot of the entire circuit and the waves. and also the calculations. Prepare the calculations of each case, which support the output signal obtained by...
You will implement the MyList ADT according to the following: 1. MyList must implement the List...
You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will look something like below: public class MyList<E> implements List<E> { ... } Note: As said in the class, when implementing an interface, you have to implement each method in it. There are a lot of methods in the List interface. We are going to address this issue below. 2. The initial capacity of MyList must be 2. This is not an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT