Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. using System;
  2. /// <summary>
  3. /// Introduces new class <c>Shape</c>
  4. /// </summary>
  5. class Shape
  6. {
  7. /// <summary>
  8. /// Initializes public method <c>Area</c>
  9. /// </summary>
  10. /// <returns>Returns area, throws exception with custom message</returns>
  11. public virtual int Area()
  12. {
  13. throw new NotImplementedException("Area() is not implemented");
  14. }
  15. }
  16.  
  17. /// <summary>
  18. /// New class <c>Rectangle</c> inheriting from <c>Shape</c>
  19. /// </summary>
  20. class Rectangle : Shape
  21. {
  22. private int width;
  23. private int height;
  24.  
  25. public int Width
  26. {
  27. get { return width; }
  28. set
  29. {
  30. if (value < 0) { throw new ArgumentException("Width must be greater than or equal to 0"); }
  31. width = value;
  32. }
  33. }
  34.  
  35. public int Height
  36. {
  37. get { return height; }
  38. set
  39. {
  40. if (value < 0) { throw new ArgumentException("Height must be greater than or equal to 0"); }
  41. height = value;
  42. }
  43. }
  44.  
  45. /// <summary>
  46. /// override of <c>Area()</c> method defined in base class
  47. /// </summary>
  48. /// <returns>Returns area of shape</returns>
  49. public new int Area()
  50. {
  51. return height * width;
  52. }
  53.  
  54. /// <summary>
  55. /// returns string output with rectangle dimensions
  56. /// </summary>
  57. public override string ToString()
  58. {
  59. return ($"[Rectangle] {width} / {height}");
  60. }
  61. }
  62.  
  63. class Square : Rectangle
  64. {
  65. private int size;
  66.  
  67. public int Size
  68. {
  69. get { return size; }
  70. set
  71. {
  72. if (size < 0) { throw new ArgumentException("Size must be greater than or equal to 0"); }
  73. size = Height = Width = value;
  74. }
  75. }
  76.  
  77. /// <summary>
  78. /// returns string output with square dimensions
  79. /// </summary>
  80. public override string ToString()
  81. {
  82. return ($"[Square] {size} / {size}");
  83. }
  84. }
  85.  
  86. class Program
  87. {
  88.  static void Main(string[] args)
  89.  {
  90.  Square aSquare = new Square();
  91. try
  92.  {
  93.  aSquare.Width = 12;
  94.  aSquare.Height = 8;
  95. Console.WriteLine("aSquare width: {0}", aSquare.Width);
  96.  Console.WriteLine("aSquare height: {0}", aSquare.Height);
  97.  Console.WriteLine("aSquare size: {0}", aSquare.Size);
  98.  Console.WriteLine("aSquare area: {0}", aSquare.Area());
  99.  Console.WriteLine(aSquare.ToString());
  100.  }
  101.  catch (Exception e)
  102.  { 
  103.  Console.WriteLine(e);
  104.  }
  105.  }
  106. }
  107. // Output:
  108. //
  109. // aSquare width: 12
  110. // aSquare height: 8
  111. // aSquare size: 0
  112. // aSquare area: 96
  113. // [Square] 0 / 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement