axeefectushka

Untitled

Oct 31st, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         Figure[] figures = new Figure[2];
  9.         figures[0] = new Rectangle(2, 3, new Color(2,3,4));
  10.         figures[1] = new Circle(100, new Color(255,0,0));
  11.  
  12.         foreach(Figure item in figures)
  13.         {
  14.             Console.WriteLine(item.ToString() + $" and area: {item.Area()}");
  15.         }
  16.  
  17.     }
  18. }
  19.  
  20. abstract class Figure
  21. {
  22.     public Color _color { get; set; }
  23.     public abstract double Area();
  24. }
  25.  
  26. class Rectangle : Figure
  27. {
  28.     private int _height;
  29.     private int _width;
  30.  
  31.     public int Height
  32.     {
  33.         get => _height;
  34.         set => _height = value >= 0 ? value : 0;
  35.     }
  36.  
  37.     public int Width
  38.     {
  39.         get => _width;
  40.         set => _width = value >= 0 ? value : 0;
  41.     }
  42.  
  43.     public override double Area()
  44.     {
  45.         return Height * Width;
  46.     }
  47.  
  48.     public Rectangle(int height, int width, Color color)
  49.     {
  50.         Height = height;
  51.         Width = width;
  52.         _color = color;
  53.     }
  54.  
  55.     public override string ToString()
  56.     {
  57.         return $"Rectangle: height = {Height}, width = {Width}, color: {_color.ToString()}";    
  58.     }
  59. }
  60.  
  61. class Circle : Figure
  62. {
  63.     private int _radius;
  64.     public int Radius
  65.     {
  66.         get => _radius;
  67.         set => _radius = value >= 0 ? value : 0;
  68.     }
  69.  
  70.     public override double Area()
  71.     {
  72.         return 3.1415 * Radius * Radius;
  73.     }
  74.  
  75.     public override string ToString()
  76.     {
  77.         return $"Circle: radius = {Radius}, color: {_color.ToString()}";
  78.     }
  79.  
  80.     public Circle(int radius, Color color)
  81.     {
  82.         Radius = radius;
  83.         _color = color;
  84.     }
  85. }
Add Comment
Please, Sign In to add comment