Patrick B. answered 05/04/21
Math and computer tutor/teacher
import java.util.Scanner;
class Nagm
{
class EnumWeight
{
protected long enumVal;
protected double weight;
public EnumWeight ( long longEnumVal, double dblWeight) { enumVal=longEnumVal; weight = dblWeight; }
public long GetEnumValue() { return(enumVal); }
public double GetWeight() { return(weight); }
}
long GetNumValue (EnumWeight A[], double weight)
{
long longIntNumReturn = 0;
int iIndexPos;
int N = A.length;
double firstWeight=A[0].GetWeight();
double lastWeight=A[N-1].GetWeight();
//linear search
if (weight > firstWeight && weight <= lastWeight)
{
for (iIndexPos=0; iIndexPos<A.length; iIndexPos++)
{
if (A[iIndexPos].GetWeight() > weight)
{
break;
}
}
EnumWeight below = A[iIndexPos-1];
EnumWeight above = A[iIndexPos];
double aboveDiff = Math.abs(above.GetWeight()-weight);
double belowDiff = Math.abs(below.GetWeight()-weight);
longIntNumReturn = ( aboveDiff > belowDiff) ? below.GetEnumValue() : above.GetEnumValue();
}
else
{
if (weight > lastWeight)
{
longIntNumReturn = N-1;
}
}
return(longIntNumReturn);
}
public void Go()
{
Scanner scanner = new Scanner(System.in);
EnumWeight A[] = new EnumWeight[18];
A[0] = new EnumWeight(1,24);
A[1] = new EnumWeight(2,32);
A[2] = new EnumWeight(3,36);
A[4] = new EnumWeight(4,42);
A[5] = new EnumWeight(5,48);
A[6] = new EnumWeight(6,52);
A[7] = new EnumWeight(7,56);
A[8] = new EnumWeight(8,60);
A[9] = new EnumWeight(9,64);
A[10] = new EnumWeight(10,72);
A[11] = new EnumWeight(11,76);
A[12] = new EnumWeight(12,80);
A[13] = new EnumWeight(13,84);
A[14] = new EnumWeight(14,88);
A[15] = new EnumWeight(15,92);
A[16] = new EnumWeight(16,96);
A[17] = new EnumWeight(18,100);
System.out.print(" Please input the weight :> ");
double weight = scanner.nextDouble();
System.out.println(" enum value for weight " + weight + " is " + GetNumValue(A,weight));
}
public static void main(String args[])
{
Nagm x = new Nagm();
x.Go();
}
}