Monday, December 12, 2005

Homework 12-5-2005

package abcd;

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 Scale,and scale set C");
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 Scale,and scale set C");
this.scale = 'C';
}
}

public double getTempC() {
double temp = 0;
if (scale == 'C')
{
temp = value;
}
else if (scale == 'F') {
temp = (value - 32) * 5 / 9;
}
return temp;
}

public double getTempF() {
double temp = 0;
if (scale == 'F') {
temp = value;
}
else if (scale == 'C') {
temp = (value * 9 / 5) + 32;
}
return temp;
}

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 Scale,and scale no change");
}
}

public void setTemp(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 Scale,and scale no change");
}
this.value = value;
}

public boolean Equals(Temperature otherTemp) {
if (this.scale == 'C' && this.value == otherTemp.getTempC()) {
return true;
}
else if (this.scale == 'F' && this.value == otherTemp.getTempF()) {
return true;
}
else {
return false;
}
}

public boolean GreaterThan(Temperature otherTemp) {
if (this.scale == 'C' && this.value >= otherTemp.getTempC()) {
return true;
}
else if (this.scale == 'F' && this.value >= otherTemp.getTempF()) {
return true;
}
else {
return false;
}
}

public boolean LessThan(Temperature otherTemp) {
if (this.scale == 'C' && this.value <= otherTemp.getTempC()) {
return true;
}
else if (this.scale == 'F' && this.value <= otherTemp.getTempF()) {
return true;
}
else {
return false;
}
}

public String toString() {
return Double.toString(value) + scale;
}

}


class TemperatureTest {

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.setTemp( -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 + " , " + coldC.getTempF() + "F");
System.out.println("coldF = " + coldF + " , " + coldF.getTempC() + "C");

System.out.print("\nTest Equals ? \n");
System.out.println(iceC + " = " + coldC + " ? " + iceC.Equals(coldC));
System.out.println(iceC + " = " + iceF + " ? " + iceC.Equals(iceF));
System.out.println(iceF + " = " + coldC + " ? " + iceF.Equals(coldC));
System.out.println(iceF + " = " + iceC + " ? " + iceF.Equals(iceC));

System.out.print("\nTest LessThan ? \n");
System.out.println(iceC + " <= " + coldC + " ? " + iceC.LessThan(coldC));
System.out.println(iceC + " <= " + fireF + " ? " + iceC.LessThan(fireF));
System.out.println(iceF + " <= " + coldC + " ? " + iceF.LessThan(coldC));
System.out.println(iceF + " <= " + fireC + " ? " + iceF.LessThan(fireC));

System.out.print("\nTest GreaterThan ? \n");
System.out.println(iceC + " >= " + fireC + " ? " + iceC.GreaterThan(fireC));
System.out.println(iceC + " >= " + coldF + " ? " + iceC.GreaterThan(coldF));
System.out.println(iceF + " >= " + fireF + " ? " + iceF.GreaterThan(fireF));
System.out.println(iceF + " >= " + coldF + " ? " + iceF.GreaterThan(coldF));


}

}

0 Comments:

Post a Comment

<< Home