Advertisement
silvi81

Box

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