Абстрактный класс Figure - C#

Узнай цену своей работы

Формулировка задачи:

Листинг программы
  1. /*
  2. 1.Создать абстрактный класс Figure с методами вычисления площади и периметра, а также методом, выводящим информацию о фигуре на экран.
  3. 2.Создать производные классы: Rectangle (прямоугольник), Circle (круг), Triangle (треугольник) со своими методами вычисления площади и периметра.
  4. 3.Создать массив n фигур и вывести полную информацию о фигурах на экран.
  5. */
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using Microsoft.Win32;
  14. namespace prakt_10
  15. {
  16. abstract public class Figure
  17. {
  18. abstract public double Area(); // get area
  19. abstract public double Perimeter(); //get perimetr
  20. abstract public void Show(); //show information
  21. }
  22. class Trangle : Figure
  23. {
  24. private readonly string name = "Trangle";
  25. private int x1, y1;
  26. private int x2, y2;
  27. private int x3, y3;
  28. public Trangle(int x1, int y1, int x2, int y2, int x3, int y3)
  29. {
  30. this.x1 = x1;
  31. this.y1 = y1;
  32. this.x2 = x2;
  33. this.y2 = y2;
  34. this.x3 = x3;
  35. this.y3 = y3;
  36. }
  37. public int lengthSegment(int x1, int y1, int x2, int y2)
  38. {
  39. return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  40. }
  41. public override double Area()
  42. {
  43. int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
  44. int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  45. int segment3 = lengthSegment(this.x1, this.x3, this.y1, this.y3);
  46. double pPerimetr = segment3 + segment1 + segment2;
  47. return pPerimetr * Math.Sqrt((pPerimetr - segment1) * (pPerimetr - segment2) * (pPerimetr - segment3));
  48. }
  49. public override double Perimeter()
  50. {
  51. int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
  52. int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  53. int segment3 = lengthSegment(this.x1, this.x3, this.y1, this.y3);
  54. return segment2 + segment1 + segment3;
  55. }
  56. public override void Show()
  57. {
  58. Console.WriteLine("Name figure:{0}", this.name);
  59. Console.WriteLine("Point coordinates:");
  60. Console.WriteLine("Point 1: ({0};{1})", this.x1, this.y1);
  61. Console.WriteLine("Point 2: ({0};{1})", this.x2, this.y2);
  62. Console.WriteLine("Point 3: ({0};{1})", this.x3, this.y3);
  63. Console.WriteLine("Length of a segment: {0}, {1}, {2}, area: {3}, perimetr: {4}",
  64. this.lengthSegment(this.x2, this.x1, this.y1, this.y2),
  65. this.lengthSegment(this.x3, this.x2, this.y3, this.y2),
  66. this.lengthSegment(this.x1, this.x3, this.y1, this.y3),
  67. this.Area(), this.Perimeter());
  68. }
  69. }
  70. class Rectangle : Figure
  71. {
  72. private readonly string name = "Rectangle";
  73. private int x1, y1;
  74. private int x2, y2;
  75. private int x3, y3;
  76. private int x4, y4;
  77. public Rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  78. {
  79. this.x1 = x1;
  80. this.y1 = y1;
  81. this.x2 = x2;
  82. this.y2 = y2;
  83. this.x3 = x3;
  84. this.y3 = y3;
  85. this.x4 = x4;
  86. this.y4 = y4;
  87. }
  88. public int lengthSegment(int x1, int y1, int x2, int y2)
  89. {
  90. return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  91. }
  92. public override double Area()
  93. {
  94. int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
  95. int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  96. return (segment2 * segment1);
  97. }
  98. public override double Perimeter()
  99. {
  100. int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
  101. int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
  102. return ((segment2 + segment1) * 2);
  103. }
  104. public override void Show()
  105. {
  106. Console.WriteLine("Name figure:{0}", this.name);
  107. Console.WriteLine("Point coordinates:");
  108. Console.WriteLine("Point 1: ({0};{1})", this.x1, this.y1);
  109. Console.WriteLine("Point 2: ({0};{1})", this.x2, this.y2);
  110. Console.WriteLine("Point 3: ({0};{1})", this.x3, this.y3);
  111. Console.WriteLine("Point 4: ({0};{1})", this.x4, this.y4);
  112. Console.WriteLine("Length of a segment: length {0}, width: {1}, area: {2}, perimetr: {3}",
  113. this.lengthSegment(this.x2, this.x1, this.y1, this.y2),
  114. this.lengthSegment(this.x3, this.x2, this.y3, this.y2),
  115. this.Area(), this.Perimeter());
  116. }
  117. }
  118. class Circle : Figure
  119. {
  120. private readonly string name = "Circle";
  121. private int x, y;
  122. private double radius;
  123. public Circle(int x, int y, int radius)
  124. {
  125. this.x = x;
  126. this.y = y;
  127. this.radius = radius;
  128. }
  129. public override double Area()
  130. {
  131. return Math.PI * this.radius * this.radius;
  132. }
  133. public override double Perimeter()
  134. {
  135. return Math.PI * this.radius * 2;
  136. }
  137. public override void Show()
  138. {
  139. Console.WriteLine("Name figure:{0}", this.name);
  140. Console.WriteLine("Point coordinates:");
  141. Console.WriteLine("Point: ({0};{1})", this.x, this.y);
  142. Console.WriteLine("Area: {0}, perimetr: {1}", this.Area(), this.Perimeter());
  143. }
  144. }
  145. class Programm
  146. {
  147. static void Main(string[] args)
  148. {
  149. int x1 = int.Parse(Console.ReadLine());
  150. int y1 = int.Parse(Console.ReadLine());
  151. int x2 = int.Parse(Console.ReadLine());
  152. int y2 = int.Parse(Console.ReadLine());
  153. int x3 = int.Parse(Console.ReadLine());
  154. int y3 = int.Parse(Console.ReadLine());
  155. int x4 = int.Parse(Console.ReadLine());
  156. int y4 = int.Parse(Console.ReadLine());
  157. Rectangle R1 = new Rectangle(x1, y1, x2, y2, x3, y3, x4, y4);
  158. R1.Show();
  159. Console.ReadKey();
  160. }
  161. }
  162. }
ломаю голову уже битые сутки, ничего более живого не могу придумать, помогите плиз

Решение задачи: «Абстрактный класс Figure»

textual
Листинг программы
  1. //Так как нужно вычислять Периметр, входные данные Для треугольника являются его сторонами
  2. using System;
  3. namespace ConsoleApplication1
  4. {
  5.     public abstract class Figure
  6.     {
  7.  
  8.         double pri_width;
  9.         double pri_height;
  10.         double pri_length;
  11.         public double Width
  12.         {
  13.             get { return pri_width; }
  14.             set { pri_width = value < 0 ? -value : value; }
  15.         }
  16.         public double Height
  17.         {
  18.             get { return pri_height; }
  19.             set { pri_height = value < 0 ? -value : value; }
  20.         }
  21.  
  22.         public double Length
  23.         {
  24.             get { return pri_length; }
  25.             set { pri_length = value < 0 ? -value : value; }
  26.         }
  27.         public string name { get; set; }
  28.  
  29.         public Figure() //Конструктор по умолчанию
  30.         {
  31.             Width = Height = Length = 0.0;
  32.             name = "null";
  33.         }
  34.  
  35.         public Figure(double x, string n) //Конструктор для круга, квадрата и равностороннего треугольника
  36.         {
  37.             Width = Height = Length = x;
  38.             name = n;
  39.         }
  40.  
  41.         public Figure(double x, double y, string n) // Равнобедренный треугольник и прямоугольник
  42.         {
  43.             Width = x;
  44.             Height = y;
  45.             Length = Width;
  46.             name = n;
  47.         }
  48.  
  49.         public Figure(double x, double y, double z, string n) // треугольник
  50.         {
  51.             Width = x;
  52.             Height = y;
  53.             Length = z;
  54.             name = n;
  55.         }
  56.  
  57.         public abstract double Area();
  58.         public abstract double Perimeter();
  59.         public abstract string ShowStyle();
  60.  
  61.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 4.071 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы