Hi
I'm doing a school work in a console application, I'm not sure if I maybe misunderstand the question. Here it goes: Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both lenght and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0.
How can I get the read-only properties in here?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Classtest
{
class Rectangle
{
private double length;
private double width;
private double sumOfArea;
private double sumOfPerimeter;
public Rectangle(double theLength, double theWidth)
{
Length = theLength;
Width = theWidth;
}
public double Length
{
get { return length;}
private set
{
if (value >= 0.0 && value <= 20.0)
length = value;
else
{
Console.WriteLine("Invalid numbers {0}", value);
value = 1;
}
}
}
public double Width
{
get { return width;}
private set
{
if (value >= 0.0 && value <= 20.0)
width = value;
else
{
Console.WriteLine("Invalid numbers {0}", value);
value = 1;
}
}
}
public void Calculating()
{
sumOfArea = length * width;
sumOfPerimeter = (length * 2) + (width * 2);
Console.WriteLine();
Console.WriteLine("The area: {0}", sumOfArea);
Console.WriteLine("The perimeter: {0}", sumOfPerimeter);
}
public class TestRectangle
{
static void Main()
{
Console.Write("Enter the length: ");
double length = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the width: ");
double width = Convert.ToDouble(Console.ReadLine());
Rectangle rect = new Rectangle(length, width);
rect.Calculating();
Console.ReadKey();
}
}
}
}
Thank you for your reply.

1 answers
It sounds to me as though you're expected to do this:
answered one year ago by:
17279
60
Thank you very much Vulpes, I believe that is exactly what they meant.