1: Temperature [30 marks] Complete the provided Temperature class. Add any attri
ID: 3593872 • Letter: 1
Question
1: Temperature [30 marks] Complete the provided Temperature class. Add any attributes and helper methods as needed You must complete the constructors and methods in the provided class (without changing any signatures, return types, or modifiers) In this problem you will need to be able to convert temperatures between Celsius, Fahrenheit and Kelvin. For help, see https://en.wikipedia.org/wiki/Conversion_of units of_temperature A temperature object holds a single temperature and displays it in one of the three scales. Once a scale has been set, it will display the temperature in that scale until changed. The default scale is Celsius if not specified Examples: Temperature t = new Temperature (10.1); System.out.println (t.getScaleO)// outputs the char 'C System. out.println(t); t.setScale("F"); System.out.println (t); System.out.println(t.getScale());// outputs the char 'F' // outputs 10.10 // outputs 50.18F Note: Repeatedly changing the scale should not ”change" the value of the temperature. For example, Temperature t = new Temperature (10.1); System.out.println(t); for (int i=0; i10000; i+-1){ t.setScale("F"); t.setScale("C"); System.out.println (t); Should print out identical strings. Note: You should have no static attributes or methods in your class (unless they were supplied in the starter code Note: Your code must use encapsulation. Your grade will be reduced by 5 marks if you do not use encapsulation. Mark breakdown: 5 marks for Style, 25 marks for CorrectnessExplanation / Answer
Temperature.java
public class Temperature {
public static String[] scales = {"Celcius", "Fahrenheit","Kelvin"};
private double temp;
private String scale;
public Temperature(double temp) {
if(temp >= -273.15 ) {
this.temp = temp;
} else {
this.temp = -273.15;
}
}
public Temperature(double temp, String scale) {
if(temp >= 0) {
this.temp = temp;
}
else {
this.temp = 0;
}
this.scale = scale;
}
public char getScale() {
return scale.charAt(0);
}
public double getTemp() {
return temp;
}
public void setScale(String scale) {
this.scale = scale;
}
public void setTemp(double temp) {
this.temp = temp;
}
public String toString() {
return ""+this.getTemp()+this.getScale();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.