namespace Class_With_Methods
{
/// <summary>
/// In addition to private fields, constructor, and public properties, a class may also defines methods.
/// Methods state the functions an object of the class can perform.
/// </summary>
public partial class Form1 : Form
{
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void btnCreateBoxes_Click(object sender, EventArgs e)
{
//get a random length, width, height;
double length = rand.Next(3, 7) + rand.NextDouble();
double width = rand.Next(1, 4) + rand.NextDouble();
double height = rand.Next(2, 6) + rand.NextDouble();
//create
Box box1 = new Box(length, width, height);
//---------------------------------------------
//get a random length, width, height;
length = rand.Next(3, 7) + rand.NextDouble();
width = rand.Next(1, 4) + rand.NextDouble();
height = rand.Next(2, 6) + rand.NextDouble();
//create
Box box2 = new Box(length, width, height);
//Save Box
//Remark for Lab// List<Box> boxList
//Display both boxes
DisplayBox(box1);
DisplayBox(box2);
}
//Define a method that display a Box object
private void DisplayBox(Box box)
{
richTextBox1.AppendText("length: " + box.Length.ToString("f2") + "\n" +
"Width: " + box.Width.ToString("f2") + "\n" + "Height: " + box.Height.ToString("f2") + "\n" +
"Volume: " + box.Volume().ToString("f2") + "\n" + "Area: " + box.Area().ToString("f2") + "\n\n");
}
}
//=========================================
public class Box
{
private double _length;
private double _width;
private double _height;
//constructor
public Box(double length, double width, double height)
{
_length = length;
_width = width;
_height = height;
}
//properties
public double Length
{ get { return _length; } }
public double Width
{ get { return _width; } }
public double Height
{ get { return _height; } }
//Methods
public double Volume()
{
return _length * _width * _height;
}
public double Area()
{
return 2 * (_length * _width) + 2 * (_length * _height) + 2 * (_height * _width);
}
}
}
///Lab:
///Define a class Cylinder
///define its private fields, constructor and properties
///define methods Volume and Area
///in Form1: Create a List of Cylinder
///Add button to create new cylinder and add to list
///Add label at the bottom corner to display the number of
///cylinders added to the list
///Add button to display all the cylinders in the list
{
/// <summary>
/// In addition to private fields, constructor, and public properties, a class may also defines methods.
/// Methods state the functions an object of the class can perform.
/// </summary>
public partial class Form1 : Form
{
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void btnCreateBoxes_Click(object sender, EventArgs e)
{
//get a random length, width, height;
double length = rand.Next(3, 7) + rand.NextDouble();
double width = rand.Next(1, 4) + rand.NextDouble();
double height = rand.Next(2, 6) + rand.NextDouble();
//create
Box box1 = new Box(length, width, height);
//---------------------------------------------
//get a random length, width, height;
length = rand.Next(3, 7) + rand.NextDouble();
width = rand.Next(1, 4) + rand.NextDouble();
height = rand.Next(2, 6) + rand.NextDouble();
//create
Box box2 = new Box(length, width, height);
//Save Box
//Remark for Lab// List<Box> boxList
//Display both boxes
DisplayBox(box1);
DisplayBox(box2);
}
//Define a method that display a Box object
private void DisplayBox(Box box)
{
richTextBox1.AppendText("length: " + box.Length.ToString("f2") + "\n" +
"Width: " + box.Width.ToString("f2") + "\n" + "Height: " + box.Height.ToString("f2") + "\n" +
"Volume: " + box.Volume().ToString("f2") + "\n" + "Area: " + box.Area().ToString("f2") + "\n\n");
}
}
//=========================================
public class Box
{
private double _length;
private double _width;
private double _height;
//constructor
public Box(double length, double width, double height)
{
_length = length;
_width = width;
_height = height;
}
//properties
public double Length
{ get { return _length; } }
public double Width
{ get { return _width; } }
public double Height
{ get { return _height; } }
//Methods
public double Volume()
{
return _length * _width * _height;
}
public double Area()
{
return 2 * (_length * _width) + 2 * (_length * _height) + 2 * (_height * _width);
}
}
}
///Lab:
///Define a class Cylinder
///define its private fields, constructor and properties
///define methods Volume and Area
///in Form1: Create a List of Cylinder
///Add button to create new cylinder and add to list
///Add label at the bottom corner to display the number of
///cylinders added to the list
///Add button to display all the cylinders in the list

No comments:
Post a Comment