ConversionTool.java 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. public class ConversionTool {
  2. private static final float CM_PER_INCH = 2.54f;
  3. private static final float FT_PER_M = 3.28084f;
  4. private static final float KM_PER_MILE = 1.60934f;
  5. public static void main(String[] args){}
  6. public static float CentimetersToInches(float centimeters){
  7. if (checkNegative(centimeters)) return 0f;
  8. return centimeters / CM_PER_INCH;
  9. }
  10. public static float InchesToCentimeters(float inches){
  11. if (checkNegative(inches)) return 0f;
  12. return inches * CM_PER_INCH;
  13. }
  14. public static float FeetToMeters(float feet){
  15. if (checkNegative(feet)) return 0f;
  16. return feet / FT_PER_M;
  17. }
  18. public static float MetersToFeet(float meters){
  19. if (checkNegative(meters)) return 0f;
  20. return meters * FT_PER_M;
  21. }
  22. public static float CelsiusToFahrenheit(float celsius){
  23. if (checkNegative(celsius)) return 0f;
  24. return celsius * 1.8f + 32.0f;
  25. }
  26. public static float FahrenheitToCelsius(float fahrenheit){
  27. if (checkNegative(fahrenheit)) return 0f;
  28. return (fahrenheit - 32) / 1.8f;
  29. }
  30. public static float MphToKph(float mph){
  31. if (checkNegative(mph)) return 0f;
  32. return mph * KM_PER_MILE;
  33. }
  34. public static float KphToMph(float kph){
  35. if (checkNegative(kph)) return 0f;
  36. return kph / KM_PER_MILE;
  37. }
  38. private static boolean checkNegative(float value) {
  39. if (value < 0) {
  40. return true;
  41. }
  42. return false;
  43. }
  44. }