Basket777
Basket777

Reputation: 17

UML diagram confusion

Hi guys,this is my first question on StackOverflow

I am kind of new to java and I need to solve this uml diagram . I got a solution from one of my classmates but I don't think it's correct and I did it my way. My question is which one of the solutions is correct? I know that the type of relation is an association one . Not an inheritance

the uml diagram I want to solve

Her code

 class Sensor {
    protected int value;
    protected String location;

    public Sensor() { // default constructor
        value = 0;
        location = "North-West";
    }

    public Sensor(int value, String location) { // overridden constructor
        this.value = value;
        this.location = location;
    }

    protected int getValue() { // value getter
        return value;
    }

    protected void setValue(int v) { // value setter
        this.value = v;
    }

    protected void displaySenzorInfo() { // display information on the sensor
        System.out.println("Temperature is " + value + ", located " + location + ".");
    }
}

class Controller extends Sensor {
    protected String name;

    public Controller(String name) { // overridden constructor
        this.name = name;
    }

    public Controller(String name, int value, String location) { // overridden
                                                                    // instructor
        this.name = name;
        super.value = value;
        super.location = location;
    }

    public Controller() { // default constructor, which creates a new Sensor()
        //Sensor s = new Sensor();
    }

    protected void checkTemperature() { // checks temperature of sensor
        System.out.println("Temperature of " + name + " is " + super.value + ", located at " + super.location + ".");
    }
}

public class E3 {

    public static void main(String[] args) {
        Controller control = new Controller();
        control.displaySenzorInfo();

        Controller c = new Controller("Pizza", 30, "North");
        c.checkTemperature();
    }
}

My code

class Sensor{

     int value;
        String location;
         Sensor(){
        value=0;
        location="Sibiu";
    }
    Sensor(int value,String location){
        this.value=value;
        this.location=location;
    }
    int getValue(){
        return value;
    }
     void setValue(int v){
        this.value=v;
    }
    void displaySenzorInfo(){
        System.out.println("Temperature is " + value + ", located " + location + ".");
    }

}

class Controller{

    Sensor tempSensor;
    String name;
    Controller(){
        name="Sibiu";
        tempSensor=30;
    }
    Controller (String name,Sensor tempSensor){
        this.name=name;
        this.tempSensor=tempSensor;
    }
    void checkTemperature(Sensor tempSensor){
       if (tempSensor>=30)
           System.out.println("the temperature is too high!");
           else
           System.out.println("the temp is too low" );

   }

}

public class E3{

     public static void main(String []args){
        Sensor s1=new Sensor();
        Controller c1=new Controller();
        c1.displaySenzorInfo();
        Controller c2=new Controller(30,"Oliver");

     }
}

Please guys. If you have some suggestions or if you see any problems in m program tell me. I know that I will have some errors because I didn't work at this exercise in any IDE because I am at work and I don't have any . Thank you!!!

Upvotes: 1

Views: 379

Answers (2)

Kedar Tokekar
Kedar Tokekar

Reputation: 418

Though overall coding (MyCode) for relationship from the given diagram is OK, I have following observations. (Her code) - Inheritance is not correct. Unidirectional association is correct.

If this is diagram is only for exercise purpose its OK, otherwise it will violate data hiding and encourage client classes to violate encapsulation (Using somebody else's data directly)

  1. tempSensor=30;is not correct for data type.
  2. if (tempSensor>=30) is incorrect for data type and even if you correct, it violates encapsulation (works on somebody else's data) as an effect of first violation of making instance variables non-private. classes should work on their own data.
  3. Even if for some reason we accept above violation, checkTemperature(Sensor tempSensor) makes use of fresh instance of Sensor (for every call), which is not the one obtained from association relationship. This method should not have parameter, it should work on this.tempSensor (with accepted data leakage). Ideally this is indication that data and its behavior are getting separated and design needs to be corrected.

In case the diagram can not be changed then just remove the parameter in checkTemperature() and take care of data types as shown above.

But I would suggest change at Design level as follows for better encapsulation.

public class SensorNew {
    private static final double UPPER_THRESHOLD = 25;
    private static final double LOWER_THRESHOLD = 20;
    private String location;
    private Controller controller;

    public SensorNew(String location, Controller controller) {
        this.location = location;
        this.controller = controller;
    }

    public int getCurrentTemp() {
        // obtain from sensor hardware
        return 10; // Just example
    }

    private void makePeriodicCheck(){
        double currentTemp = getCurrentTemp();
        if (currentTemp > UPPER_THRESHOLD){
            controller.coolDown();
        } else if (currentTemp < LOWER_THRESHOLD){
            controller.heatUp();
        } else {
            controller.stopIfRunning();
        }
    }

    public void displaySenzorInfo() { // replace by toString()
        System.out.println("Temperature is " + getCurrentTemp() 
        + ", located " + location + ".");
    }
}

public class ControllerNew {
    private String name;
    // Need to maintain the state of Controller
    // either by variable or State design pattern (preferred)

    public ControllerNew(String name, Sensor tempSensor) {
        this.name = name;
    }

    public void coolDown() {
        // action depending upon current state of controller
    }

    public void heatUp() {
        // action depending upon current state of controller
    }

    public void stopIfRunning() {
        // action depending upon current state of controller
    }
}

The advantage is that we do not have to provide public getXX() setXX() methods to these classes. Hence it maintains encapsulation.

Upvotes: 0

Christian Ammann
Christian Ammann

Reputation: 938

your solution is the correct one. As you mentioned already, it is an association and not an inheritance. You can see how an inheritance looks like on wikipedia: https://en.wikipedia.org/wiki/Class_diagram

Upvotes: 1

Related Questions