Written in C# Console Application - Visual Studio You are to create a House with
ID: 3870773 • Letter: W
Question
Written in C# Console Application - Visual Studio
You are to create a House with 2 rooms in the house. The following is an example of C++ code for the basic classes: **in C#, it may be written differently**
class Room
{
private:
double L;
double W;
public:
//functions go here.
}
class House
{
private:
Room room1;
Room room2;
public:
//functions go here
}
The code above is C++ version. In C#, you must make the variables private. Look up in the book if you have difficulty with this assignment and dont forget to use the "new" command. Basic Example: House h = new House();
*Make sure each class is in its own file with the correct functions and data types*
The program will basically ask the user to input a L and W for a room, then ask for L and W for the other room. It will then calculate which of the two rooms is bigger. It should assign the bigger room to the parents and smaller room to children. State: "parents get room #_ of size __", "children get room #_ of size __".
Explanation / Answer
Create a console application in visual studio as follows
Start Visual Studio.
On the menu bar, choose File, New, Project.
The New Project dialog box opens.
Expand Installed, expand Templates, expand Visual C#, and then choose Console Application.
In the Name box, specify a name for your project, and then choose the OK button.
The new project appears in Solution Explorer.
Now add 2 more .cs class files
House.cs and Room.cs
In Room.cs create 2 private variables which hold length and breadth.
Add a method(AddRoomDimensions) which will assign data to length and width to the room, also add 2 more methods to get dimensions(GetRoomDimensions) and for calculating room size(CalculateRoomSize)
In House.cs create 2 instances of rooms and initialize them in constructor. Now create 3 methods as follows:
SetRoomOneDimensions() : This is to set room one dimensions
SetRoomTwoDimensions() : This is to set room 2 dimensions
AllocateRooms(): This will assign rooms to parent and child based on room size
In Program.cs, Initially create an object to house. Now read respective room sizes from enduser through console and set dimensions to the room. Once we read inputs from user, we can call AllocateRooms() on house object which will allocate respective rooms.
I am attaching code snippets:
program.cs
=========================================
using HouseApplication;
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
// Initailly Create a new house using new keyword
House house = new House();
//Reading room 1 dimensions
Console.WriteLine("Enter dimensions for Room1:");
Console.WriteLine("Room 1 Length:");
String strRoom1Length = Console.ReadLine();
Double room1Length;
bool isValidRoom1Length = Double.TryParse(strRoom1Length,out room1Length);
// This is to check if a user entered valid room1 length
if (!isValidRoom1Length)
{
Console.WriteLine("Invalid room1 length");
}
Console.WriteLine("Room 1 Width:");
String strRoom1Width = Console.ReadLine();
Double room1Width;
bool isValidRoom1Width = Double.TryParse(strRoom1Width, out room1Width);
// This is to check if a user entered valid room2 width
if (!isValidRoom1Width)
{
Console.WriteLine("Invalid room1 width");
}
house.SetRoomOneDimensions(room1Length, room1Width);
//Reading room 2 dimensions
Console.WriteLine("Enter dimensions for Room2:");
Console.WriteLine("Room 2 Length:");
String strRoom2Length = Console.ReadLine();
Double room2Length;
bool isValidRoom2Length = Double.TryParse(strRoom2Length, out room2Length);
// This is to check if a user entered valid room2 length
if (!isValidRoom2Length)
{
Console.WriteLine("Invalid room2 length");
}
Console.WriteLine("Room 2 Width:");
String strRoom2Width = Console.ReadLine();
Double room2Width;
bool isValidRoom2Width = Double.TryParse(strRoom2Width, out room2Width);
// This is to check if a user entered valid room2 width
if (!isValidRoom2Width)
{
Console.WriteLine("Invalid room2 width");
}
house.SetRoomTwoDimensions(room2Length, room2Width);
//Allocate Room
house.AllocateRooms();
Console.WriteLine("Press any key to exit....");
Console.ReadKey();
}
}
House.cs
==================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HouseApplication
{
public class House
{
private Room room1;
private Room room2;
public House()
{
room1 = new Room();
room2 = new Room();
}
public void SetRoomOneDimensions(double length, double width)
{
this.room1.AddRoomDimensions(length, width);
}
public void SetRoomTwoDimensions(double length, double width)
{
this.room2.AddRoomDimensions(length, width);
}
public void AllocateRooms()
{
double room1Size = this.room1.CalculateRoomSize();
double room2Size = this.room2.CalculateRoomSize();
if(room1Size > room2Size)
{
Console.WriteLine("Parents get room #1 of size "+this.room1.GetRoomDimensions()+"=" + room1Size + ", children get room #2 of size " + this.room2.GetRoomDimensions() + "=" + room2Size);
}
else
{
Console.WriteLine("Parents get room #2 of size " + this.room2.GetRoomDimensions() + "=" + room2Size + ", children get room #1 of size " + this.room1.GetRoomDimensions() + "=" + room1Size);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HouseApplication
{
public class Room
{
private double L;
private double W;
public void AddRoomDimensions(double length,double width)
{
this.L = length;
this.W = width;
}
public String GetRoomDimensions()
{
return "" + this.L + "*" + this.W;
}
public double CalculateRoomSize()
{
double totalRoomSize = this.L * this.W;
return totalRoomSize;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.