C
C#•14mo ago
MRW419

Is anyone good with statice modifier?

I need help with my class work about statice modifiers as I'm nearly lost in what I need to do.
// TODO: Define a simple class called MyMath with the following static methods:
// * Add - adds two integers together and returns the result.
// * Subtract - subtracts integer b from integer a and returns the result.
// * Multiply - Multiplies two integers and returns the result.
// * Divide - divides two integers and returns the result.

// TODO: In MyMath, add the following static variable:
// * sumTotal - the sum of every time the Add method is called.


public class StaticPractice : MonoBehaviour
{
void Start()
{
// uncomment the following code to test your class.
/*
print(MyMath.Add(10,15));
print(MyMath.Add(13,22));
print(MyMath.Add(56,83));
print(MyMath.sumTotal);

print(MyMath.Subtract(10,5));

print(MyMath.Multiply(4,8));

print(MyMath.Divide(10,2));
*/
}
}
// TODO: Define a simple class called MyMath with the following static methods:
// * Add - adds two integers together and returns the result.
// * Subtract - subtracts integer b from integer a and returns the result.
// * Multiply - Multiplies two integers and returns the result.
// * Divide - divides two integers and returns the result.

// TODO: In MyMath, add the following static variable:
// * sumTotal - the sum of every time the Add method is called.


public class StaticPractice : MonoBehaviour
{
void Start()
{
// uncomment the following code to test your class.
/*
print(MyMath.Add(10,15));
print(MyMath.Add(13,22));
print(MyMath.Add(56,83));
print(MyMath.sumTotal);

print(MyMath.Subtract(10,5));

print(MyMath.Multiply(4,8));

print(MyMath.Divide(10,2));
*/
}
}
37 Replies
ZacharyPatten
ZacharyPatten•14mo ago
that assignment looks extremely straightforward to me what are you stuck on?
MRW419
MRW419OP•14mo ago
Well on how it works and what statics do It just doesn't make full sense to me
ZacharyPatten
ZacharyPatten•14mo ago
it tells you exactly what to do what is the first comment of the instructions you dont undersatand?
MRW419
MRW419OP•14mo ago
I get what to it wants me to do but figuring out how it works doesn't
ZacharyPatten
ZacharyPatten•14mo ago
what do you mean by "figuring out how it works doesn't"? based on what you shared you haven't even started the assignment start with the first instruction
Define a simple class called MyMath
do that first
MRW419
MRW419OP•14mo ago
Ok so that would simple
public class MyMath
{
public static int Add(int a, int b)
{
Add = a + B
}

}
public class MyMath
{
public static int Add(int a, int b)
{
Add = a + B
}

}
That's how far I got
ZacharyPatten
ZacharyPatten•14mo ago
that is a good start but not quite right you are correctly declaring a class "MyMath" so that is fine, and you made a method "Add" and you made it static as instructed. so all that is good but you have compiler errors inside your Add method what compiler errors are you getting at the moment? you should see some
MRW419
MRW419OP•14mo ago
well it was compiler erroe CS1955
ZacharyPatten
ZacharyPatten•14mo ago
yep, and what is the message with it
MRW419
MRW419OP•14mo ago
Non-invocable member 'name' cannot be used like a method.
ZacharyPatten
ZacharyPatten•14mo ago
Add = a + B look at this line specifically. it is not right
MRW419
MRW419OP•14mo ago
Oh, wait. Do I need to declare Add as a int?
ZacharyPatten
ZacharyPatten•14mo ago
no what you are making is a method methods have a "return"
MRW419
MRW419OP•14mo ago
Oh
ZacharyPatten
ZacharyPatten•14mo ago
you have defined the method to return an int but you never return anything currently
MRW419
MRW419OP•14mo ago
I see so I show make this?
return Add
return Add
ZacharyPatten
ZacharyPatten•14mo ago
you do need return. but you need to return the sum of a and b
MRW419
MRW419OP•14mo ago
Which equals add
ZacharyPatten
ZacharyPatten•14mo ago
no "Add" is the name of the method you don't need to write "Add" instide the method you already defined teh method
MRW419
MRW419OP•14mo ago
So name it something else?
ZacharyPatten
ZacharyPatten•14mo ago
you are writing the code that will run when "Add" is called
MRW419
MRW419OP•14mo ago
Then return that vaule?
ZacharyPatten
ZacharyPatten•14mo ago
Methods - C# Programming Guide - C#
A method in C# is a code block that contains a series of statements. A program runs the statements by calling the method and specifying arguments.
ZacharyPatten
ZacharyPatten•14mo ago
look at that documentation and see if you can figure out what you need
MRW419
MRW419OP•14mo ago
So like
Number = a + b;
return Number;
Number = a + b;
return Number;
ZacharyPatten
ZacharyPatten•14mo ago
closer what is "Number"?
MRW419
MRW419OP•14mo ago
A int
ZacharyPatten
ZacharyPatten•14mo ago
no it isn't you jsut wrote "Number" you didn't write int Number C# is a type safe language when you define a variable, you have to define the type of the variable too
MRW419
MRW419OP•14mo ago
I see
ZacharyPatten
ZacharyPatten•14mo ago
int a; string b; object c; you always have type then variablename
MRW419
MRW419OP•14mo ago
Alright one more time
int Number = a + b;
return Number;
int Number = a + b;
return Number;
ZacharyPatten
ZacharyPatten•14mo ago
that should work 🙂 however you can simplify it to just return a + b; you don't need to declare "Number". it is unnecessary bloat. you can do it.. just unnecessary
MRW419
MRW419OP•14mo ago
But woulden't that be needed when doign the sumtotal?
ZacharyPatten
ZacharyPatten•14mo ago
I'm not sure what exactly you are asking
MRW419
MRW419OP•14mo ago
print(MyMath.Add(10,15)); print(MyMath.Add(13,22)); print(MyMath.Add(56,83)); print(MyMath.sumTotal); At the end is the sum total of everything we added Don't we need that Number so we can use it to make equal the sumtotal?
ZacharyPatten
ZacharyPatten•14mo ago
sumTotal is a seperate member from Add Your add method should be good you haven't declared sumTotal yet but yes you will need to add up the sum total sorry I missed that in your instructions so
int Number = a + b;
return Number;
int Number = a + b;
return Number;
is probably what you want yes, but you need to add "Number" to the sumTotal before you return
MRW419
MRW419OP•14mo ago
Alright got it, thank you for your help.
Want results from more Discord servers?
Add your server