blah blah blah is here! blah blah » Close

up1down
link

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.

last answered one year ago

1 answers

up1down
link

It sounds to me as though you're expected to do this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Classtest
{
class Rectangle
{
private double length;
private double width;
// REMOVED sumOfArea and sumOfPerimeter fields

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);
length = 1; // CHANGE here
}
}
}
public double Width
{
get { return width;}

private set
{
if (value >= 0.0 && value <= 20.0)
width = value;
else
{
Console.WriteLine("Invalid numbers {0}", value);
width = 1; // CHANGE HERE
}
}
}

public double Area // ADDED this read-only property
{
get { return length * width; }
}

public double Perimeter // ADDED this read-only property
{
get { return (length + width) * 2.0; }
}

//REMOVED Calculating() method

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);
// REMOVED rect.Calculating()
// ADDED these two lines
Console.WriteLine("The area: {0}", rect.Area);
Console.WriteLine("The perimeter: {0}", rect.Perimeter);

Console.ReadKey();
}
}
}
}

Thank you very much Vulpes, I believe that is exactly what they meant.

Feedback