Advertisement
OLLI_BS

CompareTo for abstract class

Feb 7th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.43 KB | None | 0 0
  1. /*
  2. 1.Создать абстрактный класс Figure с методами вычисления площади и периметра, а также методом, выводящим информацию о фигуре на экран.  
  3. 2.Создать производные классы: Rectangle (прямоугольник), Circle (круг), Triangle (треугольник) со своими методами вычисления площади и периметра.  
  4. 3.Реализовать метод CompareTo, для сравнения объектов по площади.  
  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. using System.IO;
  16.  
  17. namespace prakt_10
  18. {
  19.     abstract public class Figure : IComparable
  20.     {
  21.         abstract public double Area();    // get area
  22.         abstract public double Perimeter();    //get perimetr
  23.         abstract public void Show();    //show information
  24.         abstract public void ShowArea(); // return area only
  25.  
  26.         public int CompareTo(object obj)
  27.         {
  28.             Figure b = (Figure)obj;
  29.             if (this.Area() == b.Area())
  30.             {
  31.                 return 0;
  32.             }
  33.             else
  34.             {
  35.                 if (this.Area() > b.Area())
  36.                 {
  37.                     return 1;
  38.                 }
  39.                 else
  40.                 {
  41.                     return -1;
  42.                 }
  43.             }
  44.         }
  45.     }
  46.  
  47.     class Trangle : Figure
  48.     {
  49.         private readonly string name = "Trangle";
  50.  
  51.         private int x1, y1;
  52.         private int x2, y2;
  53.         private int x3, y3;
  54.  
  55.         public Trangle(int x1, int y1, int x2, int y2, int x3, int y3)
  56.         {
  57.             this.x1 = x1;
  58.             this.y1 = y1;
  59.  
  60.             this.x2 = x2;
  61.             this.y2 = y2;
  62.  
  63.             this.x3 = x3;
  64.             this.y3 = y3;
  65.         }
  66.  
  67.         public int lengthSegment(int x2, int x1, int y2, int y1)
  68.         {
  69.             return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  70.         }
  71.  
  72.         public override double Area()
  73.         {
  74.             int segment1 = lengthSegment(this.x2, this.x1, this.y2, this.y1);
  75.             int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  76.             int segment3 = lengthSegment(this.x1, this.x3, this.y1, this.y3);
  77.  
  78.             double pPerimetr = segment3 + segment1 + segment2;
  79.  
  80.             return pPerimetr * Math.Sqrt((pPerimetr - segment1) * (pPerimetr - segment2) * (pPerimetr - segment3));
  81.         }
  82.  
  83.         public override double Perimeter()
  84.         {
  85.             int segment1 = lengthSegment(this.x2, this.x1, this.y2, this.y1);
  86.             int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  87.             int segment3 = lengthSegment(this.x1, this.x3, this.y1, this.y3);
  88.  
  89.             return segment2 + segment1 + segment3;
  90.         }
  91.  
  92.         public override void Show()
  93.         {
  94.             Console.WriteLine("Name figure:{0}", this.name);
  95.  
  96.             Console.WriteLine("Point coordinates:");
  97.             Console.WriteLine("Point 1: ({0};{1})", this.x1, this.y1);
  98.             Console.WriteLine("Point 2: ({0};{1})", this.x2, this.y2);
  99.             Console.WriteLine("Point 3: ({0};{1})", this.x3, this.y3);
  100.  
  101.             Console.WriteLine("Length of a segment: {0}, {1}, {2}, area: {3}, perimetr: {4}",
  102.                 this.lengthSegment(this.x2, this.x1, this.y2, this.y1),
  103.                 this.lengthSegment(this.x3, this.x2, this.y3, this.y2),
  104.                 this.lengthSegment(this.x1, this.x3, this.y1, this.y3),
  105.                 this.Area(), this.Perimeter());
  106.             Console.WriteLine();
  107.         }
  108.  
  109.         public override void ShowArea()
  110.         {
  111.             Console.WriteLine("Area {0} = {1}",this.name, this.Area());
  112.         }
  113.     }
  114.  
  115.     class Rectangle : Figure
  116.     {
  117.         private readonly string name = "Rectangle";
  118.  
  119.         private int x1, y1;
  120.         private int x2, y2;
  121.         private int x3, y3;
  122.         private int x4, y4;
  123.  
  124.         public Rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  125.         {
  126.             this.x1 = x1;
  127.             this.y1 = y1;
  128.  
  129.             this.x2 = x2;
  130.             this.y2 = y2;
  131.  
  132.             this.x3 = x3;
  133.             this.y3 = y3;
  134.  
  135.             this.x4 = x4;
  136.             this.y4 = y4;
  137.         }
  138.  
  139.         public int lengthSegment(int x2, int x1, int y2, int y1)
  140.         {
  141.             return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  142.         }
  143.  
  144.         public override double Area()
  145.         {
  146.             int segment1 = lengthSegment(this.x2, this.x1, this.y2, this.y1);
  147.             int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  148.             return (segment2 * segment1);
  149.         }
  150.  
  151.         public override double Perimeter()
  152.         {
  153.             int segment1 = lengthSegment(this.x2, this.x1, this.y2, this.y1);
  154.             int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  155.             return ((segment2 + segment1) * 2);
  156.         }
  157.  
  158.         public override void Show()
  159.         {
  160.             Console.WriteLine("Name figure:{0}", this.name);
  161.  
  162.             Console.WriteLine("Point coordinates:");
  163.             Console.WriteLine("Point 1: ({0};{1})", this.x1, this.y1);
  164.             Console.WriteLine("Point 2: ({0};{1})", this.x2, this.y2);
  165.             Console.WriteLine("Point 3: ({0};{1})", this.x3, this.y3);
  166.             Console.WriteLine("Point 4: ({0};{1})", this.x4, this.y4);
  167.  
  168.             Console.WriteLine("Length of a segment: length {0}, width: {1}, area: {2}, perimetr: {3}",
  169.                 this.lengthSegment(this.x2, this.x1, this.y2, this.y1),
  170.                 this.lengthSegment(this.x3, this.x2, this.y3, this.y2),
  171.                 this.Area(), this.Perimeter());
  172.             Console.WriteLine();
  173.         }
  174.  
  175.         public override void ShowArea()
  176.         {
  177.             Console.WriteLine("Area {0} = {1}", this.name, this.Area());
  178.         }
  179.     }
  180.  
  181.     class Circle : Figure
  182.     {
  183.         private readonly string name = "Circle";
  184.  
  185.         private int x, y;
  186.         private double radius;
  187.  
  188.         public Circle(int x, int y, int radius)
  189.         {
  190.             this.x = x;
  191.             this.y = y;
  192.             this.radius = radius;
  193.         }
  194.  
  195.         public override double Area()
  196.         {
  197.             return Math.PI * this.radius * this.radius;
  198.         }
  199.  
  200.         public override double Perimeter()
  201.         {
  202.             return Math.PI * this.radius * 2;
  203.         }
  204.  
  205.         public override void Show()
  206.         {
  207.             Console.WriteLine("Name figure:{0}", this.name);
  208.  
  209.             Console.WriteLine("Point coordinates:");
  210.             Console.WriteLine("Point: ({0};{1})", this.x, this.y);
  211.  
  212.             Console.WriteLine("Area: {0}, perimetr: {1}", this.Area(), this.Perimeter());
  213.             Console.WriteLine();
  214.         }
  215.  
  216.         public override void ShowArea()
  217.         {
  218.             Console.WriteLine("Area {0} = {1}", this.name, this.Area());
  219.         }
  220.     }
  221.  
  222.  
  223.     class Programm
  224.     {
  225.         static void Main(string[] args)
  226.         {
  227.             Console.Write("Enter n: ");
  228.             int n = int.Parse(Console.ReadLine());
  229.  
  230.             Figure[] array = new Figure[n];
  231.             using (StreamReader file = new StreamReader("../../data.txt"))
  232.             {
  233.                 for (int i = 0; i < n; i++)
  234.                 {
  235.                     string[] line = file.ReadLine().Split(' ');
  236.                     switch (line[0])
  237.                     {
  238.                         case "Rectangle":
  239.                             array[i] = new Rectangle(
  240.                                 int.Parse(line[1]),
  241.                                 int.Parse(line[2]),
  242.                                 int.Parse(line[3]),
  243.                                 int.Parse(line[4]),
  244.                                 int.Parse(line[5]),
  245.                                 int.Parse(line[6]),
  246.                                 int.Parse(line[7]),
  247.                                 int.Parse(line[8]));
  248.                             break;
  249.  
  250.                         case "Trangle":
  251.                             array[i] = new Trangle(
  252.                                 int.Parse(line[1]),
  253.                                 int.Parse(line[2]),
  254.                                 int.Parse(line[3]),
  255.                                 int.Parse(line[4]),
  256.                                 int.Parse(line[5]),
  257.                                 int.Parse(line[6]));
  258.                             break;
  259.  
  260.                         case "Circle":
  261.                             array[i] = new Circle(
  262.                                 int.Parse(line[1]),
  263.                                 int.Parse(line[2]),
  264.                                 int.Parse(line[3]));
  265.                             break;
  266.  
  267.                         default:
  268.                             Console.WriteLine("Keywords not found.");
  269.                             break;
  270.                     }
  271.                 }
  272.  
  273.                 for (int i = 0; i < n; i++)
  274.                 {
  275.                     array[i].ShowArea();
  276.                 }
  277.  
  278.                 Console.WriteLine();
  279.                 Array.Sort(array);
  280.  
  281.                 for (int i = 0; i < n; i++)
  282.                 {
  283.                     array[i].ShowArea();
  284.                 }
  285.  
  286.                 Console.ReadKey();
  287.             }
  288.         }
  289.     }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement