Method worksheet two
1. Analyze and determine the outputs of the following C# code?
A) using System;
namespace ConsoleApplication15
{
class Test
{
static int squre()
{
int x = 20,sqr;
sqr = x * x;
return sqr;
}
private void cube(double a)
{
double result = a * a * a;
Console.WriteLine("Cube of {0} is {1}",a,result);
}
static void Main(string[] args)
{
Test sample = new Test();
double x = 10;
int y;
y = squre();
Console.WriteLine("Result={0}", y);
sample.cube(x);
}
}
}
B) using System;
namespace Sample
{
class Test
{
private static void parametertest(double a,ref int b, out int c)
{
c = 10;
Console.WriteLine("----------------------");
Console.WriteLine("A={0}", a);
Console.WriteLine("B={0}", b);
Console.WriteLine("C={0}", c);
c = (c % 2 == 0 ? (c * b) : (c + b));
b = (b % 3 != 0 ? b++ : b + 2);
a = (a + b + c) / 3;
Console.WriteLine("----------------------");
Console.WriteLine("A={0}", a);
Console.WriteLine("B={0}", b);
Console.WriteLine("C={0}", c);
}
static void Main(string[] args)
{
double x=100.6;
int y=21, z;
Console.WriteLine("X={0}", x);
Console.WriteLine("Y={0}", y);
parametertest(x, y, z);
Console.WriteLine("----------------------");
Console.WriteLine("X={0}", x);
Console.WriteLine("Y={0}", y);
Console.WriteLine("Z={0}", z);
}
}
}
2. Write a method swap () the contents of two variables using a third variable.(using value
parameter )
3. Write a method sum () and average () of three numbers given by the user and display
the result. (using reference parameter )
4. Write a method largest () that can obtain two numbers from the keyboard, and determine
and display which (if either) is the larger of the two numbers. (using output parameter)
5. Write an overloaded method named validate (). The first method accepts an integer and
checks whether the number is even or not. The second method accepts two integers and
checks whether the sum of the numbers is greater than or equal to 100.