c# help... Create a RangedInteger class. This class will need to keep track of a
ID: 3887602 • Letter: C
Question
c# help... 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.
given the following 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(" Press enter to exit.");
Console.ReadLine();
}
}
}
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.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RangeRestrictedInteger
{
public class RangedInteger
{
public int min,max,value;
public RangedInteger(int min, int max)
{
min = min;
max = max;
}
public int Value
{
get
{
if( value < min ){
return min; }
elseif(value > max){
return max; }
elseif(value <= max && value value >=0){
return value; }
}
set
{
this.value = value;
}
}
}
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(" Press enter to exit.");
Console.ReadLine();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.