Абстрактный класс Figure - C#
Формулировка задачи:
/*
1.Создать абстрактный класс Figure с методами вычисления площади и периметра, а также методом, выводящим информацию о фигуре на экран.
2.Создать производные классы: Rectangle (прямоугольник), Circle (круг), Triangle (треугольник) со своими методами вычисления площади и периметра.
3.Создать массив n фигур и вывести полную информацию о фигурах на экран.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
namespace prakt_10
{
abstract public class Figure
{
abstract public double Area(); // get area
abstract public double Perimeter(); //get perimetr
abstract public void Show(); //show information
}
class Trangle : Figure
{
private readonly string name = "Trangle";
private int x1, y1;
private int x2, y2;
private int x3, y3;
public Trangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
}
public int lengthSegment(int x1, int y1, int x2, int y2)
{
return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
public override double Area()
{
int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
int segment3 = lengthSegment(this.x1, this.x3, this.y1, this.y3);
double pPerimetr = segment3 + segment1 + segment2;
return pPerimetr * Math.Sqrt((pPerimetr - segment1) * (pPerimetr - segment2) * (pPerimetr - segment3));
}
public override double Perimeter()
{
int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
int segment3 = lengthSegment(this.x1, this.x3, this.y1, this.y3);
return segment2 + segment1 + segment3;
}
public override void Show()
{
Console.WriteLine("Name figure:{0}", this.name);
Console.WriteLine("Point coordinates:");
Console.WriteLine("Point 1: ({0};{1})", this.x1, this.y1);
Console.WriteLine("Point 2: ({0};{1})", this.x2, this.y2);
Console.WriteLine("Point 3: ({0};{1})", this.x3, this.y3);
Console.WriteLine("Length of a segment: {0}, {1}, {2}, area: {3}, perimetr: {4}",
this.lengthSegment(this.x2, this.x1, this.y1, this.y2),
this.lengthSegment(this.x3, this.x2, this.y3, this.y2),
this.lengthSegment(this.x1, this.x3, this.y1, this.y3),
this.Area(), this.Perimeter());
}
}
class Rectangle : Figure
{
private readonly string name = "Rectangle";
private int x1, y1;
private int x2, y2;
private int x3, y3;
private int x4, y4;
public Rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
this.x4 = x4;
this.y4 = y4;
}
public int lengthSegment(int x1, int y1, int x2, int y2)
{
return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
public override double Area()
{
int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
return (segment2 * segment1);
}
public override double Perimeter()
{
int segment1 = lengthSegment(this.x2, this.x1, this.y1, this.y2);
int segment2 = lengthSegment(this.x3, this.x2, this.y3, this.y2);
return ((segment2 + segment1) * 2);
}
public override void Show()
{
Console.WriteLine("Name figure:{0}", this.name);
Console.WriteLine("Point coordinates:");
Console.WriteLine("Point 1: ({0};{1})", this.x1, this.y1);
Console.WriteLine("Point 2: ({0};{1})", this.x2, this.y2);
Console.WriteLine("Point 3: ({0};{1})", this.x3, this.y3);
Console.WriteLine("Point 4: ({0};{1})", this.x4, this.y4);
Console.WriteLine("Length of a segment: length {0}, width: {1}, area: {2}, perimetr: {3}",
this.lengthSegment(this.x2, this.x1, this.y1, this.y2),
this.lengthSegment(this.x3, this.x2, this.y3, this.y2),
this.Area(), this.Perimeter());
}
}
class Circle : Figure
{
private readonly string name = "Circle";
private int x, y;
private double radius;
public Circle(int x, int y, int radius)
{
this.x = x;
this.y = y;
this.radius = radius;
}
public override double Area()
{
return Math.PI * this.radius * this.radius;
}
public override double Perimeter()
{
return Math.PI * this.radius * 2;
}
public override void Show()
{
Console.WriteLine("Name figure:{0}", this.name);
Console.WriteLine("Point coordinates:");
Console.WriteLine("Point: ({0};{1})", this.x, this.y);
Console.WriteLine("Area: {0}, perimetr: {1}", this.Area(), this.Perimeter());
}
}
class Programm
{
static void Main(string[] args)
{
int x1 = int.Parse(Console.ReadLine());
int y1 = int.Parse(Console.ReadLine());
int x2 = int.Parse(Console.ReadLine());
int y2 = int.Parse(Console.ReadLine());
int x3 = int.Parse(Console.ReadLine());
int y3 = int.Parse(Console.ReadLine());
int x4 = int.Parse(Console.ReadLine());
int y4 = int.Parse(Console.ReadLine());
Rectangle R1 = new Rectangle(x1, y1, x2, y2, x3, y3, x4, y4);
R1.Show();
Console.ReadKey();
}
}
}Решение задачи: «Абстрактный класс Figure»
textual
Листинг программы
//Так как нужно вычислять Периметр, входные данные Для треугольника являются его сторонами
using System;
namespace ConsoleApplication1
{
public abstract class Figure
{
double pri_width;
double pri_height;
double pri_length;
public double Width
{
get { return pri_width; }
set { pri_width = value < 0 ? -value : value; }
}
public double Height
{
get { return pri_height; }
set { pri_height = value < 0 ? -value : value; }
}
public double Length
{
get { return pri_length; }
set { pri_length = value < 0 ? -value : value; }
}
public string name { get; set; }
public Figure() //Конструктор по умолчанию
{
Width = Height = Length = 0.0;
name = "null";
}
public Figure(double x, string n) //Конструктор для круга, квадрата и равностороннего треугольника
{
Width = Height = Length = x;
name = n;
}
public Figure(double x, double y, string n) // Равнобедренный треугольник и прямоугольник
{
Width = x;
Height = y;
Length = Width;
name = n;
}
public Figure(double x, double y, double z, string n) // треугольник
{
Width = x;
Height = y;
Length = z;
name = n;
}
public abstract double Area();
public abstract double Perimeter();
public abstract string ShowStyle();
}