Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.
Become a Premium Member and unlock a new, free course in leading technologies each month.
int GetCubicType(int Height, int Width, int Length)
{
if (Height < 0 || Width < 0 || Length < 0)
return 4;
else
{
if ((Height == Width) && (Height == Length))
return 3;
else
if ((Height == Width) || (Width == Length) || (Length == Width))
return 2;
else
return 1;
}
}
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
List<BusinessObject> theList = // initialized with various BusinessObject instances
BusinessObject foundObject = null;
foreach (BusinessObject item in theList)
{
if (item.ID == 10)
{
foundObject = item;
break;
}
}
if (foundObject != null)
{
// do something with foundObject
}
BusinessObject foundObject = theList.FirstOrDefault(item => item.ID == 10);
if (foundObject != null)
{
// do something with foundObject
}
int GetCubicType(int Height, int Width, int Length)
{
Func<int, int, int, bool> dimensionsAreInvalid = (h, w, l) => (h < 0 || w < 0 || l < 0);
Func<int, int, int, bool> isIsosceles = (h, w, l) => ((h == w) && (h == l));
Func<int, int, int, bool> isScalene = (h, w, l) => ((h == w) || (w == l) || (l == w));
if (dimensionsAreInvalid(Height, Width, Length)) return 4;
if (isIsosceles(Height, Width, Length)) return 3;
if (isScalene(Height, Width, Length)) return 2;
return 1;
}
int GetCubicType(int Height, int Width, int Length)
{
if (Height < 0 || Width < 0 || Length < 0) return 4;
if ((Height == Width) && (Height == Length)) return 3;
if ((Height == Width) || (Width == Length) || (Length == Width)) return 2;
return 1;
}
Lamda's don't help here - in fact in my opinion they will only complicate matters as in reading each of the lines you have to refer back to the lamda to figure out what it does.using System;
using System.Collections.Generic;
class Program
{
public enum CubicType
{
Scalene = 1,
Isosceles = 2,
Equilateral = 3,
Error = 4,
};
static void Main()
{
CubicType i;
i = GetCubicType(0,2,5); // Returns Scalene
i = GetCubicType(0, 2, -1); // Returns Error
i = GetCubicType(0, 0, 0); // Returns Equilateral
i = GetCubicType(1, 1, 1); // Returns Equilateral
i = GetCubicType(5, 1, 5); // Returns Scalene
}
private static CubicType GetCubicType(int Height, int Width, int Length)
{
if (Height < 0 || Width < 0 || Length < 0)
{
Console.Write(CubicType.Error);
return CubicType.Error;
}
else
if ((Height == Width)&& (Height == Length))
{
Console.Write(CubicType.Equilateral);
return CubicType.Equilateral;
}
else
if ((Height == Width) || (Width == Length) || (Length == Width))
{
Console.Write(CubicType.Isosceles);
return CubicType.Isosceles;
}
else
{
Console.Write(CubicType.Scalene);
return CubicType.Scalene;
}
}
}
in fact in my opinion they will only complicate matters as in reading each of the lines you have to refer back to the lamda to figure out what it does.Could that not be said of any function?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalculatorProgram
{
public class Triangle
{
public enum CubicType
{
Scalene = 1,
Isosceles = 2,
Equilateral = 3,
Error = 4,
};
private static CubicType GetCubicType(int Height, int Width, int Length)
{
if (Height <= 0 || Width <= 0 || Length <= 0)
{
return CubicType.Error;
}
else
if ((Height == Width) && (Height == Length))
{
return CubicType.Equilateral;
}
else
if ((Height == Width) || (Width == Length) || (Length == Width))
{
return CubicType.Isosceles;
}
else
{
return CubicType.Scalene;
}
}
}
}
CubicType i;
i = GetCubicType(0,2,5); // Returns Scalene
i = GetCubicType(0, 2, -1); // Returns Error
i = GetCubicType(0, 0, 0); // Returns Equilateral
i = GetCubicType(1, 1, 1); // Returns Equilateral
i = GetCubicType(5, 1, 5); // Returns Scalene
using System;
namespace _28171974
{
class Program
{
static void Main()
{
Geometry.CubicType i;
i = Geometry.GetCubicType(0, 2, 5); // Returns Scalene
i = Geometry.GetCubicType(0, 2, -1); // Returns Error
i = Geometry.GetCubicType(0, 0, 0); // Returns Equilateral
i = Geometry.GetCubicType(1, 1, 1); // Returns Equilateral
i = Geometry.GetCubicType(5, 1, 5); // Returns Scalene
}
}
public class Geometry
{
public enum CubicType
{
Scalene = 1,
Isosceles = 2,
Equilateral = 3,
Error = 4,
};
public static CubicType GetCubicType(int Height, int Width, int Length)
{
if (Height < 0 || Width < 0 || Length < 0)
{
Console.Write(CubicType.Error);
return CubicType.Error;
}
else if ((Height == Width) && (Height == Length))
{
Console.Write(CubicType.Equilateral);
return CubicType.Equilateral;
}
else if ((Height == Width) || (Width == Length) || (Length == Width))
{
Console.Write(CubicType.Isosceles);
return CubicType.Isosceles;
}
else
{
Console.Write(CubicType.Scalene);
return CubicType.Scalene;
}
}
}
}
using _28171974;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
[TestClass]
public class GeometryTests
{
[TestMethod]
public void When_Calling_GetCubicType__If_Invalid_Height_Is_Passed__Then_CubicType_Error_Should_Be_Returned()
{
// Arrange
Geometry.CubicType expected = Geometry.CubicType.Error;
Geometry.CubicType actual;
int h = -1;
int w = 1;
int l = 1;
// Act
actual = Geometry.GetCubicType(h, w, l);
// Assert
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void When_Calling_GetCubicType__If_Invalid_Width_Is_Passed__Then_CubicType_Error_Should_Be_Returned()
{
// Arrange
Geometry.CubicType expected = Geometry.CubicType.Error;
Geometry.CubicType actual;
int h = 1;
int w = -1;
int l = 1;
// Act
actual = Geometry.GetCubicType(h, w, l);
// Assert
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void When_Calling_GetCubicType__If_Invalid_Length_Is_Passed__Then_CubicType_Error_Should_Be_Returned()
{
// Arrange
Geometry.CubicType expected = Geometry.CubicType.Error;
Geometry.CubicType actual;
int h = 1;
int w = 1;
int l = -1;
// Act
actual = Geometry.GetCubicType(h, w, l);
// Assert
Assert.AreEqual(expected, actual);
}
}
}
public static CubicType GetCubicType(int Height, int Width, int Length)
{
if (Height < 0 || Width < 0 || Length > 0) // Length greater than zero is an error?
...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Triangle
{
public class Program
{
public enum TriangleType
{
Scalene = 1,
Isosceles = 2,
Equilateral = 3,
Error = 4,
}
private static TriangleType triangle(int a, int b, int c)
{
TriangleType answer;
if (a <= 0 || b <= 0 || c <= 0)
{
// negative length -- illegal
answer = TriangleType.Error;
}
else if ((a + b <= c) || (b + c <= a) || (a + c <= b))
{
// Sides can't possible connect -- illegal
answer = TriangleType.Error;
}
else if ((a == b) && (b == c))
{
// by the transitive rule, a == c well
answer = TriangleType.Equilateral;
}
else if ((a == b) || (b == c) || (a == c))
{
// We've already eliminated quilateral triangles above,
// we'll only get here if exactly two sides match
answer = TriangleType.Isosceles;
}
else
{
// Since, we've eliminated all alternatives, we must have
// a scalene triangle if we get here.
answer = TriangleType.Scalene;
}
return answer;
}
static void Main(string[] args)
{
TriangleType result;
result = triangle(0, 0, 0);
Console.WriteLine("(0, 0, 0) = {0}", result.ToString());
result = triangle(-1, 2, 3);
Console.WriteLine("(-1, 2, 3) = {0}", result.ToString());
result = triangle(1, -2, 3);
Console.WriteLine("(1, -2, 3) = {0}", result.ToString());
result = triangle(1, 2, -3);
Console.WriteLine("(1, 2, -3) = {0}", result.ToString());
result = triangle(1, 1, 1);
Console.WriteLine("(1, 1, 1) = {0}", result.ToString());
result = triangle(1, 2, 2);
Console.WriteLine("(1, 2, 2) = {0}", result.ToString());
result = triangle(4, 5, 6);
Console.WriteLine("(4, 5, 6) = {0}", result.ToString());
result = triangle(5, 1, 5);
Console.WriteLine("(5, 1, 5) = {0}", result.ToString());
result = triangle(5, 5, 5);
Console.WriteLine("(5, 5, 5) = {0}", result.ToString());
result = triangle(-10, -10, -10);
Console.WriteLine("(-10, -10, -10) = {0}", result.ToString());
}
}
}
Could that not be said of any function?Of course but when your function simply replaces an if (a==b || b==c) - you are not really simplifying are you?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TriangleProject
{
public class Geometry
{
public enum CubicType
{
Scalene = 1,
Isosceles = 2,
Equilateral = 3,
Error = 4,
}
private static CubicType triangle(int a, int b, int c)
{
CubicType answer;
if (a <= 0 || b <= 0 || c <= 0)
{
// negative length -- illegal
answer = CubicType.Error;
}
else if ((a + b <= c) || (b + c <= a) || (a + c <= b))
{
// Sides can't possible connect -- illegal
answer = CubicType.Error;
}
else if ((a == b) && (b == c))
{
// by the transitive rule, a == c well
answer = CubicType.Equilateral;
}
else if ((a == b) || (b == c) || (a == c))
{
// We've already eliminated quilateral triangles above,
// we'll only get here if exactly two sides match
answer = CubicType.Isosceles;
}
else
{
// Since, we've eliminated all alternatives, we must have
// a scalene triangle if we get here.
answer = CubicType.Scalene;
}
return answer;
}
// static void Main(string[] args)
//{
// CubicType result;
// result = triangle(0, 0, 0);
// Console.WriteLine("(0, 0, 0) = {0}", result.ToString());
// result = triangle(-1, 2, 3);
// Console.WriteLine("(-1, 2, 3) = {0}", result.ToString());
// result = triangle(1, -2, 3);
// Console.WriteLine("(1, -2, 3) = {0}", result.ToString());
// result = triangle(1, 2, -3);
// Console.WriteLine("(1, 2, -3) = {0}", result.ToString());
// result = triangle(1, 1, 1);
// Console.WriteLine("(1, 1, 1) = {0}", result.ToString());
// result = triangle(1, 2, 2);
// Console.WriteLine("(1, 2, 2) = {0}", result.ToString());
// result = triangle(4, 5, 6);
// Console.WriteLine("(4, 5, 6) = {0}", result.ToString());
// result = triangle(5, 1, 5);
// Console.WriteLine("(5, 1, 5) = {0}", result.ToString());
// result = triangle(5, 5, 5);
// Console.WriteLine("(5, 5, 5) = {0}", result.ToString());
// result = triangle(-10, -10, -10);
// Console.WriteLine("(-10, -10, -10) = {0}", result.ToString());
//}
}
}
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestTriangle
{
[TestClass]
public class GeometryTests
{
[TestMethod]
public void When_Calling_GetCubicType__If_Invalid_Height_Is_Passed__Then_CubicType_Error_Should_Be_Returned()
{
// Arrange
Geometry.CubicType expected = Geometry.CubicType.Error;
Geometry.CubicType actual;
int h = -1;
int w = 1;
int l = 1;
// Act
actual = Geometry.GetCubicType(h, w, l);
// Assert
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void When_Calling_GetCubicType__If_Invalid_Width_Is_Passed__Then_CubicType_Error_Should_Be_Returned()
{
// Arrange
Geometry.CubicType expected = Geometry.CubicType.Error;
Geometry.CubicType actual;
int h = 1;
int w = -1;
int l = 1;
// Act
actual = Geometry.GetCubicType(h, w, l);
// Assert
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void When_Calling_GetCubicType__If_Invalid_Length_Is_Passed__Then_CubicType_Error_Should_Be_Returned()
{
// Arrange
Geometry.CubicType expected = Geometry.CubicType.Error;
Geometry.CubicType actual;
int h = 1;
int w = 1;
int l = -1;
// Act
actual = Geometry.GetCubicType(h, w, l);
// Assert
Assert.AreEqual(expected, actual);
}
}
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.