The C# program below is not completed. Add the code to the program based on the
ID: 3710629 • Letter: T
Question
The C# program below is not completed. Add the code to the program based on the comments included in the program so that it can work properly.
public class Example
{
private string str = "hello";
public static void funA() { Console.WriteLine("this is from a static function funA()"); }
public Example(string str)
{
//initialize the field str here
}
private double funB(int r) { return 3.14 * r * r; //return the area of a circle }
/*
* add a wrapper function below, helping a user to get the value of the string str
* from outside of this class
*/
/*
* add a wrapper function below, helping a user to access the function funB()
* from outside of this class
*/
/*
* add a destructor below that will print a message: "this is from the destructor";
*/
}
public class ExampleTest
{
public static void Main(String[] args)
{
//create an instance of the class Example here
//call all methods of the class Example that can be accessed here
}
}
Explanation / Answer
using System;
public class Example
{
private string str = "hello";
public static void funA() { Console.WriteLine("this is from a static function funA()"); }
public Example(string str)
{
//initialize the field str here
this.str = str;
}
private double funB(int r) { return 3.14 * r * r;} //return the area of a circle
/*
* add a wrapper function below, helping a user to get the value of the string str
* from outside of this class
*/
public string getStr()
{
return str;
}
/*
* add a wrapper function below, helping a user to access the function funB()
* from outside of this class
*/
public double getFunB(int radius)
{
double r = funB(radius);
return r;
}
/*
* add a destructor below that will print a message: "this is from the destructor";
*/
~Example()
{
Console.WriteLine("this is from the destructor");
}
}
public class ExampleTest
{
public static void Main(String[] args)
{
//create an instance of the class Example here
Example ex = new Example("hi");
//call all methods of the class Example that can be accessed here
Console.WriteLine(ex.getStr());
Console.WriteLine("Area of circle with radius 4 is {0:00} square units",ex.getFunB(4));
}
}
Output:
hi
Area of circle with radius 4 is 50 square units
this is from the destructor
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.