Aliendreamer

box oop basic encapsulation

May 18th, 2018
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         try
  9.         {
  10.             double length = double.Parse(Console.ReadLine());
  11.             double width = double.Parse(Console.ReadLine());
  12.             double height = double.Parse(Console.ReadLine());
  13.  
  14.             Box box = new Box(length, width, height);
  15.  
  16.             Console.WriteLine($"Surface Area - {box.BoxSurfaceArea(box):f2}" +
  17.                               $"{Environment.NewLine}Lateral Surface Area - {box.BoxLateralSurface(box):f2}" +
  18.                               $"{Environment.NewLine}Volume - {box.BoxVolume(box):f2}");
  19.         }
  20.         catch (Exception ex)
  21.         {
  22.             Console.WriteLine(ex.Message);
  23.         }
  24.     }
  25. }
  26. public class Box
  27. {
  28.     private double _length;
  29.     private double _width;
  30.     private double _height;
  31.  
  32.  
  33.  
  34.  
  35.     private double Length
  36.     {
  37.         get => _length;
  38.         set
  39.         {
  40.             if (value <= 0)
  41.             {
  42.                 throw new ArgumentException($"{nameof(Length)} cannot be zero or negative.");
  43.             }
  44.  
  45.             this._length = value;
  46.  
  47.         }
  48.     }
  49.  
  50.  
  51.     private double Width
  52.     {
  53.         get => _width;
  54.         set
  55.         {
  56.             if (value <= 0)
  57.             {
  58.                 throw new ArgumentException($"{nameof(Width)} cannot be zero or negative.");
  59.             }
  60.  
  61.             this._width = value;
  62.         }
  63.     }
  64.     private double Height
  65.     {
  66.         get => _height;
  67.         set
  68.         {
  69.             if (value <= 0)
  70.             {
  71.                 throw new ArgumentException($"{nameof(Height)} cannot be zero or negative.");
  72.             }
  73.             this._height = value;
  74.         }
  75.     }
  76.  
  77.     //Volume = lwh
  78.     //Lateral Surface Area = 2lh + 2wh
  79.     //Surface Area = 2lw + 2lh + 2wh
  80.     public Box(double length, double width, double height)
  81.     {
  82.         Length = length;
  83.         Width = width;
  84.         Height = height;
  85.     }
  86.  
  87.     public double BoxVolume(Box box)
  88.     {
  89.         return box.Length * box.Width * box.Height;
  90.     }
  91.  
  92.     public double BoxLateralSurface(Box box)
  93.     {
  94.         return 2 * (box.Length * box.Height) + 2 * (box.Width * box.Height);
  95.     }
  96.     public double BoxSurfaceArea(Box box)
  97.     {
  98.         return 2 * (box.Length * box.Height) + 2 * (box.Width * box.Height) + 2 * (box.Length * box.Width);
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment