using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeometryShapes
{
public class Cylinder
{
public double R { get; set; }
public double H { get; set; }
//methods
public double Volume()
{
return Math.PI * R * R * H;
}
}
}
//=======================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GeometryShapes; //Added to use Add Reference
namespace MyExtensions
{
public static class MyExtensionMethods
{
//this method has the same effect as Extending (inheriting from)
//the cylinder class
//and adding the Area method
public static double Area(this Cylinder cylinder)
{
double A = 2 * Math.PI * cylinder.R * cylinder.H;
return A;
}
///Add an extension method to compute the perimeter of the cylinder (2 pi r)
///apply it in this form.
public static double Perimeter(this Cylinder cylinder)
{
//return 2 * Math.PI * _radius;
double P = 2 * Math.PI * cylinder.R;
return P;
}
//Method to count character in a string
public static int CountCharOccurance(this String text, char ch)
{
int occurence = text.Count(x => x == ch);
return occurence;
}
//Method to get average exclude min and max
public static double AvgExcludeMinNMax(this List<int> list)
{
double avg = list.Average(x => x - list.Min() + list.Max());
return avg;
}
}
}
///An extension method for the .Net string class, the method should take a parameter
///of type character and returns the number of its occurences
///Add GUI to test this method (Enter a string, enter a character, call the extension method,
///Display result.
///
///An extension method for the .Net List class, the method is to return the average value of
///all the values excluding min and max values.
///Provide GUI to test it.
//=====================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GeometryShapes; //Added to use reference
using MyExtensions; //Added to use reference
namespace ExtensionMethods
{
public partial class Form1 : Form
{
List<int> intList = new List<int>();
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void btnVolume_Click(object sender, EventArgs e)
{
double r, h;
double.TryParse(txtR.Text, out r);
double.TryParse(txtH.Text, out h);
Cylinder cy = new Cylinder { R = r, H = h };
//Compute Volume
double V = cy.Volume();
//Display
richTextBox1.Text = String.Format("R = {0} H = {1} \nV = {2:f3}", r, h, V);
}
//Compute area of a cylinder
//To read more... http://csharp.net-tutorials.com/csharp-3.0/extension-methods/
//To read more... http://msdn.microsoft.com/en-us/library/bb383977.aspx
private void btnArea_Click(object sender, EventArgs e)
{
double r, h;
double.TryParse(txtR.Text, out r);
double.TryParse(txtH.Text, out h);
Cylinder cy = new Cylinder { R = r, H = h };
//compute area using the extended method Area
double A = cy.Area();
richTextBox1.Text = String.Format("R = {0} H = {1} \nV = {2:f3}", r, h, A);
}
private void btnPerimeter_Click(object sender, EventArgs e)
{
double r, h;
double.TryParse(txtR.Text, out r);
double.TryParse(txtH.Text, out h);
Cylinder cy = new Cylinder { R = r, H = h };
double p = cy.Perimeter();
richTextBox1.Text = String.Format("R = {0} H = {1} \nP = {2:f2}", r, h, p);
}
private void btnCount_Click(object sender, EventArgs e)
{
string mainstr = txtString.Text;
char ch = char.Parse(txtChar.Text);
richTextBox2.Text = mainstr.CountCharOccurance(ch).ToString();
}
private void btnGetAvgExcludeMinNMax_Click(object sender, EventArgs e)
{
double avg = intList.AvgExcludeMinNMax();
richTextBox3.Text = avg.ToString();
}
//display method to display any list
private void Display(List<int> list)
{
listBox1.Items.Clear();
foreach (int n in list)
listBox1.Items.Add(n);
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
intList.Add(rand.Next(1, 12));
}
Display(intList);
}
}
}
//=====================================================
//=====================================================
///Add an extension method to compute the perimeter of the cylinder (2 pi r)
///apply it in this form.
///
///Lab: 1/16/2015
///Add to this project:
//=====================================================
///An extension method for the .Net string class, the method should take a parameter
///of type character and returns the number of its occurences
///Add GUI to test this method (Enter a string, enter a character, call the extension method,
///Display result.
///
///An extension method for the .Net List class, the method is to return the average value of
///all the values excluding min and max values.
///Provide GUI to test it.


No comments:
Post a Comment