static void Main(string[] args) { Rectangle r1 = new Rectangle(-1, -2); r1.SetLength(2.0); r1.SetWidth(-0.1); Rectangle r2 = new Rectangle(); r2.SetLength(3); r2.SetLength(-2); Rectangle r3 = new Rectangle(); r3.SetLength(0); r3.SetWidth(0); }
public void SetLength_Input11_Returns11() { //Arrange int length = 11; int width = 30; int expectedResult = length; Rectangle testRectangle = new Rectangle(length, width); //Act double actualResult = testRectangle.SetLength(length); //Assert Assert.AreEqual(expectedResult, actualResult); }
static void Main(string[] args) { int l = 0; int w = 0; int option; int count = 0; { Console.WriteLine("***Rectangle calculations***"); do { count++; if (count != 0) { Console.WriteLine("Please enter values greater than 0"); } Console.WriteLine("\nPlease enter length\t "); l = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("\nPlease enter width\t "); w = Convert.ToInt32(Console.ReadLine()); }while (l <= 0 || w <= 0); do { Console.WriteLine("\nChoose an option from the Menu\n 1. Get Rectangle Length \n2.Change Rectangle Length \n3.Get Rectangle Width \n4.Change Rectangle Width \n5.Get Rectangle Perimeter \n6.Get Rectangle Area \n7.Exit \n "); option = Convert.ToInt32(Console.ReadLine()); Rectangle rc = new Rectangle(l, w); if (option == 1) { Console.WriteLine("Rectangle length is "); Console.WriteLine(rc.GetLength()); } else if (option == 2) { do { Console.WriteLine("Enter a new length greater than 0"); l = Convert.ToInt32(Console.ReadLine()); } while (l < 1); rc.SetLength(l); Console.WriteLine("New rectangle length is "); Console.WriteLine(rc.GetLength()); } else if (option == 3) { Console.WriteLine("Rectangle width is "); Console.WriteLine(rc.GetWidth()); } else if (option == 4) { do { Console.WriteLine("Enter a new width greater than 0"); w = Convert.ToInt32(Console.ReadLine()); } while (w < 1); rc.SetWidth(w); Console.WriteLine("New rectangle width is "); Console.WriteLine(rc.GetWidth()); } else if (option == 5) { Console.WriteLine("Rectangle perimeter is "); Console.WriteLine(rc.GetPerimeter()); } else if (option == 6) { Console.WriteLine("Rectangle Area is "); Console.WriteLine(rc.GetArea()); } } while (option != 7); } }
static void Main(string[] args) { Rectangle c = new Rectangle(); bool validCalcSelect = false; string calcSelection; int selection; while (validCalcSelect == false) { Console.WriteLine("1 = Use default values of length=1 and width=1\n"); Console.WriteLine("2 = Provide your own length and width\n"); Console.WriteLine("Choose a menu item to begin:"); calcSelection = Console.ReadLine(); Console.WriteLine(); if (calcSelection != "1" && calcSelection != "2") { Console.WriteLine("That's not a valid selection, please try again.\n"); } else if (int.Parse(calcSelection) == 1) { validCalcSelect = true; Random random = new Random(); int length = 1; int width = 1; Console.WriteLine($"Your default length is {length} and width is {width}.\n"); Rectangle customCalc = new Rectangle(length, width); c = customCalc; } else if (int.Parse(calcSelection) == 2) { validCalcSelect = true; int length; int width; length = ValidateUserInput("length"); width = ValidateUserInput("width"); Console.WriteLine($"Your custom length is {length} and width is {width}.\n"); Rectangle customCalc = new Rectangle(length, width); c = customCalc; } } selection = ValidationMenuSelection(); while (selection != 7) { int result; switch (selection) { case 1: Console.WriteLine($"Length is: {c.GetLength()}\n"); break; case 2: result = ValidateUserInput("length"); c.SetLength(result); break; case 3: Console.WriteLine($"Width is: {c.GetWidth()}\n"); break; case 4: result = ValidateUserInput("width"); c.SetWidth(result); break; case 5: Console.WriteLine($"The Perimeter of {c.GetLength()} and {c.GetWidth()} is: {c.GetPerimeter()}\n"); break; case 6: Console.WriteLine($"The Area of {c.GetLength()} and {c.GetWidth()} is:{c.GetArea()}\n"); break; default: break; } selection = ValidationMenuSelection(); } }
static void Main(string[] args) { Rectangle rec = new Rectangle(); int l; int w; bool r = true; while (r) { try { Console.WriteLine("\n Welcome"); Console.WriteLine("Select one option from list"); Console.WriteLine("1. Get Rectangle Length"); Console.WriteLine("2. Change Rectangle Length"); Console.WriteLine("3. Get Rectangle Width"); Console.WriteLine("4. Change Rectangle Width"); Console.WriteLine("5. Get Rectangle Perimeter"); Console.WriteLine("6. Get Rectangle Area"); Console.WriteLine("7. Exit \n"); int input = int.Parse(Console.ReadLine()); switch (input) { case 1: Console.WriteLine(rec.GetLength()); break; case 2: try { Console.WriteLine("1. Enter the Length of Rectangle"); l = int.Parse(Console.ReadLine()); if (l <= 0) { Console.WriteLine("Wrong input"); } rec.SetLength(l); } catch (Exception e) { Console.WriteLine("Wrong Input"); } break; case 3: Console.WriteLine(rec.GetWidth()); break; case 4: try { Console.WriteLine("2. Enter Rectangle's Width"); w = int.Parse(Console.ReadLine()); if (w <= 0) { Console.WriteLine("Enter Valid Value"); } rec.SetWidth(w); } catch (Exception e) { Console.WriteLine("Wrong Input"); } break; case 5: Console.WriteLine("Perimeter " + rec.GetPerimeter()); break; case 6: Console.WriteLine("Area " + rec.GetArea()); break; case 7: Console.WriteLine("Please Visit Again "); Environment.Exit(0); r = false; break; } } catch (Exception e) { Console.WriteLine("Wrong Input"); }; } }
static void Main(string[] args) { int inputLength; int inputWidth; Rectangle rectangle; Console.WriteLine("***********Welcome to Rectangle Program************"); string lengthInput = null; string widthInput = null; int updatedLength; int updatedWidth; int userMenuSelection; try { do { Console.Clear(); Console.WriteLine("Please enter the Length"); lengthInput = Console.ReadLine(); Console.WriteLine("Please enter the Width"); widthInput = Console.ReadLine(); } while (!(int.TryParse(lengthInput, out inputLength) && int.TryParse(widthInput, out inputWidth) && inputWidth > 1 && inputLength > 0)); rectangle = new Rectangle(inputLength, inputWidth); do { Console.WriteLine("\n\nPlease select the number choice given for the below menu options\n\n1. GetLength\n2. SetLength\n3. GetWidth\n4. SetWidth\n5. GetPerimeter\n6. GetArea\n7. Exit"); bool userSelection = int.TryParse(Console.ReadLine(), out userMenuSelection); if (userSelection) { Console.Clear(); switch (userMenuSelection) { case 1: Console.WriteLine($"Rectangle's length is {rectangle.GetLength()}"); break; case 2: Console.WriteLine("Please enter the Length of the rectangle"); int.TryParse(Console.ReadLine(), out updatedLength); if (updatedLength > 0) { Console.WriteLine($"You have updated the length and rectangle's new length is {rectangle.SetLength(updatedLength)}"); break; } else { Console.WriteLine("Please enter positive length"); break; } case 3: Console.WriteLine($"Rectangle's width is {rectangle.GetWidth()}"); break; case 4: Console.WriteLine("Please enter the Width of the rectangle"); int.TryParse(Console.ReadLine(), out updatedWidth); if (updatedWidth > 0) { Console.WriteLine($"You have updated the width and rectangle's new width is {rectangle.SetWidth(updatedWidth)}"); break; } else { Console.WriteLine("Please enter positive width"); break; } case 5: Console.WriteLine($"Rectangle's perimeter is {rectangle.GetPerimeter()}"); break; case 6: Console.WriteLine($"Area of the rectangle is {rectangle.GetArea()}"); break; case 7: System.Environment.Exit(0); break; default: Console.WriteLine("Please enter the right input from the menu"); break; } } else { Console.Clear(); Console.WriteLine("Please enter only numerics"); } } while (!(userMenuSelection == 7)); } catch (Exception) { Console.WriteLine("Some error occured while entering and processing the rectangle details. Please close the application and try again."); } Console.ReadKey(); }