Programming newbie here and I've been breaking my head over this for several hours now.
I can make a coordinate object but then I want to make a dot object that can access the coordinate fields from a Coordinate object. How do I "link" these two classes together? And do you have any recommendations for good YouTube videos that explain what I'm missing here? Thanks!
class Coordinate
{
public int X { get; private set; } = 0;
public int Y { get; private set; } = 0;
public Coordinate(int x, int y)
{
x = X;
y = Y;
}
}
class Dot
{
public string color { get; set; }
public Dot(string color, Dot dot)
{
this.Color = color;
}
}
class Program
{
static void Main(string[] args)
{
Coordinate coor1 = new Coordinate(2, 3);
Dot dot1 = new Dot("Blue", coor1);
}
Here is what you are searching for a "linking" your classes. In object-oriented programming this is called composition.
That way you can use functionality and data of Coordinate-instance inside your Dot class.
class Coordinate
{
public int X { get; private set; }
public int Y { get; private set; }
public Coordinate(int x, int y)
{
X = x;
Y = y;
}
}
class Dot
{
public Coordinate coord { get; private set; }
public string color { get; set; }
public Dot(string color, Coordinate coord)
{
this.color = color;
this.coord = coord;
}
}
class Program
{
static void Main(string[] args)
{
Coordinate coor1 = new Coordinate(2, 3);
Dot dot1 = new Dot("Blue", coor1);
Console.WriteLine(dot1.coord.X);
}
}
Note: I also fixed possible typo in Coordinate-constructor (setting X=x and Y=y..)
Related
I need to find the 2D distance between two stations. I need to create a station class that can be used to store a list of stations as objects, such as this:
class Station
{
public Station(string name, double X, double Y)
{
Name = name;
xcor = X;
ycor = Y;
}
public string Name {get; set;}
public double xcor {get; set;}
public double ycor {get; set;}
}
class Program
public static void Main(string[] args)
{
public List<Station> Stationlist = new List<Station>();
Stationlist.Add(new Station("po",1,1));
Stationlist.Add(new Station("wsx",200,200));
}
I need to create a distance method that will calculate the distance between these two stations by running it like this:
Console.WriteLine(Distance.euDistance(station[0], station[1]));
I have tried to create a method to calculate the euclidean distance but can't get it to successfully calculate the distance between the two stations. This is what I have created for my Distance method:
class Distance
{
public static double distanceTEST(Station current, Station next)
{
return Math.Sqrt((Math.Pow((current.Station.X - next.Station.X), 2) +
Math.Pow((current.Station.Y - next.Station.Y), 2) *
100000.0 / 100000.0) * 1);
}
}
I want to have it print a result such as this: (that is just an example)
Console.WriteLine("{0} -> {1} {2} meters, Name[0], Name[1], distance);
po -> wsx 56.6505106 meters
Maybe you can write a extension method for Station class.
class Program
{
static void Main(string[] args)
{
var station1 = new Station("po", -7, 17);
var station2 = new Station("wsx", -4, 6.5);
Console.WriteLine(station1.CalculateEuDistance(station2));
Console.ReadKey();
}
}
public class Station
{
public string Name { get; set; }
public double X { get; set; }
public double Y { get; set; }
public Station()
{
}
public Station(string name, double x, double y)
{
Name = name;
X = x;
Y = y;
}
}
public static class StationExtensions
{
public static double CalculateEuDistance(this Station currentStation, Station station)
{
return Math.Sqrt(Math.Pow(currentStation.Y - currentStation.X,2) + Math.Pow(station.Y - station.X,2));
}
}
I'm developing a Poker Texas Hold'Em app I have all the combinations working but i cant seem to find a compact way to check which player has the highest combination and the type of it here's some of my code
void Rules(int current,int Power)
{
//checking if the player has any pair
{
Power = 1;
current = 1;
}
}
current = the type of the hand (pair=1,two pair =2..straight flush=8)
Power = the power of the hand (pair of deuces=2....pair of kings=13)
and so on.. I'm updating current and Power with each new combination and since they are parameters of the void each player has his own "current" and "Power" so they don't get messed up.This is what i have so far and my question is how do i check which player has the highest hand without using 20-30 repetitive if statements i was thinking about :
List <int> Arr = new List <int>();
void Rules(int current,int Power)
{
//checking if the player has any pair
{
Power = 1;
current = 1;
Arr.Add(Power);
Arr.Add(current);
}
}
but like this i have no idea which type belongs to which player so it's not use i also tried with strings but than i wont be able to compare them that easily.What should i do ? What's the right approach ?
You might want to make a class for reach rule, to encapsulate the logic. Something like this:
public class Card
{
public string Name { get; private set; }
/* cards have a value of 2-14 */
public int Value { get; private set; }
}
public abstract class Rule()
{
public abstract string Name { get; }
/* hands are calculated in powers of 100, when the card value is added you will get something like 335 */
public abstract int Value { get; }
public abstract bool HasHand(IReadonlyList<Card> cards);
}
public class PairRule() : Rule
{
public override string Name
{
get { return "Pair"; }
}
public override int Value
{
get { return 100; }
}
public override bool HasHand(IReadonlyList<Card> cards)
{
/* implement rule here */
return Enumerable.Any(
from x in cards
group x by x.Value into g
where g.Count() == 2
select g
);
}
}
...
public class Player
{
public IReadonlyList<Card> Hand { get; private set; }
public int GetHandValue(IReadonlyList<Rule> rules)
{
/* get value of hand 100, 200, 300 etc. */
var handValue = Enumerable.Max(
from x in rules
where x.HasHand(Hand)
select x.Value
);
/* get value of cards */
var cardValue = Hand
.OrderByDescending(x => x.Value)
.Take(5)
.Sum();
return handValue + cardValue;
}
}
public class Pot
{
public int Value { get; private set; }
public IReadonlyList<Player> Players { get; private set; }
public IReadonlyList<Player> GetWinners(IReadonlyList<Rule> rules)
{
var playerHands = Enumerable.ToList(
from x in players
select new {
Player = x,
HandValue = x.GetHandValue(rules)
}
);
var maxHand = playerHands.Max(x => x.HandValue);
return Enumerable.ToList(
from x in playerHands
where x.HandValue == maxHand
select x.Player
);
}
}
Sorry if the question is redundant, but I couldn't find a solution for my particular case.
Please consider this block of code:
public interface IPoint {}
public class GazePoint : IPoint {}
public Point AvgPoint(IEnumerable<IPoint> locations) {}
List<GazePoint> gazePoints = new List<GazePoint>();
//...
// this doesn't work:
Point avg = AvgPoint(gazePoints);
Could you please explain why it doesn't work (I was assuming C# 4.0 has solved this issue) and how can I change the signature of AvgPoint() method to make it possible to receive different implementations of IPoint. (I don't want to cast gazePoints collection to another type of collection, because it is in a big loop, and I'm concern about the performance.
[Update]: I had defined GazePoint as struct, and that was the source of problem. Still, I don't know why struct is not working here.
I'm not sure what the exact problem is you're having, but here's how it worked for me:
First, some actual class implementations:
public interface IPoint
{
int X { get; set; }
int Y { get; set; }
}
public class Point : IPoint
{
public int X { get; set; }
public int Y { get; set; }
public Point()
{
}
public Point(int x, int y)
{
X = x;
Y = y;
}
}
public class GazePoint : IPoint
{
public int X { get; set; }
public int Y { get; set; }
public GazePoint()
{
}
public GazePoint(int x, int y)
{
X = x;
Y = y;
}
}
Then an actual AvgPoint method implementation:
public static Point AvgPoint(IEnumerable<IPoint> locations)
{
if (locations == null || !locations.Any()) return new Point(0, 0);
return new Point((int) locations.Average(l => l.X),
(int) locations.Average(l => l.Y));
}
And finally a few tests:
public static void Main()
{
var points = new List<Point>
{
new Point(1, 2),
new Point(3, 4)
};
var gazePoints = new List<GazePoint>
{
new GazePoint(1, 2),
new GazePoint(3, 4)
};
Point avgPoint = AvgPoint(points);
Point avgGazePoint = AvgPoint(gazePoints);
Console.WriteLine("Average Point = {0}, {1}", avgPoint.X, avgPoint.Y);
Console.WriteLine("Average GazePoint = {0}, {1}", avgGazePoint.X, avgGazePoint.Y);
}
If your goal is to have the method return the average in the same type that was passed in, you can make it generic like so:
public static T AvgPoint<T>(IEnumerable<T> locations) where T : IPoint, new()
{
if (locations == null || !locations.Any()) return new T {X = 0, Y = 0};
return new T {X = (int) locations.Average(l => l.X),
Y = (int) locations.Average(l => l.Y)};
}
I want to know if there is any way to do a mathematical addition between methods? The code is as below.
From Main: (I need to the calculation but I can not do calculation between methods)
Ball.setPositionY = Ball.setPositionY + Ball.setSpeedY;
From a Class:
public class Ball
{
public int speedX { get; set; }
public int speedY { get; set; }
public int positionX { get; set; }
public int positionY { get; set; }
public Ball(int speedX, int speedY, int positionX, int positionY)
{
this.speedX = speedX;
this.speedY = speedY;
this.positionX = positionX;
this.positionY = positionY;
}
public void setSpeedX(int newSpeedX)
{
speedX = newSpeedX;
}
public void setSpeedY(int newSpeedY)
{
speedY = newSpeedY;
}
public void setPositionX(int newPositionX)
{
positionX = newPositionX;
}
public void setPositionY(int newPositionY)
{
positionY = newPositionY;
}
}
This is how you should do it:
public class Ball
{
public int SpeedX { get; set; }
public int SpeedY { get; set; }
public int PositionX { get; set; }
public int PositionY { get; set; }
public Ball(int speedX, int speedY, int positionX, int positionY)
{
this.SpeedX = speedX;
this.SpeedY = speedY;
this.PositionX = positionX;
this.PositionY = positionY;
}
}
public class Program
{
public static void Main(string[] args)
{
Ball ball1 = new Ball(1,1,1,1);
Ball ball2 = new Ball(2,2,2,2);
Ball ball3 = new Ball(3,3,3,3);
ball3.SpeedX = ball1.SpeedX + ball2.SpeedX;
}
}
How about letting the set method also return the value that was set or add a get method that gives you that same value. But...since you want that, why don't you just use the public properties on the object?
So, your method(s) can look like
public int setPositionX(int newPositionX)
{
positionX = newPositionX;
return newPositionX;
}
But, since you are creating your own getters and setters now and you already have them. Use the public properties and all should be fine.
After assigning the value, you just return the value. It will solve your problem. If you want to use methods.
For eg.
public int setSpeedX(int newSpeedX)
{
speedX = newSpeedX;
return speedX;
}
That's what properties are for; you should use the properties for that.
If you mean an addition in the functional programming sense, I can make no sense of your example.
Your methods must return value in order to add them,but public properties do what you want.
Ball.positionY =Ball.positionY + Ball.SpeedY;
Since all your methods have void return type there's no way to "add" them.
If you want to do that, change return type of your methods to int.
Edit
You can "add" methods that have int return type, but the result can't be a method.
Sample:
public int setPositionY(int newPositionY)
{
positionY = newPositionY;
return positionY;
}
public int setSpeedY(int newSpeedY)
{
speedY = newSpeedY;
return speedY;
}
positionY = setPositionY(/*new position*/) + setSpeedY(/*new speed*/);
try the below code...
namespace Foo
{
class Program
{
static void Main(string[] args)
{
Ball b = new Ball(1,2,3,4);
//b.setPositionY() = b.setSpeedY() + b.setSpeedY();
b.setPositionY(b.setSpeedX() + b.setSpeedY());
}
}
public class Ball
{
public int speedX { get; set; }
public int speedY { get; set; }
public int positionX { get; set; }
public int positionY { get; set; }
public Ball(int speedX, int speedY, int positionX, int positionY)
{
this.speedX = speedX;
this.speedY = speedY;
this.positionX = positionX;
this.positionY = positionY;
}
public int setSpeedX()
{
//speedX = newSpeedX;
return 10;
}
public int setSpeedY()
{
//speedY = newSpeedY;
return 20;
}
public int setPositionX()
{
//positionX = newPositionX;
return 1;
}
public void setPositionY(int newPositionY)
{
positionY = newPositionY;
}
}
Sorry for the title i will put here an example of what i want to accomplish:
namespace mdclass
{
class pClass
{
static void Main()
{
tiles<int> tl = new tiles<int>();
tl[0].X = 0;
}
}
class tiles<T> : List<T>
{
public int X
{
//get/set the X-coordonate
}
public int Y
{
//get/set the Y-coordonate
}
}
}
how can i transfer the [0] from the tl[0] in the public int X and work with it?
Create a class for x and y coordinates:
public sealed class Point {
public int X { get; set; }
public int Y { get; set; }
}
Use the Point class to store the coordinates into the list:
public sealed class Program {
public static void Main() {
var tiles = new List<Point>();
tiles.Add(new Point { X = 5, Y = 10 });
tiles[0].X = 15;
}
}
Could you not just make tl public?
Then myInt = mypClass.tl[0].X
A data heirachy like this might work for you (no time to add actual code, sorry!). Then you could implement appropriate getters and setters.
Public class Grid {
List<Row>
}
Public class Row{
List<Point>
}
Public Class Point{
public int X { get; set; }
public int Y { get; set; }
}