2 questions, first one is about a program that i'm trying to execute which won't work, and the second is about the .this keyword.
first question is from my c# book, which provided the following task. i have the idea & pretty sure i know how to write it, but a problem occurred.
this is the task :
Write a program with a Math class that has four methods - add, subtract, multiply and divide, each of which take 2 parameters. Call each method from Main() and provide an appropriate output statement to demonstrate that each method works. You don't need to have the user provide input; just provide the two integers to the methods within main.
this is my code :
namespace MathExercise
{
public class Math
{
// private variables set here
private int x;
private int y;
private int sum;
public void doAdd() // Add method
{
sum = x + y;
Console.WriteLine("sum of {0} + {1} = {2}",
x, y, sum);
}
public void doSubtract() // Subtract method
{
sum = x - y;
Console.WriteLine("{0} - {1} = {2} ",
x, y, sum);
}
public void doMultiply() // Multiply method
{
sum = x * y;
Console.WriteLine("(0} * {1} = {2} ",
x, y, sum);
}
public void doDivide() // Divide method
{
sum = x / y;
Console.WriteLine("(0} / {1} = {2} ",
x, y, sum);
}
public Math(int myX, int myY) // constructor method
{
myX = x;
myY = y;
}
}
public class Tester
{
static void Main()
{
Math Add = new Math(6, 3);
Add.doAdd(); // invoke the doAdd method
Math Subtract = new Math(6, 3);
Subtract.doSubtract(); //invoke the doSubtract method
Math Multiply = new Math(6, 3);
Multiply.doMultiply(); //invoke the doMultiply method
Math Divide = new Math(6, 3);
Divide.doDivide(); //invoke the doDivide method
}
}
}
these are the errors that the compiler tells me about :
Warning 1 Field 'StaticTester.Math.x' is never assigned to, and will always have its default value 0
Warning 2 Field 'StaticTester.Math.y' is never assigned to, and will always have its default value 0
can you guys explain what am i doing wrong?
i've announced the x and y variables at the beginning of the first class, and used a constructor method to convert the private int x and y into myX and myY that will be used later on in the main method. then, the variables were given a value at the main method, while creating a new instance of Math. what am i doing wrong?
the book gives a solution to the exercise, i wont copy it (unless you guys want me to), but i can tell you that what they did was while creating a method they gave it parameters within the parentheses [ example - public int Add( int left, int right) { return left + right; } ] etc. and at the main method in a new class they wrote -
Math m = new Math();
int sum = m.Add(3,5); and so on, then wrote it on the screen with a simple console.writeline.
so here are my questions -
a. why am i getting those errors?
b. was my solution to the exercise basically wrong? was the whole idea fked up?
c. i know that semantically using sum for divide etc. is wrong but thats the identifier that i thought of while writing the code. Syntax wise; is it wrong to give sum the value x + y at the beginning and then x - y and so on?
my second question is about the .this keyword.
the book gave the following example-
private int length;
// bits of code
public void SomeMethod (int length)
{
this.length = length;
}
what i cant seem to understand is the use of it. why should the compiler care that this.length refers to the member variable and length refers to the parameter? can't he understand that by himself? why should i use it and when?
any sort of help would be really appreciated!

New Topic/Question
Reply




MultiQuote






|