If there is an error in the code segment, write error and explain the error. els
ID: 3905711 • Letter: I
Question
If there is an error in the code segment, write error and explain the error. else write output and display the output. We need an explanation of errors and please write right code.
Example;
class Multiply
{
public Multiply(int x, int y)
{
Console.WriteLine("Production result= {0}", x*y);
}
public int Product(int a, int b)
{
return a*b;
}
}
class Program
{
static void Main(string[ ] args)
{
Multiply c = new Multiply( );
Console.WriteLine("Result= {0} ", c.Product(3,5));
}
}
ERROR OR OUTPUT: If a constructor, different from default constructor, is declared, you can not use default constructor until you declare the default constructor
QUESTIONS:
1-)
class Division
{
public Division(int a,int b)
{
}
public static void Div(int a, int b)
{
Console.WriteLine("Result= {0:.##}", 1.0 * a / b);
}
}
class Program
{
static void Main(string[]args)
{
Division d = new Division();
Division.Div(3, 5);
}
}
2-)
class Multiply
{
public static int a, b;
public Multiply(int x,int y)
{
a = x;
b = y;
}
public static int Product()
{
return a * b
}
}
class Program
{
static void Main(string[]args)
{
Multiply c = new Multiply(3, 5);
Console.WriteLine("Result={0}", c.Product());
}
}
3-)
class Circle
{
private double pi = 3.14;
private int radius;
public double CArea()
{
return pi * radius * radius;
}
}
class Program
{
static void Main(string[]args)
{
Circle e = new Circle();
Circle radius = 10;
Console.WriteLine("Area={0:.##}", e.CArea());
}
}
4-)
class Arithmetic
{
public int Arithmetic()
{
private int Product (int a,int b)
{
return a * b;
}
}
class Program
{
static void Main(string[]args)
Arithmetic a = new Arithmetic();
Console.Writeline("Result={0}", a.Product(3,5));
}
}
5-)
void Summation(int a,intb)
{
int result;
result = a + b;
}
public static void Main(string[]args)
{
Console.Writeline("{0}", Summation(3,5));
}
Explanation / Answer
If you have any doubts, please give me comment...
1) ERROR: If a constructor, different from default constructor, is declared, you can not use default constructor until you declare the default construct
class Division
{
public Division(){}
public Division(int a,int b)
{
}
public static void Div(int a, int b)
{
Console.WriteLine("Result= {0:.##}", 1.0 * a / b);
}
}
class Program
{
static void Main(string[]args)
{
Division d = new Division();
Division.Div(3, 5);
}
}
2) ERROR: Product() cannot be accessed with an instance reference, because Product() method defined static.
class Multiply
{
public static int a, b;
public Multiply(int x,int y)
{
a = x;
b = y;
}
public int Product()
{
return a * b
}
}
class Program
{
static void Main(string[]args)
{
Multiply c = new Multiply(3, 5);
Console.WriteLine("Result={0}", c.Product());
}
}
3) ERROR: cannot implicity convert type int to Circle, even though we can't assign any value radius property bacause it is private property. If you want to assign value either public or set through accessor method
class Circle
{
private double pi = 3.14;
public int radius;
public double CArea()
{
return pi * radius * radius;
}
}
class Program
{
static void Main(string[]args)
{
Circle e = new Circle();
e.radius = 10;
Console.WriteLine("Area={0:.##}", e.CArea());
}
}
4) Constructor doesn't return any value, it can be declare as public with class name only and private method Product cannot be accessed outside of class
class Arithmetic
{
public Arithmetic()
{}
public int Product (int a,int b)
{
return a * b;
}
}
class Program
{
static void Main(string[]args)
Arithmetic a = new Arithmetic();
Console.Writeline("Result={0}", a.Product(3,5));
}
}
5) We cannot assign members directly, It must be in class and Summation member must be static within class, it must have return datatype as int and return value as restult.
class Program{
static int Summation(int a,int b)
{
int result;
result = a + b;
return result;
}
public static void Main(string[]args)
{
Console.WriteLine("{0}", Summation(3,5));
}
}