Sort three variables: Difference between revisions
→{{header|Java}}: New version that compiles without warnings.
(Found incorrectness.) |
(→{{header|Java}}: New version that compiles without warnings.) |
||
Line 911:
=={{header|Java}}==
{{
<lang Java>public class SortThreeVariables {▼
private static class Triple<T extends Comparable<T>> {▼
▲public class SortThreeVariables {
▲ private static class Triple<T> {
T first, second, third;
Triple(T x, T y, T z) {
first = x;
second = y;
third = z;
}
Triple<T> sortInPlace()
{
T s;
if( first.compareTo( second ) > 0 ){ s = second; second = first; first = s; }
if( first.compareTo( third ) > 0 ){ s = third; third = first; first = s; }
if( second.compareTo( third ) > 0 ){ s = third; third = second; second = s; }
return this;
}
}
private static <T extends Comparable<T>> Triple<T> sortThree(T x, T y, T z) {
}
private static <T> void printThree(T x, T y, T z) {
System.out.printf("x=%s%ny=%s%nz=%s%n%n", x, y, z);
}
public static void main(String[] args) {
String x = "lions, and tigers, and";
Line 945 ⟶ 948:
z = t.third;
printThree(x, y, z);
Integer x2 = 77444;
Integer y2 = -12;
Line 954 ⟶ 957:
z2 = t2.third;
printThree(x2, y2, z2);
Double x3 = 174.5;
Double y3 = -62.5;
|