Write a progam that inputs three numbers from the user and informs the user whet
ID: 3677482 • Letter: W
Question
Write a progam that inputs three numbers from the user and informs the user whether or not those values could represent the sides of a triangle. The program itself should consist of at least two functions: main and isTriangle.
The main function must input three double values from the console. The numbers which the user enters should be stored in double variables, with names sideA, sideB, and sideC. After the input of sideA, sideB, and sideC, the main function should call another function called isTriangle. The function prototype for isTriangle is given below:
bool isTriangle(double, double, double);
If the value returned by isTriangle is true, then the main function should output a message to the user, stating that a triangle can be formed with those three side values. If the value returned by isTriangle is false, then the main function should output a message to the user that a triangle cannot be formed with those three sie values.
The isTriangle function should return false if:
- Any of the input parameters is less than or equal to zero, or Any of the input parameters is greater than or equal to the sum of the other two input parameters.
Otherwise, the isTriangle function should return true.
I have completed the beginning of this assignment but for some reason cannot get the return false portion to work. Any help would be reatly appreciated. Thank you in advance!
Explanation / Answer
Triangle.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Resources;
namespace Task4_lib
{
public class Triangle
{
uint a, b, c;
public struct Point
{
public uint x, y;
}
public Triangle(uint a, uint b, uint c)
{
if (!IsTriangle(a, b, c) || a == 0 || b == 0 || c == 0)
throw new ArgumentException(Properties.Resources.ArgExcString);
this.a = a;
this.b = b;
this.c = c;
}
public Triangle(Point a,Point b,Point c)
{
if (a.Equals(null) || b.Equals(null) || c.Equals(null))
throw new ArgumentNullException(Properties.Resources.ArgNullExcString);
uint sideA = GetSide(a, b);
uint sideB = GetSide(a, c);
uint sideC = GetSide(b, c);
if (!IsTriangle(sideA, sideB, sideC) || sideA == 0 || sideB == 0 || sideC == 0)
throw new ArgumentException(Properties.Resources.ArgExcString);
this.a = sideA;
this.b = sideB;
this.c = sideC;
}
/// <summary>
/// Method calclate side's of triangle
/// </summary>
/// <param name="coord"></param>
/// <returns></returns>
private uint GetSide(Point p1,Point p2)
{
if (p1.x == p2.x )
return (uint)Math.Abs(p1.y-p2.y);
if (p1.y == p2.y)
return (uint)Math.Abs(p1.x - p2.x);
uint x = (uint)Math.Abs(p1.x - p2.x);
uint y = (uint)Math.Abs(p1.y - p2.y);
return (uint)Math.Sqrt(x * x + y * y);
}
public uint A
{
get
{
return a;
}
set
{
if (value < 0 || !IsTriangle(value,b,c))
throw new ArgumentException(Properties.Resources.ArgExcString);
a = value;
}
}
public uint B
{
get
{
return b;
}
set
{
if (value < 0 || !IsTriangle(a, value, c))
throw new ArgumentException(Properties.Resources.ArgExcString);
b = value;
}
}
public uint C
{
get
{
return c;
}
set
{
if (value < 0 || !IsTriangle(a,b,value))
throw new ArgumentException(Properties.Resources.ArgExcString);
c = value;
}
}
/// <summary>
/// The method takes three sides and verifies the existence of a triangle
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="c"></param>
/// <returns></returns>
private bool IsTriangle(uint a, uint b, uint c)
{
if (a + b > c && a + c > b && c + b > a)
return true;
return false;
}
/// <summary>
/// The method calculate perimeter of the triangle
/// </summary>
/// <returns></returns>
public uint Perimeter()
{
return a + b + c;
}
/// <summary>
/// The method calculate square of the thiangle
/// </summary>
/// <returns></returns>
public double CalcSquare()
{
double p = Perimeter()/2;
return Math.Sqrt(p * (p - c) * (p - b) * (p - a));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.