SHARE
TWEET

Untitled

a guest Feb 21st, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. namespace NeetaAssignment
  4. {
  5.     class FunctionCalculator
  6.     {
  7.         private double Lower;
  8.         private double Upper;
  9.         private int Steps;
  10.  
  11.         // Constructor Overriding
  12.  
  13.         public FunctionCalculator ()
  14.         {
  15.         }
  16.  
  17.         public FunctionCalculator (double LowerLimit, double UpperLimit, int steps)
  18.         {
  19.             this.Lower = LowerLimit;
  20.             this.Upper = UpperLimit;
  21.             this.Steps = steps;
  22.         }
  23.  
  24.         // Get and Set method for lower limit,upper limit and steps
  25.  
  26.         // get and return method for lower limit,upper limit,and steps
  27.         public double getLowerLimit ()
  28.         {
  29.             return this.Lower;
  30.         }
  31.  
  32.         public double getUpperLimit ()
  33.         {
  34.             return this.Upper;
  35.         }
  36.  
  37.         public double getsteps ()
  38.         {
  39.             return this.Steps;
  40.         }
  41.  
  42.         // Set method for lower limit,upper limit,and steps
  43.  
  44.         public void setLowerLimit (double a)
  45.         {
  46.             this.Lower = a;
  47.         }
  48.  
  49.         public void setUpperLimit (double b)
  50.         {
  51.             this.Upper = b;
  52.         }
  53.  
  54.         public void setsteps (int c)
  55.         {
  56.             this.Steps = c;
  57.         }
  58.  
  59.  
  60.         //Now write method for given function which will return value
  61.  
  62.         public virtual double Funct (double X)
  63.         {
  64.             double function;
  65.             function = 2 / (1 + 4 * X * X);
  66.             return function;
  67.         }
  68.        
  69.         public double GetStepsize ()
  70.         {
  71.             double Stepssize = ((Upper - Lower) / Steps);
  72.             return Stepssize;
  73.         }
  74.  
  75.         //one more method for integration as Simpson Calculation
  76.  
  77.         public double CalculateSimpson ()
  78.         {
  79.            
  80.             double Stepssize = GetStepsize ();
  81.             double Simpson = 0;
  82.             for (double i = Lower; i <= Upper; i = i + Stepssize) {
  83.                 double a = i;
  84.                 double b = i + Stepssize;
  85.                 Simpson = Simpson + ((b - a) / 6) * (Funct (a) + 4 * Funct ((a + b) / 2) + Funct (b));
  86.             }
  87.            
  88.             return Simpson;
  89.         }
  90.  
  91.         public double CalculateMidpointRule ()
  92.         {
  93.             double Stepssize = ((Upper - Lower) / Steps);
  94.             double MidPoint = 0;
  95.             for (double i = Lower; i <= Upper; i = i + Stepssize) {
  96.  
  97.                 double a = i;
  98.                 double b = i + Stepssize;
  99.                 MidPoint = MidPoint + ((b - a) * Funct ((a + b) / 2));
  100.             }
  101.             return MidPoint;
  102.  
  103.         }
  104.  
  105.         public double CalculateTrapeziumRule ()
  106.         {
  107.             double Stepssize = ((Upper - Lower) / Steps);
  108.             double Trapezium = 0;
  109.             for (double i = Lower; i <= Upper; i = i + Stepssize) {
  110.  
  111.                 double a = i;
  112.                 double b = i + Stepssize;
  113.                 Trapezium = Trapezium + ((b - a) / 2) * (Funct (a) + Funct (b));
  114.             }
  115.             return Trapezium;
  116.  
  117.         }
  118.  
  119.         public void GetResult ()
  120.         {
  121.             Console.WriteLine (" \n Lower Limit={0}\n upperlimit={1} \n Steps={2} \n ", Lower, Upper, Steps);
  122.         }
  123.  
  124.     }
  125.    
  126.     class Class2 : FunctionCalculator
  127.     {
  128.         public override double Funct (double X)
  129.         {
  130.             double function;
  131.             function = Math.Sin (X);
  132.             return function;
  133.         }
  134.     }
  135.    
  136.     class MainClass
  137.     {
  138.         public static void Main (string[] args)
  139.         {
  140.             FunctionCalculator c = new FunctionCalculator ();
  141.             Console.WriteLine (" Enter Lower Limit");
  142.             c.setlowerlimit (Convert.ToDouble (Console.ReadLine ()));
  143.             Console.WriteLine (" Enter Upper Limit");
  144.             c.setupperlimit (Convert.ToDouble (Console.ReadLine ()));
  145.             Console.WriteLine (" Enter Steps");
  146.             c.setsteps (Convert.ToDouble (Console.ReadLine ()));
  147.             double Simpson = c.CalculateSimpson ();
  148.             c.GetResult ();
  149.             Console.WriteLine ("Simpson=" + Simpson);
  150.            
  151.             /*
  152.             Class2 C1 = new Class2();
  153.             Console.WriteLine(" Enter Lower Limit");
  154.             C1.setlowerlimit(Convert.ToInt32(Console.ReadLine()));
  155.             Console.WriteLine(" Enter Upper Limit");
  156.             C1.setupperlimit(Convert.ToInt32(Console.ReadLine()));
  157.             Console.WriteLine(" Enter Steps");
  158.             C1.setsteps(Convert.ToInt32(Console.ReadLine()));
  159.             double Simpson1 = C1.CalculateSimpson();
  160.             C1.GetResult();
  161.             Console.WriteLine("Simpson 1=" + Simpson1);
  162.             */
  163.         }
  164.     }
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top