Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Reflection;
- namespace Encapsulation
- {
- public class Box
- {
- private double lenght;
- private double width;
- private double height;
- public Box(double lenght, double width, double height)
- {
- this.Lenght = lenght;
- this.Width = width;
- this.Height = height;
- }
- public double Lenght
- {
- get { return this.lenght; }
- private set
- {
- if (value <= 0)
- {
- throw new ArgumentException($"Length cannot be zero or negative.");
- }
- this.lenght = value;
- }
- }
- public double Width
- {
- get { return this.width; }
- private set
- {
- if (value <= 0)
- {
- throw new ArgumentException($"Width cannot be zero or negative.");
- }
- this.width = value;
- }
- }
- public double Height
- {
- get { return this.height; }
- private set
- {
- if (value <= 0)
- {
- throw new ArgumentException($"Height cannot be zero or negative.");
- }
- this.height = value;
- }
- }
- public double Volume()
- {
- var volume = this.Width * this.Lenght * this.Height;
- return volume;
- }
- public double SurfaceArea()
- {
- //2lw + 2lh + 2wh
- var surfaceArea = (2 * this.Lenght * this.Width) + (2 * this.Lenght * this.Height) + (2 * this.Width * this.Height);
- return surfaceArea;
- }
- public double LateralSurfaceArea()
- {
- var lateralSurfaceArea = (2 * this.Lenght * this.Height) + (2 * this.Width * this.Height);
- return lateralSurfaceArea;
- }
- }
- public class Program
- {
- public static void Main(string[] args)
- {
- Type boxType = typeof(Box);
- FieldInfo[] fields = boxType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
- Console.WriteLine(fields.Count());
- double a = double.Parse(Console.ReadLine());
- double b = double.Parse(Console.ReadLine());
- double c = double.Parse(Console.ReadLine());
- try
- {
- Box box = new Box(a, b, c);
- Console.WriteLine($"Surface Area - {box.SurfaceArea():F2}");
- Console.WriteLine($"Lateral Surface Area - {box.LateralSurfaceArea():F2}");
- Console.WriteLine($"Volume - {box.Volume():F2}");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement