Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4.  
  5. namespace Encapsulation
  6. {
  7.     public class Box
  8.     {
  9.         private double lenght;
  10.         private double width;
  11.         private double height;
  12.  
  13.  
  14.         public Box(double lenght, double width, double height)
  15.         {
  16.             this.Lenght = lenght;
  17.             this.Width = width;
  18.             this.Height = height;
  19.         }
  20.  
  21.  
  22.         public double Lenght
  23.         {
  24.             get { return this.lenght; }
  25.             private set
  26.             {
  27.                 if (value <= 0)
  28.                 {
  29.                     throw new ArgumentException($"Length cannot be zero or negative.");
  30.                 }
  31.  
  32.                 this.lenght = value;
  33.             }
  34.         }
  35.         public double Width
  36.         {
  37.             get { return this.width; }
  38.             private set
  39.             {
  40.                 if (value <= 0)
  41.                 {
  42.                     throw new ArgumentException($"Width cannot be zero or negative.");
  43.                 }
  44.                 this.width = value;
  45.             }
  46.         }
  47.         public double Height
  48.         {
  49.             get { return this.height; }
  50.             private set
  51.             {
  52.                 if (value <= 0)
  53.                 {
  54.                     throw new ArgumentException($"Height cannot be zero or negative.");
  55.                 }
  56.  
  57.                 this.height = value;
  58.             }
  59.         }
  60.  
  61.         public double Volume()
  62.         {
  63.             var volume = this.Width * this.Lenght * this.Height;
  64.             return volume;
  65.         }
  66.  
  67.         public double SurfaceArea()
  68.         {
  69.             //2lw + 2lh + 2wh
  70.             var surfaceArea = (2 * this.Lenght * this.Width) + (2 * this.Lenght * this.Height) + (2 * this.Width * this.Height);
  71.             return surfaceArea;
  72.         }
  73.  
  74.         public double LateralSurfaceArea()
  75.         {
  76.             var lateralSurfaceArea = (2 * this.Lenght * this.Height) + (2 * this.Width * this.Height);
  77.             return lateralSurfaceArea;
  78.         }
  79.     }
  80.  
  81.     public class Program
  82.     {
  83.         public static void Main(string[] args)
  84.         {
  85.             Type boxType = typeof(Box);
  86.             FieldInfo[] fields = boxType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
  87.             Console.WriteLine(fields.Count());
  88.  
  89.  
  90.             double a = double.Parse(Console.ReadLine());
  91.             double b = double.Parse(Console.ReadLine());
  92.             double c = double.Parse(Console.ReadLine());
  93.             try
  94.             {
  95.                 Box box = new Box(a, b, c);
  96.                 Console.WriteLine($"Surface Area - {box.SurfaceArea():F2}");
  97.                 Console.WriteLine($"Lateral Surface Area - {box.LateralSurfaceArea():F2}");
  98.                 Console.WriteLine($"Volume - {box.Volume():F2}");
  99.             }
  100.             catch (Exception ex)
  101.             {
  102.                 Console.WriteLine(ex.Message);
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement