Answer to Question #166915 in C# for Habib

Question #166915

Create a class "House", with an attribute "area", a constructor that sets its value and a method "ShowData" to display "I am a house, my area is 200 m2" (instead of 200, it will show the real surface). Include getters an setters for the area, too. The "House" will contain a door. Each door will have an attribute "color" (a string), and a method "ShowData" wich will display "I am a door, my color is brown" (or whatever

color it really is). Include a getter and a setter. Also, create a "GetDoor" in the house. A "SmallApartment" is a subclass of House, with a preset area of 50 m2.

Also create a class Person, with a name (string). Each person will have a house. The method "ShowData" for a person will display his/her name, show the data of his/her house and the data of the door of that house. Write a Main to create a SmallApartment, a person to live in it, and to show the data of the person.


1
Expert's answer
2021-02-27T11:55:11-0500
using System;

static class Program
{
    static void Main(string[] args)
    {
        SmallApartment apartment = new SmallApartment();
        Person person = new Person("Jhon");
        apartment.door = new House.Door("black");
        person.house = apartment;
        person.ShowData();
    }
}

public class House
{
    public int Area { get; set; }
    public Door door;

    public House(int area = 200)
    {
        Area = area;
    }

    public void ShowData()
    {
        Console.WriteLine($"I am a house, my area is {Area} m2");
    }

    public Door GetDoor()
    {
        return door;
    }

    public class Door
    {
        public string Color { get; set; }

        public Door(string color = "brown")
        {
            Color = color;
        }

        public void ShowData()
        {
            Console.WriteLine($"I am a door, my color is {Color}");
        }
    }
}

public class SmallApartment : House
{
    public SmallApartment(int area = 50) : base(area)
    {
    }
}

public class Person
{
    private string name;
    public House house { get; set; }

    public Person(string name)
    {
        this.name = name;
    }

    public void ShowData()
    {
        Console.WriteLine($"A person whose name is {name}");
        Console.WriteLine("Data of house:");
        house.ShowData();
        Console.WriteLine("Data of door:");
        house.GetDoor().ShowData();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS