Is there any way to do a mathematical addition between methods? - c#

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;
}
}

Related

Can property setter condition acitvate by constructor?

I was expected that constructor can go into the setter condition, I have done the following attempts.
static void Main(string[] args)
{
Iitem car = new Car(7000);
Console.WriteLine($"cost={car.cost}");//expect output "too expensive",but actually show 7000
car.cost = 7000;
Console.ReadLine();
}
public interface Iitem
{
int cost { get; set; }
string status {get;set; }
}
class Car:Iitem
{
private int mycost;
public int cost
{
get { return mycost; }
set
{
if (value > 5000)
{
mycost = 0;
Console.WriteLine("too expensive");
}
else
{
mycost = value;
}
}
}
public string status { get; set; }
public Car(int cost)
{
this.mycost = cost;
}
}
If I discard car.cost=7000 from Main() function, then I can't get the output of too expensive.
you are not getting the desired result because you are setting the value directly into the variable "mycost" in the constructor. Replace it with this.cost = cost;

i have a problem with my adaptive moving average trading system

I created a trading system with an adaptive moving average on the average true range but the program reports this error to me
the modifier public is not valid for this item
at line 21 of the code
public int avgTrueRange.value1 { get; set; }
I tried to change public but it always reports this error.
this is the code :
public class MediaMobileAdattiva : SignalObject
{
public MediaMobileAdattiva(object _ctx): base(_ctx)
{
Range = 14;
FirstLength = 10;
AvgTrueRange.value1 = 1;
}
private IOrderMarket buy_order;
public int Range { get; set; }
public double FirstLength { get; set; }
public int AvgTrueRange.value1 { get; set; }
private double FirstAverage()
{
if (AverageTrueRange < AvgTrueRange.value1)
return FirstLength;
}
protected override void Create()
{
// create variable objects, function objects, order objects
buy_order = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, EOrderAction.Buy));
}
protected override void StartCalc()
{
// assign inputs
}
protected override void CalcBar()
{
// strategy logic
if (Bars.CurrentBar > 1)
{
switch (FirstAverage)
{
case FirstLength:
return 1;
}
}
if (Bars.CurrentBar > 1 && Bars.Close.CrossesOver(FirstAverage, ExecInfo.MaxBarsBack)
{
switch (FirstLength)
{
case 1:
buy_order.Send(Bars.Close[0]);
}
}
}
}
What you need is to make a struct for AvgTrueRange:
public struct Range
{
public int value1 {get; set;}
}
and change:
public int AvgTrueRange.value1 { get; set; }
to
public Range AvgTrueRange { get; set; }
Your code still won't compile btw but I don't really understand what you are trying to do in this line:
if (AverageTrueRange < AvgTrueRange.value1)
Also, change:
switch (FirstAverage)
{
case FirstLength:
return 1;
}
to
var avg = FirstAverage();
int? result = avg switch
{
var avg when avg == FirstLength => 1,
_ => null
};
if (result.HasValue) return result.Value;
as cases can only be constant values.

C# How to make an object from class Person kill another?

In the following code, I'm trying to learn how to get objects to interact with each other, because I feel that's a bit more important than what I have done now which is simply collect a bunch of variables assign to each object.
For fun, what I want these different objects to do is to kill each other. The person named Jack can kill. The other two can not. What I want Jack to do is strike the other two, making them lose 1, 5 or 10 HitPoints multiple times until they are dead, and then set their Alive to false.
I don't know how to even start this, but I think it would be a very fun and interesting exercise.
The most important thing about doing this is learning how one object can directly change something about a different object, just because it can, and that the objects it has then changed, will then suffer a consequence from this action.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OOP_Learning
{
class Program
{
static void Main(string[] args)
{
Person p1;
p1 = new Person("Jack", 27, true, true, 10);
Person p2;
p2 = new Person("Vincent", 63, true, false, 10);
Person p3;
p3 = new Person("Tim", 13, true, false, 10);
Console.ReadLine();
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public bool Alive { get; set; }
public bool AbilityToKill { get; set; }
public int HitPoints { get; set; }
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
{
HitPoints = hitPoints;
AbilityToKill = abilityToKill;
Alive = alive;
Name = name;
Age = age;
}
}
}
You need 2 methods in the Person class.
Hit -> This method reduce the HitPoints on self object with each hit. When the HitPoints become Zero, status Alive is set to false.
Kill -> This will take person as a parameter and call Hit method on that person till that person is Alive.
Add following methods to Person class:
public void Hit()
{
if(Alive)
{
if(HitPoints > 0)
{
HitPoints -= 1;
}
else
{
Alive = false;
}
}
}
public bool Kill(Person person)
{
if(!AbilityToKill)
{
Console.WriteLine("You don't have ability to kill! You cannont kill {0}.", person.Name);
return false;
}
while(person.Alive)
{
person.Hit();
}
Console.WriteLine("{0} is dead.", person.Name);
return true;
}
Call Kill method in Main method.
p2.Kill(p3);
p1.Kill(p2);
p1.Kill(p3);
Hope this helps!
Is this the kind of thing you mean?
public class Person
{
public string Name { get; private set; }
public int Age { get; private set; }
public bool Alive { get; private set; }
public bool AbilityToKill { get; private set; }
public int HitPoints { get; private set; }
public void Hit(int points)
{
this.HitPoints -= points;
if (this.HitPoints <= 0)
{
this.HitPoints = 0;
this.Alive = false;
}
}
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
{
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
}
}
Now with the check for AbilityToKill:
public class Person
{
public string Name { get; private set; }
public int Age { get; private set; }
public bool Alive { get; private set; }
public bool AbilityToKill { get; private set; }
public int HitPoints { get; private set; }
public int Attack(Person victim, int points)
{
var hp = victim.HitPoints;
if (this.AbilityToKill)
{
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
{
victim.HitPoints = 0;
victim.Alive = false;
}
}
hp -= victim.HitPoints;
return hp;
}
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
{
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
}
}
This can be used like this:
var jack = new Person("Jack", 27, true, true, 10);
var vincent = new Person("Vincent", 63, true, false, 10);
var tim = new Person("Tim", 13, true, false, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);
The Attack method returns the actual number of hits points reduced by the attack - the damage dealt.
And here's a more strongly-typed version. Using bool for properties isn't always the clearest way to code. Through in some enums and it's clearer.
public class Person
{
public string Name { get; private set; }
public int Age { get; private set; }
public Alive Alive { get; private set; }
public AbilityToKill AbilityToKill { get; private set; }
public int HitPoints { get; private set; }
public int Attack(Person victim, int points)
{
var hp = victim.HitPoints;
if (this.AbilityToKill == AbilityToKill.Yes)
{
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
{
victim.HitPoints = 0;
victim.Alive = Alive.No;
}
}
hp -= victim.HitPoints;
return hp;
}
public Person(string name, int age, Alive alive, AbilityToKill abilityToKill, int hitPoints)
{
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
}
}
public enum Alive { Yes, No }
public enum AbilityToKill { Yes, No }
It's used like this:
var jack = new Person("Jack", 27, Alive.Yes, AbilityToKill.Yes, 10);
var vincent = new Person("Vincent", 63, Alive.Yes, AbilityToKill.No, 10);
var tim = new Person("Tim", 13, Alive.Yes, AbilityToKill.No, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);

How to acces a method from another instance that was given in the constructor?

So I have the following set-up.
A sensor that detects the temperature of a room a controller then checks if it is below or above a set temperature and if it is below it starts a heater.
Now how do I get the method GetCurTemp() to get the temperature I have set?
public class TempSensor
{
public int Temp { get; set; }
}
public class Control
{
private int threshold;
public Control(TempSensor t, Heater h, int thr)
{
threshold = thr;
}
public void SetThreshold(int thr)
{
threshold = thr;
}
public int GetThreshold()
{
return threshold;
}
public int GetCurTemp()
{
return ???;
}
}
class Test
{
static void Main(string[] args)
{
var tempSensor = new TempSensor();
var heater = new Heater();
var uut = new Control(tempSensor, heater, 25);
Console.WriteLine("Set the current temperatur");
int n= int.Parse(Console.ReadLine());
tempSensor.Temp = n;
}
}
You need to keep a reference to the TempSensor in your Control class. Then you can access the temperature from that reference.
public class Control
{
private int threshold;
private TempSensor sensor;
public Control(TempSensor t, Heater h, int thr)
{
threshold = thr;
sensor = t;
}
public void SetThreshold(int thr)
{
threshold = thr;
}
public int GetThreshold()
{
return threshold;
}
public int GetCurTemp()
{
return sensor.Temp;
}
}
You aren't doing anything with the TempSensor object you're passing into the Control constructor. You should set up a field like sensorTemp in your Control class to hold this value.
public class Control
{
private int threshold;
private int sensorTemp;
public Control(TempSensor t, Heater h, int thr)
{
threshold = thr;
sensorTemp = t.Temp;
}
public void SetThreshold(int thr)
{
threshold = thr;
}
public int GetThreshold()
{
return threshold;
}
public int GetCurTemp()
{
return sensorTemp;
}
}

Get the index in a multidimensional class

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; }
}

Categories

Resources