Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. public class Temperature{
  2.  
  3. // names for the different temperature scales
  4. public static String[] scales = {"Celsius", "Fahrenheit", "Kelvin"};
  5. private double temp;
  6. private String scale;
  7. // private char unit
  8. /* ----------------------------------------------------
  9. * constructors
  10. * ----------------------------------------------------
  11. */
  12.  
  13. public Temperature(double temp){
  14. // - creates a temperature object with given value in Celsius
  15. this.temp = temp;
  16. this.scale = scales[0];
  17. }
  18. public Temperature(double temp, String scale){
  19. // - creates a temperature object with given temp using the given scale
  20. // - scale must be one of the three strings in the scales attribute
  21. // OR the the first letter of the name (upper case)
  22. // Examples: new Temperature(12.3, "K")
  23. // new Tempersture(-90.2, "Celsius")
  24. this.temp = temp;
  25. this.scale = scale;
  26.  
  27.  
  28.  
  29. }
  30.  
  31. /* ----------------------------------------------------
  32. * methods
  33. * ----------------------------------------------------
  34. */
  35.  
  36. public char getScale(){
  37. // - returns the current scale of the object
  38. // as a single character
  39. // 'C' for Celsuius, 'F' for Fahrenheit, 'K' for Kelvin
  40.  
  41. // add your code and return the correct char
  42.  
  43. char unit=' ';
  44.  
  45. if(this.scale.equals(scales[0]) || this.scale.equals("C")){
  46. unit= 'C';
  47. }
  48. else if(this.scale.equals(scales[1]) || this.scale.equals("F")){
  49. unit='F';
  50. }
  51. else if (this.scale.equals(scales[2]) || this.scale.equals ("K")){
  52. unit='K';
  53. }
  54. return unit;
  55. }
  56.  
  57. public double getTemp(){
  58. // - returns the object's temperature using its current scale
  59. return this.temp;
  60. }
  61.  
  62.  
  63.  
  64. public void setScale(String scale){
  65. // - scale must be one of the three strings in the scales attribute
  66. // OR the first letter of the name (see the second constructor)
  67. // - sets the scale of the current object
  68. if(this.scale.equals(scales[0]) || this.scale.equals("C")){
  69.  
  70. if(scale.equals(scales[1]) || scale.equals("F")){
  71. // converting from c to f
  72. this.temp = (this.temp*9/5)+32;
  73. this.scale = scale;
  74. }
  75. else if (scale.equals(scales[2]) || scale.equals("K")){
  76. // converting c to k
  77. this.temp = this.temp+273.15;
  78. this.scale = scale;
  79. }
  80. }
  81. else if(this.scale.equals(scales[1]) || this.scale.equals("F")){
  82. if(scale.equals(scales[0]) || scale.equals("C")){
  83. // converting from f to c
  84. this.temp = (this.temp-32) *5/9;
  85. this.scale = scale;
  86.  
  87. }
  88. else if (scale.equals(scales[2]) || scale.equals("K")){
  89. // converting f to k
  90. this.temp = (this.temp+459.67)*5/9;
  91. this.scale = scale;
  92. }
  93. }
  94. else if (this.scale.equals(scales[2]) || this.scale.equals("K")){
  95. if(scale.equals(scales[0]) || scale.equals("C")){
  96. // converting from k to c
  97. this.temp = this.temp-273.15;
  98. this.scale = scale;
  99. }
  100.  
  101. else if (scale.equals(scales[1]) || scale.equals("F")){
  102. // converting k to f
  103. this.temp = (this.temp *9/5)-459.67;
  104. this.scale=scale;
  105. }
  106. }
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. public void setTemp(double temp){
  117. // - sets the objects temperature to that specified using the
  118. // current scale of the object
  119. this.temp = temp;
  120. }
  121.  
  122. public void setTemp(double temp, char scale){
  123. // - sets the objects temperature to that specified using the
  124. // specified scale ('K', 'C' or 'F')
  125. this.temp = temp;
  126. if(scale == 'K'){
  127. this.scale = "K";
  128. }
  129. else if(scale == 'C'){
  130. this.scale = "C";
  131. }
  132. else if(scale == 'F'){
  133. this.scale = "F";
  134. }
  135. }
  136.  
  137. public void setTemp(double temp, String scale){
  138. // - sets the objects temperature to that specified using the
  139. // specified scale
  140. // - scale must be one of the three strings in the scales attribute
  141. // OR the the first letter of the name (upper case)
  142. this.temp = temp;
  143. this.scale = scale;
  144. }
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. /* do not change anything below this line */
  154. /* -------------------------------------- */
  155.  
  156. /* string representation of object */
  157. public String temp(){
  158. return "" + this.getTemp() + this.getScale();
  159. }
  160.  
  161.  
  162.  
  163. /* Override the toString() method */
  164. /* with this we will not need the temp() method from above */
  165. /* we will cover this soon! */
  166. /* you do not need to use this for this assignment! */
  167. @Override
  168. public String toString(){
  169. return "" + this.getTemp() + this.getScale();
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement