Advertisement
OLLI_BS

Abstract class

Feb 6th, 2017
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.73 KB | None | 0 0
  1. /*
  2. 1.Создать абстрактный класс Figure с методами вычисления площади и периметра, а также методом, выводящим информацию о фигуре на экран.  
  3. 2.Создать производные классы: Rectangle (прямоугольник), Circle (круг), Triangle (треугольник) со своими методами вычисления площади и периметра.  
  4. 3.Создать массив n фигур и вывести полную информацию о фигурах на экран.  
  5. */
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Runtime.CompilerServices;
  11. using System.Runtime.InteropServices;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using Microsoft.Win32;
  15.  
  16. namespace prakt_10
  17. {
  18.     abstract public class Figure
  19.     {
  20.         abstract public double Area();    // get area
  21.         abstract public double Perimeter();    //get perimetr
  22.         abstract public void Show();    //show information
  23.     }
  24.  
  25.     class Trangle : Figure
  26.     {
  27.         private readonly string name = "Trangle";
  28.  
  29.         private int x1, y1;
  30.         private int x2, y2;
  31.         private int x3, y3;
  32.  
  33.         public Trangle(int x1, int y1, int x2, int y2, int x3, int y3)
  34.         {
  35.             this.x1 = x1;
  36.             this.y1 = y1;
  37.  
  38.             this.x2 = x2;
  39.             this.y2 = y2;
  40.  
  41.             this.x3 = x3;
  42.             this.y3 = y3;
  43.         }
  44.  
  45.         public int lengthSegment(int x1, int y1, int x2, int y2)
  46.         {
  47.             return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  48.         }
  49.  
  50.         public override double Area()
  51.         {
  52.             int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
  53.             int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  54.             int segment3 = lengthSegment(this.x1, this.x3, this.y1, this.y3);
  55.  
  56.             double pPerimetr = segment3 + segment1 + segment2;
  57.  
  58.             return pPerimetr * Math.Sqrt((pPerimetr - segment1) * (pPerimetr - segment2) * (pPerimetr - segment3));
  59.         }
  60.  
  61.         public override double Perimeter()
  62.         {
  63.             int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
  64.             int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  65.             int segment3 = lengthSegment(this.x1, this.x3, this.y1, this.y3);
  66.  
  67.             return segment2 + segment1 + segment3;
  68.         }
  69.  
  70.         public override void Show()
  71.         {
  72.             Console.WriteLine("Name figure:{0}", this.name);
  73.  
  74.             Console.WriteLine("Point coordinates:");
  75.             Console.WriteLine("Point 1: ({0};{1})", this.x1, this.y1);
  76.             Console.WriteLine("Point 2: ({0};{1})", this.x2, this.y2);
  77.             Console.WriteLine("Point 3: ({0};{1})", this.x3, this.y3);
  78.  
  79.             Console.WriteLine("Length of a segment: {0}, {1}, {2}, area: {3}, perimetr: {4}",
  80.                 this.lengthSegment(this.x2, this.x1, this.y1, this.y2),
  81.                 this.lengthSegment(this.x3, this.x2, this.y3, this.y2),
  82.                 this.lengthSegment(this.x1, this.x3, this.y1, this.y3),
  83.                 this.Area(), this.Perimeter());
  84.         }
  85.     }
  86.  
  87.     class Rectangle : Figure
  88.     {
  89.         private readonly string name = "Rectangle";
  90.  
  91.         private int x1, y1;
  92.         private int x2, y2;
  93.         private int x3, y3;
  94.         private int x4, y4;
  95.  
  96.         public Rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  97.         {
  98.             this.x1 = x1;
  99.             this.y1 = y1;
  100.  
  101.             this.x2 = x2;
  102.             this.y2 = y2;
  103.  
  104.             this.x3 = x3;
  105.             this.y3 = y3;
  106.  
  107.             this.x4 = x4;
  108.             this.y4 = y4;
  109.         }
  110.  
  111.         public int lengthSegment(int x1, int y1, int x2, int y2)
  112.         {
  113.             return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  114.         }
  115.  
  116.         public override double Area()
  117.         {
  118.             int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
  119.             int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  120.             return (segment2 * segment1);
  121.         }
  122.  
  123.         public override double Perimeter()
  124.         {
  125.             int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
  126.             int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  127.             return ((segment2 + segment1) * 2);
  128.         }
  129.  
  130.         public override void Show()
  131.         {
  132.             Console.WriteLine("Name figure:{0}", this.name);
  133.  
  134.             Console.WriteLine("Point coordinates:");
  135.             Console.WriteLine("Point 1: ({0};{1})", this.x1, this.y1);
  136.             Console.WriteLine("Point 2: ({0};{1})", this.x2, this.y2);
  137.             Console.WriteLine("Point 3: ({0};{1})", this.x3, this.y3);
  138.             Console.WriteLine("Point 4: ({0};{1})", this.x4, this.y4);
  139.  
  140.             Console.WriteLine("Length of a segment: length {0}, width: {1}, area: {2}, perimetr: {3}",
  141.                 this.lengthSegment(this.x2, this.x1, this.y1, this.y2),
  142.                 this.lengthSegment(this.x3, this.x2, this.y3, this.y2),
  143.                 this.Area(), this.Perimeter());
  144.         }
  145.     }
  146.  
  147.     class Circle : Figure
  148.     {
  149.         private readonly string name = "Circle";
  150.  
  151.         private int x, y;
  152.         private double radius;
  153.  
  154.         public Circle(int x, int y, int radius)
  155.         {
  156.             this.x = x;
  157.             this.y = y;
  158.             this.radius = radius;
  159.         }
  160.  
  161.         public override double Area()
  162.         {
  163.             return Math.PI * this.radius * this.radius;
  164.         }
  165.  
  166.         public override double Perimeter()
  167.         {
  168.             return Math.PI * this.radius * 2;
  169.         }
  170.  
  171.         public override void Show()
  172.         {
  173.             Console.WriteLine("Name figure:{0}", this.name);
  174.  
  175.             Console.WriteLine("Point coordinates:");
  176.             Console.WriteLine("Point: ({0};{1})", this.x, this.y);
  177.  
  178.             Console.WriteLine("Area: {0}, perimetr: {1}", this.Area(), this.Perimeter());
  179.         }
  180.     }
  181.    
  182.     class Programm
  183.     {
  184.         static void Main(string[] args)
  185.         {
  186.             int x1 = int.Parse(Console.ReadLine());
  187.             int y1 = int.Parse(Console.ReadLine());
  188.  
  189.             int x2 = int.Parse(Console.ReadLine());
  190.             int y2 = int.Parse(Console.ReadLine());
  191.  
  192.             int x3 = int.Parse(Console.ReadLine());
  193.             int y3 = int.Parse(Console.ReadLine());
  194.  
  195.             int x4 = int.Parse(Console.ReadLine());
  196.             int y4 = int.Parse(Console.ReadLine());
  197.  
  198.             Rectangle R1 = new Rectangle(x1, y1, x2, y2, x3, y3, x4, y4);
  199.             R1.Show();
  200.  
  201.             Console.ReadKey();
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement