Make a Class that have following attributes and behaviors (set values to X & Y, get values of X & Y
and calculate area).
Loading
Make a Class that have following attributes and behaviors (set values to X & Y, get values of X & Y
and calculate area).
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal JoshiRank #377 · 6.7K reputationPosted Feb 24, 2024, 11:57 PM EST
Hello,
Please check out the below sample code to get the expected result.
public class Rectangle { // Attributes private double x; private double y; // Constructor public Rectangle(double x, double y) { this.x = x; this.y = y; } // Set values of X & Y public void SetValues(double x, double y) { this.x = x; this.y = y; } // Get values of X & Y public (double, double) GetValues() { return (x, y); } // Calculate area public double CalculateArea() { return x * y; } }Below is the code to create an instance of the class and call its behavior.
class Program { static void Main(string[] args) { Rectangle rectangle = new Rectangle(5, 10); // Get values of X & Y (double x, double y) = rectangle.GetValues(); Console.WriteLine($"X: {x}, Y: {y}"); // Calculate area double area = rectangle.CalculateArea(); Console.WriteLine($"Area: {area}"); // Set new values of X & Y rectangle.SetValues(8, 12); // Get values of X & Y (x, y) = rectangle.GetValues(); Console.WriteLine($"X: {x}, Y: {y}"); // Calculate area area = rectangle.CalculateArea(); Console.WriteLine($"Area: {area}"); } }Thanks
Vishal Joshi
Sam HobbsRank #107 · 20.5K reputationPosted Feb 24, 2024, 11:58 PM EST
How much of what your instructor taught you do you remember? Do you have a specific question? How much have you been able to do?
Abhishek YadavRank #217 · 11.3K reputationPosted Feb 27, 2024, 2:24 AM EST
public class Shape { private int x; private int y; public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } public double CalculateArea() { return x * y; } }