Lập trình hướng đối tượng Java: mối quan hệ giữa các đối tượng.

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create Point and Line classes as in the following class diagram:

Explanation:

The above diagram describes the relationship between Line class and Point class.

Point class

  • This class is used for storing information about a point in a two-dimensional coordinate system.
  • x and y attributes describe the abscissa and ordinate of a point.
  • Point(x: int, y: int) is the parameter constructor used for initializing values to x and y.
  • setX(), getX(), setY(), getY() are setter and getter.

Line class

  • This class stores information about a line in a two-dimensional coordinate system.
  • begin and end attributes describe the start point and end point of a line. They are the object of Point class.
  • Line(begin: Point, end: Point) and Line(x1: int, y1: int, x2: int, y2: int) constructors are used for initializing values to attributes.
  • setBegin(), getBegin(), setEnd(), getEnd() are setter and getter.
  • getLength() method returns the length of a line. For example, if begin = (1, 1), end = (2, 2)getLength() method will return 1.4142135623730951.

A program to test the above classes:

public class Entry {
	public static void main(String[] args) {
		Point begin = new Point(1, 1);
		Point end = new Point(2, 2);
		Line line1 = new Line(begin, end);
		System.out.println(line1.getLength());

		Line line2 = new Line(2, 3, 5, 4);
		System.out.println(line2.getLength());
	}
}

When the above code is compiled and executed, it produces the following result:

1.4142135623730951
3.1622776601683795

Note: CodeLearn accepts error less than 0.000001

Theory

In this lesson, you will learn about the relationships between objects.

Dependency

The dependency is the relationship in which the object of this class uses the object of the other class. For example, if the ClassA class uses objects of ClassB class (it can be properties, parameters, local variables, etc.) then ClassA class has a dependency with ClassB class. For example:

class ClassA{
	public void myMethod(ClassB b) {
		b.doWork();
	}
}

In the above case, ClassA will depend on ClassB, if ClassB changes, then ClassA may have to change.

This is the widest relation and is represented by a dashed arrow that looks like this:


Association

Associations occur when an object has attributes which are another object. For example:

class Employee {
	private String name;
	private Manager manager;

	public Employee(String name, Manager manager) {
		this.name = name;
		this.manager = manager;
	}

	public String getManagerName() {
		return manager.getName();
	}

	public String getName() {
		return name;
	}
}

class Manager {
	private String name;

	public Manager(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
}

In the above code, the Employee class has an attribute of an object belonging to the Manager class, so these two classes are associated. Associations are divided into 2 types: Aggregation and Composition.

Aggregation

Aggregation occurs when an object has an attribute that is another object and these two objects can exist independently. For example:

public class ClassA {       
    ClassB b;
    public void setB(ClassB b){
    	this.b = b;
     }
}

It can be seen that object of ClassB exists independently and it does not rely on ClassA object (ClassA does not create ClassB objects).

Composition

hen an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition. For example, the relationship between a File and a Folder: a folder will have many files and if the folder no longer exists, the file will disappear as well. This relationship will be shown in the following class diagram:

The number 1 and the * symbol indicate that a Folder will have many files. When it comes to programming, if an object is created in another object, it is an association:

public class ClassA{
	private ClassB b;
	public ClassA() {
		b = new ClsasB();
	}
}

Instruction

Code sample:

Point.java file 

public class Point {
	private int x;
	private int y;

	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
}

Line.java file 

public class Line {
	private Point begin;
	private Point end;

	public Line(Point begin, Point end) {
		this.begin = begin;
		this.end = end;
	}

	public Line(int x1, int y1, int x2, int y2) {
		this.begin = new Point(x1, y1);
		this.end = new Point(x2, y2);
	}

	public Point getBegin() {
		return begin;
	}

	public void setBegin(Point begin) {
		this.begin = begin;
	}

	public Point getEnd() {
		return end;
	}

	public void setEnd(Point end) {
		this.end = end;
	}
	public double getLength() {
		return Math.sqrt(Math.pow(end.getX() - begin.getX(), 2) + Math.pow(end.getY() - begin.getY(), 2));
	}
}