Question

Triangle stuff. Write a function IsValid(side1, side2, side3) that takes as argument 3 positive integers and...

  1. Triangle stuff.
    1. Write a function IsValid(side1, side2, side3) that takes as argument 3 positive integers and returns True if the three sides can form a triangle, False otherwise.
    2. Write a function Perimeter(side1, side2, side3) that takes 3 sides of a triangle and returns the perimeter of the triangle. Remember to only calculate the perimeter of valid triangles that you determined through the IsValid function.
    3. Now use Heron’s formula to calculate the area of a triangle with sides side1, side2, side3. Call this function Area(side1, side2, side3). Look up Heron’s formula online.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go this code and let me know if u need any changes in this.Thank You
_________________

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

// Creatingan Exception class
class InvalidTriangleException : public exception {
private:
public:
// Zero argumented constructor
InvalidTriangleException()
{
}

virtual const char* what() const throw()
{
return "** Invalid Triangle **";
}
} myex;

class Triangle {
private:
//Declaring variables
int side1;
int side2;
int side3;

public:
//Parameterized constructor
Triangle(int side1, int side2, int side3)
{
if ((side1 + side2) > side3
&& (side2 + side3) > side1
&& (side3 + side1) > side2) {
this->side1 = side1;
this->side2 = side2;
this->side3 = side3;
}
else {
throw myex;
}
}
//Getters
int getSide1()
{
return side1;
}
int getSide2()
{
return side2;
}
int getSide3()
{
return side3;
}
//This function will calculate area of triangle
double area()
{
// calculating the semi perimeter
double s = (side1 + side2 + side3) / 2.0;
// calculating area
double area = sqrt(s * (s - side1) * (s - side2) * (s - side3));
return area;
}
//This function will calculate perimeter of triangle
double perimeter()
{
// calculating perimeter
double peri = side1 + side2 + side3;
return peri;
}
};
int main()
{
//Declaring variables
int side1, side2, side3;

/* This loop continues to execute until
       * the user enters valid sides of triangle
       */
while (true) {
cout << "Triangle#1" << endl;
cout << "Enter side 1:";
cin >> side1;
cout << "Enter side 2:";
cin >> side2;
cout << "Enter side 3:";
cin >> side3;
try {
Triangle t(side1, side2, side3);
cout << "Area of Triangle :" << t.area() << endl;
cout << "Perimeter of Triangle :" << t.perimeter() << endl;
break;
}
catch (exception& myex) {
cout << myex.what() << endl;
continue;
}
}

return 0;
}
___________________________

Output:

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Triangle stuff. Write a function IsValid(side1, side2, side3) that takes as argument 3 positive integers and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

  • Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task...

    Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task Create the following fields and methods for a Triangle class that extends the provided GeometricShapes class public class GeometricShapes { private String color = "red"; private boolean filled; private java.util.Date dateCreated;    public GeometricShapes() { dateCreated = new java.util.Date(); }    public GeometricShapes(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; }    public void setColor (String color)...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.* A no-arg constructor that creates a default triangle.* A constructor that creates a triangle with the specified side1, side2, and side3.* The accessor methods for all three data fields.* A method named getArea() that returns...

  • Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3:...

    Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3: 5 Enter the base: 5 Enter the height: 8 --------------------- The triangle is Isosceles since it has two equal length sides. Isosceles triangle also has two equal angles The area is: 20 The permimeter is: 13 Continue? (y/n): y Enter length 1: 10 Enter length 2: 10 Enter length 3: 10 Enter the base: 10 Enter the height: 7 --------------------- The triangle is Equilateral...

  • (Need to complete the methods and pass a Junit test i can email them to you.)...

    (Need to complete the methods and pass a Junit test i can email them to you.) public class Triangle implements Comparable {    /**    * Stores the number of objects instantiated from the {@code Triangle} class    */    private static int numTriangles;    /**    * The shortest side of the triangle    */    private final double a;    /**    * The side of medium length of the triangle    */    private final double b;...

  • Write a function that takes, as an argument, a list of positive integers and a target...

    Write a function that takes, as an argument, a list of positive integers and a target value, and returns the number of times that the target value appears in the list. Call this function problem1(myList, target). For example, >>>problem1([1,2,3,4,5,6,5,4,3], 5) should return 2, and >>>problem1([1,2,3,4,5,6,5,4,3], 7) should return 0.

  • (JAVA) Implement a Triangle class. Any triangle can be represented by its THREE sides. Therefore,...

    (JAVA) Implement a Triangle class. Any triangle can be represented by its THREE sides. Therefore, your class will have THREE private member variables → side1, side2 and side3. Use the double data type to represent the triangle sides. In addition, please provide public methods that perform the following FIVE tasks: ▪ An input method that obtains the appropriate values for the three sides from the user. While entering the values of the three sides, please remember the triangle property that...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • Write the java program: A right triangle can have sides whose lengths are all integers. The...

    Write the java program: A right triangle can have sides whose lengths are all integers. The set of three integer values for the length of the sides of a triangle is called a Pythagorean triple. The length of the three sides must satisfy the relationship that the sum of the squares of the sides is equal to the square of the hypotenuse. Write a Java application that prompts the user for an integer that represents the largest side value and...

  • Write the java program: A right triangle can have sides whose lengths are all integers. The...

    Write the java program: A right triangle can have sides whose lengths are all integers. The set of three integer values for the length of the sides of a triangle is called a Pythagorean triple. The length of the three sides must satisfy the relationship that the sum of the squares of the sides is equal to the square of the hypotenuse. Write a Java application that prompts the user for an integer that represents the largest side value and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT