Create a C# console application for this question.You modified class Height in P
ID: 3940172 • Letter: C
Question
Create a C# console application for this question.You modified class Height in Problem 1. You are asked to copy the code and add more in this program. Write four constructors for class Height:
Version 1: Take 2 integer arguments and use them to initialize foot and inch
Version 2: Take 1 integer argument and use it to initialize foot. Set inch to 0.
Version 3: Take no arguments. Initialize foot and inchto 0.
Version 4: Take another Height object as argument. Use foot and inch of that Height object to initialize this object’sfoot and inch.
[note: do not directly access the instance variables. Always go through the properties.]
Use the following code to test your modified class Height.
publicclassHeightTest
{
staticvoid Main(string[] args)
{
Height myHeight1 = newHeight(5, 7);
Console.WriteLine("My height 1: {0} ft. {1} in.", myHeight1.Foot, myHeight1.Inch);
Height myHeight2 = newHeight(5);
Console.WriteLine("My height 2: {0} ft. {1} in.", myHeight2.Foot, myHeight2.Inch);
Height myHeight3 = newHeight();
Console.WriteLine("My height 3: {0} ft. {1} in.", myHeight3.Foot, myHeight3.Inch);
Height myHeight4 = newHeight(myHeight1);
Console.WriteLine("My height 4: {0} ft. {1} in.", myHeight4.Foot, myHeight4.Inch);
}
}
The following is the expected output.
My height 1: 5 ft. 7 in.
My height 2: 5 ft. 0 in.
My height 3: 0 ft. 0 in.
My height 4: 5 ft. 7 in.
Press any key to continue . . .
Explanation / Answer
using System;
class Height
{
private int foot;
private int inch;
public Height(int foot,int inch) //constructor with 2 arguments
{
this.foot = foot;
this.inch = inch;
}
public Height(int foot) //constructor with 1 parameter
{
this.foot = foot;
this.inch = 0;
}
public Height() //constructor with no parameter
{
this.foot = 0;
this.inch = 0;
}
public Height(Height h) //constructor with Height class object as parameter
{
this.foot = h.foot;
this.inch = h.inch;
}
public int Foot // set and get properties for foot
{
get
{
return foot;
}
set
{
foot = value;
}
}
public int Inch //set and get properties for inch
{
get
{
return inch;
}
set
{
inch = value;
}
}
}
public class HeightTest
{
static void Main(string[] args)
{
Height myHeight1 = new Height(5, 7);
Console.WriteLine("My height 1: {0} ft. {1} in.", myHeight1.Foot, myHeight1.Inch);
Height myHeight2 = new Height(5);
Console.WriteLine("My height 2: {0} ft. {1} in.", myHeight2.Foot, myHeight2.Inch);
Height myHeight3 = new Height();
Console.WriteLine("My height 3: {0} ft. {1} in.", myHeight3.Foot, myHeight3.Inch);
Height myHeight4 = new Height(myHeight1);
Console.WriteLine("My height 4: {0} ft. {1} in.", myHeight4.Foot, myHeight4.Inch);
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}
}
output: