Richard Brennan
Member
- Joined
- Apr 22, 2016
- Messages
- 8
- Programming Experience
- 1-3
I have been learning c# for 5 weeks, and I have put a class together to see if I can code without looking at my notes. The result follows.
I would like to know if there is any other things I could add to the class that I do not know about:
I am aware of indexers, but unsure of them at the moment, and I am aware of IEnumerable and again I am unsure of these.
I am not necessarily asking for a detailed explanation, but more of a heads up.. did you know you can add this to your class... etc.
I would like to know if there is any other things I could add to the class that I do not know about:
I am aware of indexers, but unsure of them at the moment, and I am aware of IEnumerable and again I am unsure of these.
I am not necessarily asking for a detailed explanation, but more of a heads up.. did you know you can add this to your class... etc.
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Geometry
{
public class Point : IComparable<Point>
{
#region Fields
private double x;
private double y;
private double magnitude;
static int numberOfPoints;
#endregion
#region Properties
public double X
{
get { return this.x; }
set
{
this.x = value;
this.magnitude = Math.Sqrt(Math.Pow(value, 2) + Math.Pow(y, 2));
}
}
public double Y
{
get { return this.y; }
set
{
this.y = value;
this.magnitude = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(value, 2));
}
}
public double Magnitude
{
get { return this.magnitude; }
}
#endregion
#region constructors
static Point()
{
numberOfPoints = 0;
}
public Point()
{
numberOfPoints++;
this.magnitude = 0;
this.x = 0;
this.y = 0;
}
public Point(Point p)
{
numberOfPoints++;
this.x = p.x;
this.y = p.y;
this.magnitude = p.magnitude;
}
public Point(double xCoordinate, double yCoordinate)
{
numberOfPoints++;
this.x = xCoordinate;
this.y = yCoordinate;
this.magnitude = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
}
#endregion
//Methods
#region overloaded operators
static public Point operator +(Point point1, Point point2)
{
Point newPoint = new Point((point1.X + point2.X), (point1.Y + point2.Y));
return newPoint;
}
static public Point operator -(Point point1, Point point2)
{
Point newPoint = new Point((point1.X - point2.X), (point1.Y - point2.Y));
return newPoint;
}
#endregion
#region sort
public int CompareTo(Point obj)
{
return this.magnitude.CompareTo(obj.magnitude);
}
#endregion
#region object overrides
public override string ToString()
{
return String.Format("({0}, {1}) Magnitude: {2}", this.x, this.y, this.magnitude);
}
public override bool Equals(object obj)
{
if (!(obj is Point)) return false;
if (obj == null) return false;
return (this.x == ((Point)obj).x &&
this.y == ((Point)obj).y);
}
public override int GetHashCode()
{
return this.x.GetHashCode() ^ this.y.GetHashCode() ^ this.magnitude.GetHashCode();
}
#endregion
}
}