package hw;
public class Temperature {
private double value;
private char scale;
public Temperature() {
this.value = 0;
this.scale = 'C';
}
public Temperature(double value) {
this.value = value;
this.scale = 'C';
}
public Temperature(char scale) {
this.value = 0;
if (scale == 'f' || scale == 'F') {
this.scale = 'F';
}
else if (scale == 'c' || scale == 'C') {
this.scale = 'C';
}
else {
System.out.println("Error ");
this.scale = 'C';
}
}
public Temperature(double value, char scale) {
this.value = value;
if (scale == 'f' || scale == 'F') {
this.scale = 'F';
}
else if (scale == 'c' || scale == 'C') {
this.scale = 'C';
}
else {
System.out.println("Error ");
this.scale = 'C';
}
}
public double getC() {
double rtn = 0;
if (scale == 'C') {
rtn = value;
}
else if (scale == 'F') {
rtn = (value - 32) * 5 / 9;
}
return rtn;
}
public double getF() {
double rtn = 0;
if (scale == 'F') {
rtn = value;
}
else if (scale == 'C') {
rtn = (value * 9 / 5) + 32;
}
return rtn;
}
public void setValue(double value) {
this.value = value;
}
public void setScale(char scale) {
if (scale == 'f' || scale == 'F') {
this.scale = 'F';
}
else if (scale == 'c' || scale == 'C') {
this.scale = 'C';
}
else {
System.out.println("Error ");
}
}
public void setTemperature(double value, char scale) {
if (scale == 'f' || scale == 'F') {
this.scale = 'F';
}
else if (scale == 'c' || scale == 'C') {
this.scale = 'C';
}
else {
System.out.println("Error ");
}
this.value = value;
}
public boolean equals(Temperature otherTemp) {
if (this.scale == 'C' && this.value == otherTemp.getC()) {
return true;
}
else if (this.scale == 'F' && this.value == otherTemp.getF()) {
return true;
}
else {
return false;
}
}
public boolean greater(Temperature otherTemp) {
if (this.scale == 'C' && this.value >= otherTemp.getC()) {
return true;
}
else if (this.scale == 'F' && this.value >= otherTemp.getF()) {
return true;
}
else {
return false;
}
}
public String less(Temperature coldC) {
return "";
}
}
----------------------------------------------------------------------------
package hw;
class TemperatureDemo {
public static void main(String[] args) {
Temperature iceC = new Temperature(0.0, 'C');
Temperature iceF = new Temperature(32.0, 'F');
Temperature fireC = new Temperature(100.0);
fireC.setScale('C');
Temperature fireF = new Temperature(212.0);
fireF.setScale('F');
Temperature coldC = new Temperature();
coldC.setTemperature( -40.0, 'C');
Temperature coldF = new Temperature();
coldF.setScale('F');
coldF.setValue( -40.0);
System.out.println("iceC = " + iceC);
System.out.println("iceF = " + iceF);
System.out.println("fireC = " + fireC);
System.out.println("fireF = " + fireF);
System.out.println("coldC = " + coldC);
System.out.println("coldF = " + coldF);
System.out.print("\nTest equals.\n");
System.out.println(fireF + " = " + fireC + " ? " + fireF.equals(fireC));
System.out.println(iceC + " = " + iceF + " ? " + iceC.equals(iceF));
System.out.println(coldF + " = " + coldC + " ? " + coldF.equals(coldC));
System.out.println(iceF + " = " + iceC + " ? " + iceF.equals(iceC));
System.out.print("\nTest less.\n");
System.out.println(iceC + " <= " + coldC + " ? " + iceC.less(coldC));
System.out.println(iceC + " <= " + fireF + " ? " + iceC.less(fireF));
System.out.println(iceF + " <= " + coldC + " ? " + iceF.less(coldC));
System.out.println(iceF + " <= " + fireC + " ? " + iceF.less(fireC));
System.out.print("\nTest geater\n");
System.out.println(iceC + " >= " + fireC + " ? " + iceC.greater(fireC));
System.out.println(iceC + " >= " + coldF + " ? " + iceC.greater(coldF));
System.out.println(iceF + " >= " + fireF + " ? " + iceF.greater(fireF));
System.out.println(iceF + " >= " + coldF + " ? " + iceF.greater(coldF));
}
}