I don't like coffee (only the smell of it) but, the first Java activity was like drinking it.
UML representation is not difficult, it needs a bit of practice. I did a bit every day and, the result I got wasn't that bad.
From a given case, we had to generate the class diagram. You have to read the case a few times to figure out which are the classes needed and the relations between them. For me, it felt the same as doing a relational model from a database. Here you can see they are not that different 👇
Relational model
Class diagram
Defining the class is the easiest thing. You just take the name of the thing that will become a class and name it as it is.
Next thing, attributes. Normally this will also be specified in the case description, so you'll just need to figure out which type is it. Normally, and to name an example, a tracking number will be an int (integer) or a productName will be a string.
Next thing, where you can get mad if you haven't done Java before, methods. Setters and getters take part in here, and you have to have your mind clear to understand it at first.
To make things clear, we use a setter to update the value of a variable and a getter to read the value of a variable.
Let's see this with an example.
We have a new product. This product will have a name and will be read by the code machine.
The table will look like this:
Product |
-productId: int |
+getProductId(): int |
+setProductId(): int |
In the table the getter and the setter won't be separated with a line in the middle, like the ones you can see in the image above (I'm not so sure if this is possible with MD).
Also, you can see there's a - and a + in front of the attribute and the methods. Those relate to private (-) and public (+).
The code will be like this:
public class product {
/* Class is now created */
private int productId;
/* Attribute is now set */
public int getProductId() {
return this.number;
}
/* Getter is ready to return getProductId when used. */
public void setProductId(int num) {
this.number = num;
}
/* Setter is ready to be used when a new productId needs to be set */
};
If we had more attributes we could use more methods, functions or whatever, and this will get so long (writing code in Java is like writing the bible).
I think that, for a first approach, I made it more or less clear (if you have experience with Java, please tell me if I made any mistake and if I made it clear to understand 😜)
Salut, Jordi.
Cover image credit to @dsmacinnes