Created
July 10, 2019 00:35
-
-
Save egsy/cf53271b9adc98fab008f893dfa30ba6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/// <summary> | |
/// Introduces new class <c>Shape</c> | |
/// </summary> | |
class Shape | |
{ | |
/// <summary> | |
/// Initializes public method <c>Area</c> | |
/// </summary> | |
/// <returns>Returns area, throws exception with custom message</returns> | |
public virtual int Area() | |
{ | |
throw new NotImplementedException("Area() is not implemented"); | |
} | |
} | |
/// <summary> | |
/// New class <c>Rectangle</c> inheriting from <c>Shape</c> | |
/// </summary> | |
class Rectangle : Shape | |
{ | |
private int width; | |
private int height; | |
public int Width | |
{ | |
get { return width; } | |
set | |
{ | |
if (value < 0) { throw new ArgumentException("Width must be greater than or equal to 0"); } | |
width = value; | |
} | |
} | |
public int Height | |
{ | |
get { return height; } | |
set | |
{ | |
if (value < 0) { throw new ArgumentException("Height must be greater than or equal to 0"); } | |
height = value; | |
} | |
} | |
/// <summary> | |
/// override of <c>Area()</c> method defined in base class | |
/// </summary> | |
/// <returns>Returns area of shape</returns> | |
public new int Area() | |
{ | |
return height * width; | |
} | |
/// <summary> | |
/// returns string output with rectangle dimensions | |
/// </summary> | |
public override string ToString() | |
{ | |
return ($"[Rectangle] {width} / {height}"); | |
} | |
} | |
class Square : Rectangle | |
{ | |
private int size; | |
public int Size | |
{ | |
get { return size; } | |
set | |
{ | |
if (size < 0) { throw new ArgumentException("Size must be greater than or equal to 0"); } | |
size = Height = Width = value; | |
} | |
} | |
/// <summary> | |
/// returns string output with square dimensions | |
/// </summary> | |
public override string ToString() | |
{ | |
return ($"[Square] {size} / {size}"); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Square aSquare = new Square(); | |
try | |
{ | |
aSquare.Width = 12; | |
aSquare.Height = 8; | |
Console.WriteLine("aSquare width: {0}", aSquare.Width); | |
Console.WriteLine("aSquare height: {0}", aSquare.Height); | |
Console.WriteLine("aSquare size: {0}", aSquare.Size); | |
Console.WriteLine("aSquare area: {0}", aSquare.Area()); | |
Console.WriteLine(aSquare.ToString()); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
} | |
} | |
} | |
// Output: | |
// | |
// aSquare width: 12 | |
// aSquare height: 8 | |
// aSquare size: 0 | |
// aSquare area: 96 | |
// [Square] 0 / 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment