이것은 사용자가 2024-10-21 10:30에 https://codegym.cc/quests/lectures/cgu.module2.lecture06을(를) 위해 저장한 이중 언어 스냅샷 페이지로, 몰입형 번역에 의해 제공된 이중 언어 지원이 있습니다. 저장하는 방법을 알아보세요?

Unfinished business

To finish creating your account,
enter a valid email address and create a password.
Invalid email address
Required field
CodeGym/Java Course/Module 2. Java Core/Association: composition and aggregation

Association: composition and aggregation

Available

Composition and aggregation

Classes and objects can be related to each other. Inheritance describes an "IS A" relationship. A lion IS AN animal. This relationship is easily expressed using inheritance, where Animal would be the parent class and Lion would be the child.

But not every relationship in the world is properly described this way. For example, a keyboard definitely has some relationship with a computer, but it isn't a computer. Hands have some relationship with a person, but they are not a person. These cases represent a different type of relationship — not "IS A", but "HAS A". Hands are not a person, but a person HAS hands.

A keyboard is not a computer, but a computer HAS A keyboard. "HAS A" relationships can be described in code using composition and aggregation. The difference between these concepts lies in the "strictness" of the relationships.

Let's take a simple example:

  • We have a Car.
  • Every car has an engine. Additionally, every car can carry passengers.

What is the fundamental difference between the Engine engine and Passenger[] passengers fields? If passenger A is sitting inside a car, that doesn't mean that passengers B and C cannot also be in the car.

One car can accommodate multiple passengers. What's more, all the passengers may get out of the car, yet it will continue to function smoothly. The relationship between the Car class and the Passenger[] passengers array is less strict. It is called aggregation.

Here's a good article on this topic: Relationships between classes (objects).

It provides another good example of aggregation. Let's say we have a Student class that represents a student, and a StudentGroup that represents a group of students. A student can be a member of a physics club, a Star Wars student fan club, or a comedy club.

Composition is a stricter type of relationship. When using composition, an object has another object, but it cannot belong to another object of the same type. The simplest example is a car engine. If a car has an engine, then that engine cannot belong to another car. As you can see, that relationship is much stricter than that of Car and Passengers.

Designing a motorcycle

To make each car have its own engine, create an immutable private engine field in the Car class.

The field must be initialized with a new engine when a vehicle is created.

The car methods must call the corresponding engine methods.

Requirements:
  • Don't change the Engine class.
  • The Car class must have an engine field.
  • The engine field must be initialized when a Car object is created.
  • The methods of the Car class (start and turnOff) must call the corresponding methods of the Engine class.
The task passed testing!
1
Number
of attempts made
2.6
Average number
of attempts
10
Your reward
package en.codegym.task.jdk13.task11.task1112;
public class Car {
private final Engine engine;
public Car() {
engine = new Engine();
}
public void start() {
engine.start();
}
public void turnOff() {
engine.turnOff();
}
private static class Engine {
private boolean running;
private void start() {
running = true;
System.out.println("Vroom! VROOM!");
}
private void turnOff() {
running = false;
System.out.println("Pffffff");
}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Output
Vroom! VROOM! Pffffff
Reinstalling Windows

Implement the OperatingSystem and Laptop classes.

In the OperatingSystem class, you need to:

  • add private String name and version fields, where name is immutable and version is mutable;
  • implement a constructor with two String parameters, which assigns values to fields;
  • add getters and setters (where possible) for fields.

In the Laptop class, you need to:

  • add immutable private String name and OperatingSystem os fields;
  • implement a constructor with three String parameters, which assigns values to fields;
  • add a getter for the name field;
  • add a public void updateOs(String version) method that updates the version of the operating system;
  • add a public void printInfo() method that displays the name of the laptop as well as the name and version of the operating system in an arbitrary format.
Requirements:
  • You need to create name and version fields in the OperatingSystem class and name and os fields in the Laptop class.
  • In the OperatingSystem and Laptop classes, constructors must be implemented in accordance with the task conditions.
  • In the OperatingSystem and Laptop classes, getters and setters must be added in accordance with the task conditions.
  • In the Laptop class, the updateOS method must be implemented in accordance with the task conditions.
  • The Laptop class must implement the printInfo method in accordance with the task conditions.
package en.codegym.task.jdk13.task11.task1113;
public class OperatingSystem {
public OperatingSystem(String name, String version) {
//write your code here
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
10
Task
Module 2. Java Core,  level 3lesson 1
Available
Aggregation
task1114
10
Task
Module 2. Java Core,  level 3lesson 1
Available
Aggregation - Part 2
task1115
Comments
  • Popular
  • New
  • Old
stackfish
This page doesn't have any comments yet