i want to convert celsius to farenhiet and vice versa i have got the conversion part down but i want to be able to do with only the letter so that if someone types F at the end it converst to C and if someone types C at the end of any number it converst to farenheit without asking the user to what they want it converted to
public final class Temperature {
private final double c;
private Temperature(double c) {
this.c = c;
}
public static Temperature fromCelcius(double c) {
return new Temperature(c);
}
public static Temperature fromFarenheight(double f) {
return (f - 32) * (5.0 / 9.0);
}
public double celcius() {
return this.c;
}
public double farenheight() {
return (this.c * (9.0 / 5.0)) + 32;
}
}
Obviously you know how to do the math, but one technique is to just always store one temperature type, one internal representation, and do conversions as needed but directly, at the boundaries.