Please help me with this program. I can't figure it out. This is my previous work and I've been asked to modify it with the objectives below.

Given Script:

Main (Class1.cs)
using System;

namespace Assignment1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Dwelling x = new Dwelling();

x.SetRooms( 5 );
Console.WriteLine("The rooms of x is: " + x.GetRooms() );

x.SetRooms( -4 );
Console.WriteLine("The rooms of x is: " + x.GetRooms() );

x.SetPrice( 200000 );
Console.WriteLine("The rooms of x is: " + x.GetPrice() );

x.SetPrice( -4 );
Console.WriteLine("The rooms of x is: " + x.GetPrice() );


}
}
}

--------------------------------------…

Dwelling.cs

using System;

namespace Assignment1
{
public class Dwelling
{
private int rooms;
private double price;

public int GetRooms()
{
return rooms;
}

public double GetPrice()
{
return price;
}

public void SetRooms(int NewRooms)
{
if (NewRooms >= 1 && NewRooms <= 100)
rooms = NewRooms;
else
rooms = 1;
}
public void SetPrice(double NewPrice)
{
if (NewPrice >= 10000)
price = NewPrice;
else
price = 10000;
}
}
}

--------------------------------------…

Objective:

Modify the class Dwelling as follows:

• Remove the setter/getter methods from the class. Class design
dictates that either we use setter/getter methods OR properties, but not both. In other programming languages (such as C++ and Java), setter/getter methods are used to modify private members. In C#, however, properties are the preferred method.

• Since every real estate property contains an address, create a new
private member address. Also create a property which checks to make sure that the address is not an empty string. If empty, this member is set to the string "N/A".
• Add a default constructor which sets the price and rooms members
to 1 and 1000 respectively, and assigns the string "N/A" to the address.

• Add an overloaded constructor which accepts 3 arguments, one to
initialize each of the 3 private members of the class.

• Add a copy constructor to the class. With this constructor it is
possible to create one Dwelling object and initialize it with another existing one.

Testing the class you made is extremely important. You must use multiple test data, exposing the class to all (boundary) cases. The test data you print for the instructor should clearly reflect this. The following illustrates how the class is supposed to be tested:

The client code you devise to test the class should create a Dwelling object
and attempt to set its rooms member to all possible (boundary) cases. In
other words, since the rooms member can only be between 1 and 100, your
code should test the rooms member by setting it to a negative value, followed
by one, followed by a value between 1 and 100, then 100, and finally a value
larger than 100. Each time the rooms are set to one of the values, the contents of the object to which x point should be printed on the screen to verify that filtering code has worked.

The same testing procedure should be performed on the price and name.

Also, create a new Dwelling object using the overloaded constructor and print the contents of that object. Next, create a new Dwelling as a copy of the previous object utilizing the copy constructor. Print the contents of the second object to verify that its private members are a copy of the original.

Recommended Answers

All 2 Replies

Please use CODE tags if you post code!
Here is an example of property syntax:

class Person
{
    private string _Name; //private field
    
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }
}

Use like this:

Person.Name = "John"; //sets the private field _Name
Console.WriteLine(Person.Name); //gets the private field and writes it to the console

Good luck!

Can you be more specific about what you are stuck with? ddanbe has given you the code for properties if you need advise on any of the other requirements let us know, but you need to make an attempt and we can help you trouble shoot specifics.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.