0

i have to write a program in JAVA using switch case statements.I need to check the following conditions using switch case statements.But i cannot compare values in switch case statements,How can i do this task.

if Avarage - AVG
AVG ≥ 3.50   “A Pass” 
3.49 ≥ AVG ≥ 3.00  “B Pass” 
2.99 ≥ AVG ≥ 2.50  “C Pass" 
2.49 ≥ AVG ≥ 2.00  “D Pass” 
2.00 ≥ AVG   “Fail” 

2 Answers2

2

You can use a series of if statements or you could use a TreeMap which implements the NavigableMap interface. It could look like:

TreeMap<Double, String> map = new TreeMap<>();
map.put(0d, "Fail");
map.put(2d, "D Pass");
map.put(2.5, "C Pass");
map.put(3d, "B Pass");
map.put(3.5, "A Pass");

System.out.println(map.floorEntry(1.9).getValue()); //Fail
System.out.println(map.floorEntry(2.49).getValue()); //D
System.out.println(map.floorEntry(2.5).getValue()); //C
System.out.println(map.floorEntry(3d).getValue()); //B
System.out.println(map.floorEntry(3.5).getValue()); //A

So back to your example, you could simply call return map.floorEntry(AVG).getValue();.

Performance-wise it will be very close, especially with only 5 different cases. I leave it to you to decide which is more readable.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • But i have to do it using Switch Case Statements it is a Question that ask for use Switch case statements.Can anyone help me ? – astrodileepa Jan 27 '14 at 14:32
  • 1
    @astrodileepa if AVG can be any doubles, this can't be written with a switch statement. If only certain values are authorised (say 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5) then it is possible. You need to give more information. – assylias Jan 27 '14 at 14:33
  • Yeah i know that we cannot use switch case statement to compare values .But this is a challenging question got.The rule is not to use if..else statements.I dont know how to handle this situation.This is very tough question. – astrodileepa Jan 27 '14 at 14:51
0

Switch uses a jump-table internally, that is why case statements can only test for exact and final values. You can imagine this like this:

result = jumpTable[key]; // switch pseudo-code

This is not what you'll find in the compiled code, but it is the same idea in general, so I use it to answer your question.

Any index that is not in this array cannot be accessed, so if you have an array of length 5 you cannot do something like:

result = jumpTable[2.3]; // impossible in Java

because this is just not defined and Java cannot magically figure out that you actually wanted to round down to 2. To archive that you need to write a function that can understand and convert intermediate values, for example a simple floor function

result = jumpTable[floor_int(2.3)]; // a solution for a specific case

would do the trick for some cases. It is obvious that the function used needs to actually fit your problem, so if 2.3 should point to index 1, you obviously need to use a different math behind the scene.

As assylias wrote, the method floorEntry of a TreeMap is a generalized version of what I wrote above and can be used to solve your problem.

The alternative is a standard if-else chain like this:

String getTestResult(final double avg) {
  if (avg >= 3.5) {
    return "A Pass";
  } else if (avg >= 3.0) {
    return "B Pass";
  } else if (avg >= 2.5) {
    return "C Pass";
  } else if (avg >= 2.0) {
    return "D Pass";
  } else {
    return "Fail";
  }
}

This uses the natural ordering of numbers, which states that (obviously) avg cannot be >= 3.5 and <= 3.5 at the same time.

For your case you should probably just write a function gradeToIndex, then use that in place of the floor_int function as in my example above, something like

index = (int) Math.round((avg - 2.0) * 2.0);
TwoThe
  • 13,879
  • 6
  • 30
  • 54