Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. "ConvertTemperature.java" 118L, 3871C written
  2. aylward@sunfire [22:13:09] ~ $ javac ConvertTemperature.java
  3. ConvertTemperature.java:114: error: variable sourceString might not have been initialized
  4. System.out.println(temperature + " degrees " + sourceString + " = " + df.format(newTemp) + " degrees " + destString);
  5. ^
  6. ConvertTemperature.java:114: error: variable newTemp might not have been initialized
  7. System.out.println(temperature + " degrees " + sourceString + " = " + df.format(newTemp) + " degrees " + destString);
  8. ^
  9. 2 errors
  10. aylward@sunfire [22:13:14] ~ $ vim ConvertTemperature.java
  11. 1 // CS1020 (AY2014/5 Semester 2)
  12. 2 // Take-home Lab1 Ex1
  13. 3 // Name: aylward
  14. 4 // Matric. No.: A0124174L
  15. 5 // Lab group: 4
  16. 6 // Write the program description below.
  17. 7 // It is mandatory to write program description at the top of every
  18. 8 // program.
  19. 9 // Marks will be awarded for this in sit-in labs.
  20. 10
  21. 11 //this program will read a temperature and convert the value from one
  22. 12 //scale to another in two decimal places.
  23. 13
  24. 14 import java.util.Scanner;
  25. 15 import java.text.DecimalFormat;
  26. 16
  27. 17 public class ConvertTemperature {
  28. 18
  29. 19 public static void main(String[] args){
  30. 20
  31. 21 Scanner sc = new Scanner(System.in);
  32. 22
  33. 23 System.out.print("Enter temperature: ");
  34. 24 double temperature = sc.nextDouble();
  35. 25 printMenuSource();
  36. 26 int fromScale = sc.nextInt();
  37. 27 printMenuDest(fromScale);
  38. 28 int toScale = sc.nextInt();
  39. 29
  40. 30 convert(temperature, fromScale, toScale);
  41. 31 }
  42. 32
  43. 33 // Print menu to select source scale
  44. 34 private static void printMenuSource() {
  45. 35 System.out.println();
  46. 36 System.out.println("Choose source scale:");
  47. 37 System.out.println(" 1. Celsius");
  48. 38 System.out.println(" 2. Fahrenheit");
  49. 39 System.out.println(" 3. Kelvin");
  50. 40 System.out.print("Enter your choice: ");
  51. 41 }
  52. 42
  53. 43 //Print menu to select destination scale
  54. 44 private static void printMenuDest(int scale) {
  55. 45 System.out.println();
  56. 46 if (scale == 1) {
  57. 47 System.out.println();
  58. 48 System.out.println("Choose destination scale:");
  59. 49 System.out.println(" 1. Fahrenheit");
  60. 50 System.out.println(" 2. Kelvin");
  61. 51 System.out.print("Enter your choice: ");
  62. 52 } else if (scale == 2) {
  63. 53 System.out.println();
  64. 54 System.out.println("Choose destination scale:");
  65. 55 System.out.println(" 1. Celsius");
  66. 56 System.out.println(" 2. Kelvin");
  67. 57 System.out.print("Enter your choice: ");
  68. 58 } else if (scale == 3) {
  69. 59 System.out.println();
  70. 60 System.out.println("Choose destination scale:");
  71. 61 System.out.println(" 1. Celsius");
  72. 62 System.out.println(" 2. Fahrenheit");
  73. 63 System.out.print("Enter your choice: ");
  74. 64 } else {}
  75. 65 }
  76. 66
  77. 67 // Convert temperature from fromScale to toScale
  78. 68 private static void convert(double temperature, int fromScale,
  79. 69 int toScale) {
  80. 70
  81. 71 double newTemp;
  82. 72 String sourceString, destString;
  83. 73
  84. 74
  85. 75 if (fromScale == 1 && toScale == 1) {
  86. 76 newTemp = 1.8 * temperature + 32.0;
  87. 77 } else if (fromScale == 1 && toScale == 2) {
  88. 78 newTemp = temperature + 273.15;
  89. 79 } else if (fromScale == 2 && toScale == 1) {
  90. 80 newTemp = (double)5/9 * (temperature - 32.0);
  91. 81 } else if (fromScale == 2 && toScale == 2) {
  92. 82 newTemp = (double)5/9 * (temperature - 459.67);
  93. 83 } else if (fromScale == 3 && toScale == 1) {
  94. 84 newTemp = temperature - 273.15;
  95. 85 } else if (fromScale == 3 && toScale == 2) {
  96. 86 newTemp = 1.8 * (temperature - 273.15) + 32.0;
  97. 87 } else {}
  98. 88
  99. 89 //determine unit of measurement to print at the end.
  100. 90
  101. 91 if (fromScale == 1) {
  102. 92 sourceString = "Celsius";
  103. 93 if (toScale == 1) {
  104. 94 destString = "Fahrenheit";
  105. 95 } else {
  106. 96 destString = "Kelvin";
  107. 97 }
  108. 98 }
  109. 99
  110. 100 else if (fromScale == 2) {
  111. 101 sourceString = "Fahrenheit";
  112. 102 if (toScale == 1) {
  113. 103 destString = "Celsius";
  114. 104 } else {
  115. 105 destString = "Fahrenheit";
  116. 106 }
  117. 107 } else {
  118. 108 destString = "Fahrenheit";
  119. 109 }
  120. 110
  121. 111 DecimalFormat df = new DecimalFormat("0.00");
  122. 112
  123. 113 System.out.println();
  124. 114 System.out.println(temperature + " degrees " + sourceString + " = " + df.format(newTemp) + " degrees " + destString);
  125. 115 }
  126. 116 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement