1

I am trying to get code that implements a textual view and a graphical view of the conversion using the MVC pattern. Currently, my code converts temperature from Fahrenheit to Celsius.

I think I have to use the Observable class and Observer interface in the Java standard library for the MVC pattern implementation, but I could be wrong.

This is what i got so far.

public class Temperature {
 public static String CUnit = " °C";
 private double tempF;
 private double tempC;
 public Temperature() {
 tempF=0;
 }
 public Temperature(double tempF) {
 this.setTempF(tempF);
 }
 public double getTempF() {
 return tempF;
 }
 public void setTempF(double tempF) {
 this.tempF = tempF;
 }
 private void convertFtoC(){
 tempC = (getTempF()-32) * 5/9;
 }
 public double getTempC() {
 convertFtoC();
 return tempC;
 }
 public void setTempC(double tempC) {
 this.tempC = tempC;
 }
}

Also here is my class MVCenter image description here

Am I close? Any help is appreciated

Thank you

  • + functionName(params) : is the same as public functionName(params), your getter musn't call any function. – Bzil Nov 03 '15 at 15:30
  • Beware the confounding results of integer division in this line: `(getTempF()-32) * 5/9;` You may want to cast these as doubles. For more info: http://stackoverflow.com/questions/7220681/division-of-integers-in-java – TayTay Nov 03 '15 at 15:39
  • Personally, I wouldn't make the "convert" a publicly exposed function. Have them set Celsius or Fahrenheit, and in the "setter" code calculate the values for both (and store them in member variables) – Edwin Buck Nov 03 '15 at 22:28

1 Answers1

1

You have a decent MVC class diagram, but your code doesn't match your class diagram.

For example, in your class diagram, Temperature is a subclass of Observable, but in your code, it is not a subclass of Observable. Also, from this side of the internet, it is not clear if you even have an observable class.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138