Necatur
Necatur

Reputation: 33

How do I strictly implement the given UML using Java?

I am trying to implement the UML below using Java:

enter image description here

I have successfully implemented every instruction except one which is:

Theater class:

I am new to UML and from my understanding, I am not allowed to create constructors in any of the classes. This is confusing since I don't know where I can define the size for showArea.

Below is the working code I have right now.

Place Class

public abstract class Place {
    private String placeName;
    private int capacity;
    private String placeDescription;
    private int workingHours;


    public abstract void showEvents();
}

Building Class

public abstract class Building extends Place{
    
    public abstract void showArea();
}

Theater Class

public class Theater extends Building{
    @Override
    public void showArea() {
        System.out.println("Theater area : " );
    }

    @Override
    public void showEvents() {
        System.out.println("Events ready to be hosted !!");
    }
}

Main class

public class CodingtonDemo {
    public static void main(String[] args) {
        Theater theater = new Theater();
        theater.showArea();
        theater.showEvents();
    }
}

Expected result in console:

Theater area : 6000

Events ready to be hosted !!

My result so far:

Theater area : [No value yet]

Events ready to be hosted !!

Upvotes: 1

Views: 190

Answers (2)

Christophe
Christophe

Reputation: 73376

The problem with your diagram

Your diagram is a partial representation of your model:

  • The Place's properties placeName, placeDescription, capacity, workingHours are all private (-). This means that they are not accessivle for Building nor for Theater. Since you have no constructor and no setter, how can these properties be of any use? ANd since you have no public getter, how can these values be used in any showXxxx() operation of more specialized classes?

  • Since Building has no access to the private properties and has no other properties, how coud showArea() provide anything useful?

  • Finally, you're right about the overriding in Theatre. But the issues you've diagnosed for the missing size and its initialization are already true for Building.

So you will never be able to achieve your expected results on the console if you strictly stick to your diagram and add no other implicit operations. I hope this will not be a shock to you. Here the UML specification quote that backs my statement:

11.4.3.1: (...) A Class cannot access private Features of another Class, or protected Features on another Class that is not its ancestor.

Minor syntactical issues, you could improve:

  • The italic notation is meant for class names to document that they are abstract. It is no longer officially defined for abstract operations, although many (I say this because I do it myself) still use this notation. Using italics for operations which have an implementation is therefore utterly ambiguous.

  • void is no standard UML type. An operation returning void is just indicated without return type.

  • showEvents in Theater desserves a pair of braces ().

Complete your diagram

You could add the missing getters and setters. And you could define a constructor as explained in the UML specifications:

11.4.4: (...) A constructor is an Operation having a single return result parameter of the type of the owning Class, and marked with the standard stereotype «Create». The InstanceSpecification that is the supplier of the usage dependency represents the default value of the single return result parameter of a constructor Operation.

This would look like:

«Create» Place(...) : Place

You have now the means to complete your diagram and achieve the expected results.

Some more improvements may be desirable:

  • if Place implements no operation you should make it abstract, since it cannot be instantiated.
  • you may make the overriding explicit using the {redefines ...} in the diagram. But this is not necessary.
  • I guess that there is a missing relation to an Event class or interface, considering showEvents() and no other Events are in sight...

Reconsider your design

Is a theater a building? In my town, there is a building that hosts a mall AND a theater. So my advice would be to prefer composition over inheritance.

Upvotes: 2

Gustavo Oliveira
Gustavo Oliveira

Reputation: 93

Your code and UML are not equal and your UML have some problems. In your diagram, Place is a concrete class and it is an abstract class by your code. There is no reason to have private attributes in the Place class because they can't be accessed by it's children, so their visibility should be replaced by protected (symbol in UML is #). The building class doesn't make anything but declare a method, so it could be replaced by an interface (in UML you can use a circle or use the interface stereotype) without any inheritance of Place, this solution would affect Theater that instead of being a child of Building, would become a child of Place that implements the Building interface.

About your code, the showArea method in the Theater class doesn't display any value, so why would you expect the 6000 value ? If you expected the 6000 value to come from the Place's capacity, first you would need a method (probably a setter) that sets the capacity, use this method in the Main class, then make capacity protected to be accessible by Theater and finally use it at the showArea method.

Upvotes: 0

Related Questions