BloodMoonYTC

RectangleClass

Oct 19th, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TeglalapOsztalya
  4. {
  5.     class Rectangle
  6.     {
  7.         private float length = 1;
  8.         private float width = 1;
  9.  
  10.         public Rectangle(float length, float width)
  11.         {
  12.             Length = length;
  13.             Width = width;
  14.         }
  15.         public float Length
  16.         {
  17.             get { return this.length; }
  18.             set {
  19.                 if (value <= 0.0f)
  20.                     this.length = 1.0f;
  21.                 else
  22.                     this.length = value;
  23.                 }
  24.         }
  25.         public float Width
  26.         {
  27.             get { return this.width; }
  28.             set
  29.             {
  30.                 if (value <= 0.0f)
  31.                     this.width = 1.0f;
  32.                 else
  33.                     this.width = value;
  34.             }
  35.         }
  36.  
  37.         public float Perimeter
  38.         {
  39.             get
  40.             {
  41.                 return (Length + Width) * 2;
  42.             }
  43.         }
  44.  
  45.         public float Area
  46.         {
  47.             get
  48.             {
  49.                 return (Length * Width);
  50.             }
  51.         }
  52.  
  53.         public override string ToString()
  54.         {
  55.             return string.Format("Perimeter is {0:F2} and the area is {1:F2}", Perimeter, Area);
  56.         }
  57.     }
  58.     class Program
  59.     {
  60.         static void Main(string[] args)
  61.         {
  62.             float length, width;
  63.             Console.WriteLine("Write down the Length of the rectangle: ");
  64.             length = float.Parse(Console.ReadLine());
  65.             Console.WriteLine("Write down the Width of the rectangle: ");
  66.             width = float.Parse(Console.ReadLine());
  67.             Rectangle rect = new Rectangle(length, width);
  68.             Console.WriteLine(rect);
  69.             Console.ReadLine();
  70.         }
  71.     }
  72. }
  73.  
Add Comment
Please, Sign In to add comment