I am putting 20 objects in an array then I am using a bubble sort (not the most efficient, I know) to organize the set from highest to lowest. I am wondering if there is a way to do this without using sets (mutators)? Code is below.

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RangeChecker
{
class RangeChecker
{
Random random = new Random();
private int x =0;
private int y = 0;
private int under = 0;
private int between = 0;
private int over = 0;

public RangeChecker()
{

x = random.Next(100);
y = random.Next(100);
while(x>y)
x = random.Next(100);

}

public RangeChecker( int x, int y)
{

x = this.x;
y = this.y;

}

// Precondition-none

// Postcondtion-returns value of x
public int getX()
{
return x;

}

// Precondition-none

// Postcondtion-returns value of y
public int getY()
{
return y;
}

// Precondition-none

// Postcondtion-retruns if left value is smaller then right value

public static bool operator <(RangeChecker op1, RangeChecker op2)
{
if ((op1.x < op2.x) && (op1.y < op2.y))
return true;
else if ((op1.x + op1.y) < (op2.x + op2.y))
return true;
else
return false;
}

// Precondition-none

// Postcondtion-retruns if left value is greater then right value
public static bool operator >(RangeChecker op1, RangeChecker op2)
{
if ((op1.x > op2.x) && (op1.y > op2.y))
return true;
else if ((op1.x + op1.y) > (op2.x + op2.y))
return true;
else
return false;
}
// Precondition-none

// Postcondtion-x has new value
public void setX(int x)
{

x = this.x;

}
// Precondition-none

// Postcondtion-y has new value
public void setY(int y)
{
y = this.y;
}
public void inRange(int z)
{
if ((x <= z) && (z <= y))
{
between++;
Console.WriteLine(" between");
}

}
public void underRange(int z)
{
if (z < x)
{
under++;
Console.WriteLine(" under");
}

}
public void overRange(int z)
{
if (y < z)
{
over++;
Console.WriteLine(" over");
}

}
public int getUnder()
{
return under;
}
}
}
``````

Code:

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RangeChecker
{
class Program
{
static void initialize(RangeChecker[] rangeChecker)
{
for (int i = 0; i < 20; i++)
{
rangeChecker[i] = new RangeChecker();

}
}

static void order(RangeChecker[] rangeChecker, int temp)
{
for (int i = 0; i < 19; i++)
{

RangeChecker a = new RangeChecker(rangeChecker[i].getX(), rangeChecker[i].getY());
RangeChecker b = new RangeChecker(rangeChecker[i + 1].getX(), rangeChecker[i + 1].getY());

if (a > b)
{
temp = rangeChecker[i].getX();
rangeChecker[i].setX(rangeChecker[i + 1].getX());
rangeChecker[i + 1].setX(temp);
temp = rangeChecker[i].getY();
rangeChecker[i].setY(rangeChecker[i + 1].getY());
rangeChecker[i + 1].setY(temp);
}
}

}

{
Random random = new Random();
for (int i = 0; i < 20; i++)
{
int z = random.Next(100);
Console.WriteLine(rangeChecker[i].getX() + " " + rangeChecker[i].getY() + " " + z);
rangeChecker[i].inRange(z);
rangeChecker[i].underRange(z);
rangeChecker[i].overRange(z);
}

}
static void display(RangeChecker[] rangeChecker)
{
int under = 0;
int xValue = 0;
int yValue = 0;
for (int i = 0; i < 19; i++)
{

if (rangeChecker[i].getUnder() < rangeChecker[i + 1].getUnder())
{
under = rangeChecker[i + 1].getUnder();
xValue = rangeChecker[i + 1].getX();
yValue = rangeChecker[i + 1].getY();
}

}
Console.WriteLine(under + " " + xValue + " " + yValue);

}

public enum Options : byte
{
newZ = 1,
display,
Exit
}
{
Console.WriteLine("Enter newZ to try new integers");
Console.WriteLine("Enter display to see which Range Checker has the most Z's under the range");
Console.WriteLine("Enter Exit to exit");
}
static void Main(string[] args)
{
RangeChecker[] rangeChecker = new RangeChecker[20];
Random random = new Random();
int temp = 0;
initialize(rangeChecker);
order(rangeChecker, temp);

for (; ; )
{
Options options = (Options)Enum.Parse(typeof(Options), volString);
switch (options)
{
case Options.newZ:
break;
case Options.display:
display(rangeChecker);
break;
case Options.Exit:
Console.WriteLine("Goodbye");
System.Environment.Exit(1);
break;
default:
break;

}
}
}
}
}
``````

I'm not sure what you are asking here. You do things in a very odd way and you've hard coded some of the values that shouldn't be. Can you explain more about what you do/don't want to do? And you have an error in Display.

## All 4 Replies

I'm not sure what you are asking here. You do things in a very odd way and you've hard coded some of the values that shouldn't be. Can you explain more about what you do/don't want to do? And you have an error in Display.

What is my error is Display? The program runs.

Could you explain what a Z is?
Your code seems a direct translation from C++.

Z is a random integer that is tested to see where it falls in the range
of x and y. The reason why it looks like it was translated from C++ is
because all the previous classes I took were in C++.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.