Wednesday, October 22, 2014
Polymorphism with Rectangle Shapes_Lab
namespace Zau_Polymorphic_Rect_Lab
{
[Serializable]
public class Rectangle
{
internal protected double _length;
internal protected double _width;
//Constructor
public Rectangle(double length, double width)
{
_length = length;
_width = width;
}
//Default Constructor
public Rectangle()
{
_length = 5;
_width = 5;
}
//Property
public virtual double Length
{ get { return _length; } }
public virtual double Width
{ get { return _width; } }
public virtual double Height
{ get { return 0; } }
//Methods
public virtual double Perimeter()
{
return 2 * (_length + _width);
}
public virtual double Area()
{
return _length * _width;
}
public virtual double Volume()
{
return 0;
}
}
}
//==============================================================
namespace Zau_Polymorphic_Rect_Lab
{
[Serializable]
public class Box : Rectangle
{
internal protected double _height;
public Box(double length, double width, double height):base (length, width)
{
_height = height;
}
public Box(double height)
{
_height = 6;
}
public override double Height
{ get { return _height; } }
public override double Perimeter()
{
// P = 4L+4W+4H
return (4 * Length) + (4 * Width) + (4 * Height);
}
public override double Area()
{
return 2 * (Length * Height) + 2 * (Length * Width) + 2 * (Width * Height);
}
public override double Volume()
{
return Length * Width * Height;
}
}
}
//==============================================================
namespace Zau_Polymorphic_Rect_Lab
{
[Serializable]
public class Pyramid : Rectangle
{
private double _height;
public Pyramid(double length, double width, double height): base(length, width)
{
_height = height;
}
public override double Height
{ get { return _height; } }
public override double Area()
{
return Length * Width + (Length * Math.Sqrt (Math.Pow((Width/2), 2) +
Math.Pow (Height, 2)) + Width * Math.Sqrt (Math.Pow((Length/2), 2) + Math.Pow(Height, 2)));
}
public override double Volume()
{
return 1f / 3f * (Length * Width) * Height;
}
}
}
//==============================================================
using System.Windows.Forms;
using System.IO; //added
using System.Runtime.Serialization;//added
using System.Runtime.Serialization.Formatters.Binary;//added
namespace Zau_Polymorphic_Rect_Lab
{
public partial class Form1 : Form
{
List<Rectangle> rectList = new List<Rectangle>();
string fileLocation = "RectList.dat";
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void btnCreateRectangle_Click(object sender, EventArgs e)
{
double length = rand.Next(5, 50);
double width = rand.Next(10, 100);
Rectangle r = new Rectangle(length, width);
rectList.Add(r);
MessageBox.Show("Rectangle shape has been added");
Display();
}
private void btnCreateBox_Click(object sender, EventArgs e)
{
double length = rand.Next(10, 100);
double width = rand.Next(5, 50);
double height = rand.Next(5, 50);
Box b = new Box(length, width, height);
rectList.Add(b);
MessageBox.Show("Rectangle box has been added");
Display();
}
private void btnCreatePyramid_Click(object sender, EventArgs e)
{
double length = rand.Next(10, 100);
double width = rand.Next(5, 50);
double height = rand.Next(5, 50);
Pyramid p = new Pyramid(length, width, height);
rectList.Add(p);
MessageBox.Show("Your shape has been added");
Display();
}
private void Display()
{
listView1.Items.Clear();//clear the contents
ListViewItem lstvitem;
foreach (Rectangle r in rectList)
{
lstvitem = new ListViewItem(r.GetType().Name.ToString());
lstvitem.SubItems.Add(r.Length.ToString("f"));
lstvitem.SubItems.Add(r.Width.ToString("f"));
lstvitem.SubItems.Add(r.Height.ToString("f"));
lstvitem.SubItems.Add(r.Perimeter().ToString("f"));
lstvitem.SubItems.Add(r.Area().ToString("f"));
lstvitem.SubItems.Add(r.Volume().ToString("f"));
listView1.Items.Add(lstvitem);
}
}
private void btnDisplayAllObjects_Click(object sender, EventArgs e)
{
Display();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("Would you like to save your data", "Save Data?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
IFormatter formatter = null;
Stream stream = null;
try
{
formatter = new BinaryFormatter();
stream = new FileStream(fileLocation, FileMode.Append, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, rectList);
}
catch (SerializationException se)
{
MessageBox.Show(se.Message);
}
finally
{
if (stream != null)
{
stream.Close();
}
}
MessageBox.Show("Data saved successfully", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//deserialize peopleList
BinaryFormatter formatter = new BinaryFormatter();
Stream s = null;
try
{
//1. Create a Formatter
//2. Create file stream
s = new FileStream(fileLocation,
FileMode.Open,
FileAccess.ReadWrite, FileShare.None);
//3.Deserialize (reconstruct the list of rect
while (s.Position != s.Length)
{
rectList = (List<Rectangle>)formatter.Deserialize(s);
}
MessageBox.Show("Rectangle list has been deserialized");
}
catch (IOException ioe)
{
MessageBox.Show(ioe.Message);
}
catch (SerializationException se)
{
MessageBox.Show(se.Message);
}
finally
{
if (s != null) s.Close();
}
}
}
}
Labels:
CSI-155
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment