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;
using System.Threading;
namespace RangeChecker
{
class Program
{
static void initialize(RangeChecker[] rangeChecker)
{
for (int i = 0; i < 20; i++)
{
Thread.Sleep(100);
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);
}
}
}
static void addZ(RangeChecker[] rangeChecker)
{
Random random = new Random();
for (int i = 0; i < 20; i++)
{
int z = random.Next(100);
Thread.Sleep(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
}
static void displayMenu()
{
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 (; ; )
{
displayMenu();
string volString = Console.ReadLine();
Options options = (Options)Enum.Parse(typeof(Options), volString);
switch (options)
{
case Options.newZ:
addZ(rangeChecker);
break;
case Options.display:
display(rangeChecker);
break;
case Options.Exit:
Console.WriteLine("Goodbye");
System.Environment.Exit(1);
break;
default:
Console.WriteLine("entry not found");
break;
}
}
}
}
}