How to Create a console-based program whose Main() method
holds two integer variables. Assign values to the variables.
Within the class, create two methods, Sum() and
Difference(), that compute the sum of and difference
between the values of the two variables, respectively. Each
method should perform the computation and display
the results. In turn, call each of the two methods from
Main(), passing the values of the two integer variables.?
VulpesPosted Sep 10, 2014, 6:29 AM
Wim SturkenboomPosted Sep 13, 2014, 3:03 AM
look at Int32.TryParse() and at try/catch
Wim SturkenboomPosted Sep 13, 2014, 1:58 AM
Create a gui app, drag three textboxes on to it. Drag a couple of buttons. Give the textboxes and buttons sensible names. For the textbox that holds the result, e.g. tbResult, for a button to add tbAdd. Add an event handler to each button; in VS, simply double click the button.
I did not see a requirement for a static class so the class for the calculations (called Numbers) could be like this:
public class Numbers
{
int ii;
int jj;
public Numbers(int num1, int num2)
{
ii = num1;
jj = num2;
}
public int Add()
{
return ii + jj;
}
}
You can add methods for multiply etc
Your btnAdd event handler
private void btnAdd_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(textBox1.Text);
int y = Convert.ToInt32(textBox2.Text);
Numbers newnumbers = new Numbers(x, y);
int z = newnumbers.Add();
tbResult.Text = z.ToString();
}
First you need to instantiate the numbers class with two numbers (coming from textBox1 and textBox2). Next you can cal the method to do the work and in the last step you place the result in tbResult.
You can figure out the other ones yourself.
Note: there is no checking if the user actually types numbers; if a user types a letter and click the button, it will crash. For you to figure out how to prevent that ;)
lovlly bersbePosted Sep 12, 2014, 11:32 PM
VulpesPosted Sep 10, 2014, 11:51 AM
lovlly bersbePosted Sep 10, 2014, 10:48 AM