For this task you are asked to create a RangedInteger class. This class will need to keep track of a single value and ensure that it remains between a provided minimum or maximum, which are specified in RangedInteger's constructor. If the value is set to something outside those values it should be set to the minimum or maximum value, whichever is closer.

RangedInteger myInteger = new RangedInteger(0, 100);
myInteger.Value = 57;
Console.WriteLine("{0}", myInteger.Value); // Should be 57
myInteger.Value = 103;
Console.WriteLine("{0}", myInteger.Value); // Should be 100
myInteger.Value = -4;
Console.WriteLine("{0}", myInteger.Value); // Should be 0

The RangedInteger class will have one constructor and one property.

RangedInteger(int minimum, int maximum)
This creates a new RangedInteger with a minimum of minimum and a maximum of maximum

int Value
This is a property. It can be set to any integer, but if it is set to a value lower than the minimum then when you next read the value it should return the minimum instead. The same is true if the value is set to something greater than the maximum.

A simple Main() function is provided, although all it performs is a very basic test of functionality. You will want to add your own testing code. The Main() function is not tested by AMS- only the RangedInteger class is tested.

If you still have difficulties, here are some tips when writing programs for AMS:

Do not write your program directly into the text box. You should copy and paste the sample code into Visual Studio, write your program there, run it and test it to make sure it works, then paste it into AMS. You only get a limited number of attempts at each AMS exercise, so make sure they count.

Sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RangeRestrictedInteger
{
public class RangedInteger
{
// Add private variables here, as necessary
// ...

public RangedInteger(int min, int max)
{
// ...
}

public int Value
{
get
{
// ...
}
set
{
// ...
}
}
}
class Program
{
static void Main(string[] args)
{
RangedInteger myInteger = new RangedInteger(0, 100);
myInteger.Value = 57;
Console.WriteLine("{0}", myInteger.Value); // Should be 57
myInteger.Value = 103;
Console.WriteLine("{0}", myInteger.Value); // Should be 100
myInteger.Value = -4;
Console.WriteLine("{0}", myInteger.Value); // Should be 0

Console.WriteLine("nPress enter to exit.");
Console.ReadLine();
}
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.