Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following program defines class Height. This class has two publicly accessib

ID: 3764588 • Letter: T

Question

The following program defines class Height. This class has two publicly accessible instance variables foot and inch. Since there is no control on access to these variables, outside code can store any value in them. For example, we can store -8 in foot and 14 in inch as shown in the program:

using System;

public class Height

{

    public int foot;

    public int inch;

}

public class HeightTest

{

    static void Main(string[] args)

    {

        Height myHeight1 = new Height();

        myHeight1.foot = 5;

        myHeight1.inch = 7;

        Console.WriteLine("My height 1: {0} ft. {1} in.", myHeight1.foot, myHeight1.inch);

        Height myHeight2 = new Height();

        myHeight2.foot = -8;

        myHeight2.inch = 14;

        Console.WriteLine("My height 2: {0} ft. {1} in.", myHeight2.foot, myHeight2.inch);

    }

}

Modify the program above by making foot and inch private. Add publicly accessible properties to get and set these instance variables. No negative value can be stored in both of these variables. Also, no value 12 or higher can be stored in inch. Also, modify the Main method of the HightTest class to access the properties instead of the instance variables.

using System;

public class Height

{

    public int foot;

    public int inch;

}

public class HeightTest

{

    static void Main(string[] args)

    {

        Height myHeight1 = new Height();

        myHeight1.foot = 5;

        myHeight1.inch = 7;

        Console.WriteLine("My height 1: {0} ft. {1} in.", myHeight1.foot, myHeight1.inch);

        Height myHeight2 = new Height();

        myHeight2.foot = -8;

        myHeight2.inch = 14;

        Console.WriteLine("My height 2: {0} ft. {1} in.", myHeight2.foot, myHeight2.inch);

    }

}

Explanation / Answer

public class Height
    {

        private int foot;

        private int inch;

        public int getFoot()
        {
            return foot;
        }

        public void setFoot(int foot)
        {
            if(foot >0)
                this.foot = foot;
        }

        public int getInch()
        {
             return inch;
        }

        public void setInch(int inch)
        {
            if (inch < 12)
                this.inch = inch;
        }
    }



    public class HeightTest
    {

        static void Main(string[] args)
        {

            Height myHeight1 = new Height();

            myHeight1.setFoot(5);

            myHeight1.setInch(7);

            Console.WriteLine("My height 1: {0} ft. {1} in.", myHeight1.getFoot(), myHeight1.getInch());



            Height myHeight2 = new Height();

             myHeight2.setFoot(-8);

            myHeight2.setInch(14);

            Console.WriteLine("My height 2: {0} ft. {1} in.", myHeight2.getFoot(), myHeight2.getInch());
            Console.ReadKey();

        }

    }

My height 1: 5 ft. 7 in.
My height 2: 0 ft. 0 in.