-7

I need the highest value among North, south, east and west and in result, i just have to return the name north, south, east or west. Highest value have to be obtained by comparing all the values of north, south, east and west.

Below is my code snippet.

public String startpgm(String x){
    String result=null;
    double priority = 0;
    double North    = 1;
    double South    = 3;    
    double East     = 4;
    double West     = 5;

    System.out.println("Priority:"+Math.max(North, South)); //I am getting the max value, what i need is North or South!!
    priority= Math.max(North, South);
    result = Double.toString(priority);
    return result ;
}
jasim
  • 459
  • 1
  • 6
  • 24

7 Answers7

3

As cardinal points are a well known set of values, use an enum :

public enum CardinalPoint {
  NORTH,
  SOUTH,
  EAST,
  WEST
}

Then, here is an example of max method :

public static CardinalPoint max(CardinalPoint cp1, CardinalPoint cp2) {
  return cp1.ordinal() > cp2.ordinal() ? cp1 : cp2;
}

Use like this :

public static void main(String[] args) {
  System.out.println(max(CardinalPoint.NORTH, CardinalPoint.SOUTH));
}

Result :

NORTH
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
1

Use hashmap, or enum. this is better approach of the algorithm. Anyway if you want to to use variables:

class Constants {

    public static final int priority = 0;
    public static final int North    = 1;
    public static final int South    = 3;    
    public static final int East     = 4;
    public static final int West     = 5;
  public static String getConstantName(int constVal) {
    if (constantNames == null) {
      Map<Integer, String> cNames = new HashMap<Integer, String>()
      for (Field field : MyClass.class.getDeclaredFields()){
        if ((field.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) != 0) {
            && int.class == field.getType()){
          // only record final static int fields
          cNames.put((Integer)field.get(null), field.getName());
        }
      }
      constNames = cNames;
    }
    return constantNames.get(constVal);
  }
public String startpgm(String x){
    String result=null;

    System.out.println("Priority:"+Math.max(North, South)); //I am getting the max value, what i need is North or South!!
    priority= Math.max(North, South);
    result = Integer.toString(priority);
    return Constants.getConstantName(result) ;
}
}

Thanks to:

https://stackoverflow.com/a/6838811/1979882

Community
  • 1
  • 1
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
1

Store the names in a Map and then retrieve the name for key value.

String result=null;
double priority = 0;
double North    = 1;
double South    = 3;    
double East     = 4;
double West     = 5;

Map<Double, String> names = new HashMap();
names.put(North, "North");
names.put(South, "South");
names.put(East, "East");
names.put(West, "West");

System.out.println("Priority:"+Math.max(North, South));
priority= Math.max(North, South);
result = names.get(priority);

return result ;
Héctor
  • 24,444
  • 35
  • 132
  • 243
1

This is assuming that there are only 4 variables to track.

String[] sValue = {"North","South","East","West"};
int[]    iValue = {1,2,3,4,5}

index = Math.max(North, South);
System.out.println("Priority:"+sValue[index]); 

Note: This solution does not scale well.

sean
  • 717
  • 2
  • 9
  • 23
1

If you need named entities for the directions, as you seem to, you might want to use an enum.

enum Direction { North, South, East, West }

You can associate each direction with a number using an EnumMap.

Map<Direction, Double> dirMap = new EnumMap<>(Direction.class);
dirMap.put(Direction.North, 1.0);
dirMap.put(Direction.South, 3.0);
dirMap.put(Direction.East, 4.0);
dirMap.put(Direction.West, 5.0);

And you could find the maximum of North and South something like this:

private static Direction findMax(List<Direction> directions, Map<Direction, Double> dirMap) {
    Direction best = null;
    Double bestValue = null;
    for (Direction d : directions) {
        double dValue = dirMap.get(d);
        if (bestValue==null || bestValue < dValue) {
            best = d;
            bestValue = dValue;
        }
    }
    return best;
}

...
System.out.println("max of north and south is "+findMax(Arrays.asList(Direction.North, Direction.South), dirMap));
khelwood
  • 55,782
  • 14
  • 81
  • 108
0

The way your code is written currently, you'd need to use reflection to get what you want. You should better use Java Enum Types (https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) which can give you both the numeric value and its string representation at the same time.

  • You cannot [reflect local variables](http://stackoverflow.com/questions/6816951/can-i-get-information-about-the-local-variables-using-java-reflection) in Java. The enum approach is the correct one. – Seelenvirtuose Jan 12 '16 at 09:38
0

You can use an enum with a private constructor and now you can asign a diffetent double value to each cardinal point, but you need a public method that get that value. Now you only need a method for find the name of the cardinal point, I use a static method in the enum.

public class Startpgm {

public static void main(String[] args) {


    System.out.println("priority: "+ 
            CardinalPoints.nameCardinalPoint(Math.max(CardinalPoints.NORTH.getPriority(), CardinalPoints.SOUTH.getPriority())));


}

enum CardinalPoints {
    NORTH(1d), SOUTH(3d), EAST(4d), WEST(5d);

    double priority;

    private CardinalPoints (double priority){
        this.priority= priority;
    }
    public double getPriority(){
        return priority;
    }
    public static String nameCardinalPoint( double priority){
        String nameCardinalPoint = "";
        for (CardinalPoints cardinalPoint : CardinalPoints.values()) {
            if(cardinalPoint.getPriority() == priority){
                nameCardinalPoint = cardinalPoint.name();
            }
        }
        return nameCardinalPoint;
    }
}

}