posted Oct 5, 2018, 6:24 AM by Konstantinovich Samuel   [ updated Oct 9, 2018, 5:35 AM ]
Goal UML.

HW at bottom.

UML Diagrams are used to visualize a class:

Classname is at the top.
Fields are in the top half.
name : type

Methods in the bottom half, with a return type after the colon.
name(paramTypes) :returnType

+ means public
- means private

There are several types of associations (relationships) between classes that we will discuss. 

For now, we can look at:

Composition: (Ownership) The item contained inside DOES NOT exist outside of the container. 
  A house has rooms. You cannot have the rooms without the house. 
  A company has a collection of accounts, the accounts do not exist without the company.  

Aggregation:  The item contained inside DOES exist outside of the container.  Neither "owns" each other, they exist separately but are related in some way.
  A student has a collection of courses, and courses have a collection of students. 
  A course can have an instructor, instructors have one or more courses. 
  A company has a collection of employees, but they exist outside of the company.

Sometimes it is not so clear what we want. Everything in computer science can depend on your goals or your priorities when modeling a situation.

Cars have an Engine.
CAN the Engine exist outside of the car?
    Sure, we can track them separately. Car engines are parts that can be bought and sold as inventory of a supply chain. (Aggregation)
CAN we treat the Engine as being a dedicated part of the car that doesn't get moved separately?
    Sure, we can treat the engine and car as one unit that you don't separate. (Composition)

In a computer model, it depends on your requirements. 
-If you need to track the component as an object separate from the outside object, then you can treat it like an Aggregation. (e.g. A car factory that deals with inventory might have a pile of engines)
-If you just own a bunch of outside objects, and never instantiate the inside object separately then you can treat it like a Composition. (e.g. A car dealer, or a car owner)

Static vs non-static methods:

Java has a built in Math class.

The methods are all static. You can use them by saying:


Strings also have static methods such as:
static String copyValueOf(char[] data)

char[] x = {'a','b','c'}
String s = String.copyValueOf( x );
//s now contains "abc"

Static methods are not part of an instance, they are part of the class.
Since they are not part of an instance they cannot see instance variables. (Except for static instance variables which we haven't discussed)

Static methods CAN be called from any instance, OR the classname.
String.copyValueOf( ...)
"Hello".copyValueOf( ...)
s.copyValueOf( ... )

These are all the same but we usually just use the classname.

Given initialized points in a main:

public class demo{ 

  /*other methods here*/
 public static double distanceBetween(Point a, Point b){...}

 public static void main(String[]args){ Point P1 = new Point(1,2); Point P2 = new Point(2,2);
    double d; //1. locally declared static method d = distanceBetween(P1,P2); //This is not a good way to deal with distance, need to write it in multiple places //You have to re-write this when used by several classses! //2. static method inside a class d = Point.distanceBetween(P1,P2);//OR P1.distanceBetween(P1,P2); //This is a better way to organize distance. //A static method is put it in the related class (Point) //You do not have to re-write this when used by several classses! //3. nonstatic method in a class d = P1.distanceTo(P2); //Similar to 3, a little more convinient syntactically.
//This is just like the transferTo method
} }

HOMEWORK! (all 5 parts)
 Draw a UML diagram for a class hierarchy, bring in a paper copy tomorrow.
-If you draw by hand I expect you to use a ruler, and write neatly. 
-Printed versions are much better than hand drawn.
-You can use a word processor if you want, but there are free tools available if you google a little.
-If it looks like you put no effort into this, I will not accept it. 

Class heirarchy is as follows:
1. You will make all fields private.
2. Because of this add in sensible setters/getters and constructors.
3. Also include a method inside of point that would calculate distance to another point. 
4. Also include a method inside of Triangle that would calculate perimeter.

Think about the following:
Triangle vs Point
Does the Triangle OWN the point?
Does the Point exist outside of the Triangle?

5. JUSTIFY your composition/aggregation decision. A few sentences please.

Also think of this:
Can we change the point? What if the point is Immutable?

How do we prevent the same point from being put into 2 different triangles?