Вычислить площадь и периметр прямоугольника в иерархии класса Прямоугольник.(Код есть) - C#
Формулировка задачи:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
Tochka t = new Tochka(new Point (2,6));
Rectangle rect1 = new Rectangle(new Point(4, 4), new Point(5, 5), new Point(6, 6), new Point(7, 7));
IFigure ifig;
ifig = t;
double P_tr = ifig.P();
double S_tr = ifig.S();
foreach (var pt in ifig.Coordinates)
Console.WriteLine("X={0}, Y={1}", pt.X, pt.Y);
ifig = rect1;
double P_rect = ifig.P();
double S_rect = ifig.S();
foreach (var pt in ifig.Coordinates)
Console.WriteLine("X={0}, Y={1}", pt.X, pt.Y);
Console.WriteLine("{0}, {1}", P_tr, S_tr);
Console.WriteLine("{0}, {1}", P_rect, P_rect);
Console.Read();
}
}
//Класс фигура
class Figure
{
public int X
{
get;
private set;
}
public int Y
{
get;
private set;
}
public Figure(int x, int y)
{
X = x;
Y = y;
}
}
//Класс точка
class Tochka : Figure, IFigure
{
Point p1;
Point[] points;
public Tochka(Point pt1)
: base(pt1.X, pt1.Y)
{
p1 = pt1;
points = new Point[1];
points[0] = p1;
}
public double P()
{
//Тут код просчета периметра точки
return 1;
}
public double S()
{
//Тут код просчета площади точки
return 1;
}
public Point[] Coordinates
{
get
{
return points;
}
set
{
points = value;
}
}
}
//Класс прямоугольник
class Rectangle : Figure, IFigure
{
Point p1;
Point p2;
Point p3;
Point p4;
Point[] points;
public Rectangle(Point pt1, Point pt2, Point pt3, Point pt4)
: base(pt1.X, pt1.Y)
{
p1 = pt1;
p2 = pt2;
p3 = pt3;
p4 = pt4;
points = new Point[4];
points[0] = p1;
points[1] = p2;
points[2] = p3;
points[3] = p4;
}
public double P()
{
//Тут код просчета периметра прямоугольника p = 2 * (a + b); а как записать?
return 2;
}
public double S()
{
//Тут код просчета площади прямоугольника S = a * b как записать?
return 2;
}
public Point[] Coordinates
{
get
{
return points;
}
set
{
points = value;
}
}
}
//Интерфейс
interface IFigure
{
double P();
double S();
Point[] Coordinates { get; set; }
}
}Решение задачи: «Вычислить площадь и периметр прямоугольника в иерархии класса Прямоугольник.(Код есть)»
textual
Листинг программы
using System;
namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
Point p = new Point(2, 6);
Rectangle rect1 = new Rectangle(p, 8, 5);
IFigure ifig = rect1;
double perRec = ifig.Perimeter();
double squRec = ifig.Square();
foreach (var pt in rect1.GetPoints())
Console.WriteLine("X={0}, Y={1}", pt.X, pt.Y);
Console.WriteLine("{0}, {1}", perRec, squRec);
Console.Read();
}
}
//Интерфейс фигуры, эта фигура должна иметь периметр, площадь, и находиться в определенном месте
interface IFigure
{
double Perimeter(); //периметр
double Square(); //площадь
Point Location { get; set; } //местоположение
}
//Класс точка, у нее нет ни периметра, ни площади, на то она и точка.
class Point
{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
}
//Класс прямоугольник
class Rectangle : IFigure
{
public Point Location { get; set; } //Положение
public int Width { get; set; } //Ширина
public int Height { get; set; } //Высота
public Rectangle(Point location, int width, int height)
{
Width = width;
Height = height;
Location = location;
}
public double Perimeter()
{
return 2 * Height * Width;
}
public double Square()
{
return Height * Width;
}
public Point[] GetPoints()
{
Point[] pts = new Point[4];
pts[0] = new Point(Location.X, Location.Y);
pts[1] = new Point(Location.X + Width, Location.Y);
pts[2] = new Point(Location.X + Width, Location.Y - Height);
pts[3] = new Point(Location.X, Location.Y - Height);
return pts;
}
}
}