Using C#, Create class Rectangle. * The class has attributes length and width, e
ID: 3882441 • Letter: U
Question
Using C#, Create class Rectangle. * The class has attributes length and width, each of which defaults to 1. * One instance constructor that initializes the attributes length and width when an instance is created * It has several methods as described below: methodA - this is a static method that can only be accessed inside of the class Rectangle. It calculates the Area of the rectangle. methodB - this is a static method that can be accessed from another class. It calculates the Area of the rectangle. methodC - this is an instance method that can only be accessed inside of the class Rectangle. It calculates the Area of the rectangle. methodD - this is an instance method that can be accessed from another class. It calculates the Area of the rectangle. * in the Main method, each method above is called * Write class testRectangle to call the methods in class Rectangle that can be accessed by class testRectangle.
Explanation / Answer
//Below is the c # code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProjectRectangle
{
class Rectangle
{
static double length = 1;
static double width = 1;
Rectangle(double len, double wid)
{
length = len;
width = wid;
}
//this is a static method that can only be accessed inside of the class Rectangle.
//It calculates the Area of the rectangle
private static double methodA()
{
return length * width;
}
//this is a static method that can be accessed from another class.
//It calculates the Area of the rectangle.
public static double methodB()
{
return length * width;
}
//this is an instance method that can only be accessed inside of the class Rectangle.
//It calculates the Area of the rectangle
private double methodC()
{
return length * width;
}
//this is an instance method that can be accessed from another class.
//It calculates the Area of the rectangle. *
public double methodD()
{
return length * width;
}
static void Main(string[] args)
{
Rectangle rect = new Rectangle(11, 15);
Console.WriteLine("Calculate Area of the rectangle from static method is " + methodA());
Console.WriteLine("Calculate Area of the rectangle using instance method is " + rect.methodC());
Console.ReadLine();
}
}
}
OUTPUT:
Calculate Area of the rectangle from static method is 165
Calculate Area of the rectangle using instance method is 165
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.