Page 393 - Computer_Science_F5
P. 393
Computer Science class Car:
private String make;
private String model;
private int year;
private boolean running;
public Car (String make, String model, int year) {
FOR ONLINE READING ONLY
this.make = make; // Encapsulated attribute
this.model = model; // Encapsulated attribute
this.year = year; // Encapsulated attribute
this.running = false; // Encapsulated attribute
}
public void startEngine() {
this.running = true; // Setter method
}
public void stopEngine() {
this.running = false; // Setter method
}
public String getMake() {
return this.make; // Getter method
}
i. What do you understand by Setter method and Getter method from codes
above.
ii. Explain why are the attributes in the class labelled private.
iii. In the provided code examples, how is encapsulation implemented in the
Car class?
iv. What are the advantages of encapsulating data and methods within a class,
as demonstrated in the code?
Project: Exploring Garbage Collection in Java
Project overview: Use this project, investigate the inner workings of Java’s
garbage collection mechanism. By creating and destroying a significant number
of objects, observe how the finalize() method is invoked just before an object is
garbage collected.
Materials required:
Computer with Java Development Kit (JDK) installed, Integrated Development
Environment (IDE) such as IntelliJ IDEA, Eclipse, or any
Deliverables:
A detailed report including screenshots of the running java garbage collection
mechanism program.
384
for Advanced Secondary Schools
Computer Science Form 5.indd 384 23/07/2024 12:34

