Select the correct answer based on your knowledge of Java Encapsulation.
1. What is the primary meaning and purpose of Encapsulation in Java?
To create special classes that represent a group of constants.
To set initial values for object attributes upon creation.
To make sure that sensitive data is hidden from users and external code.
To inherit attributes and methods from one class to another.
Answer: C. To make sure that sensitive data is hidden from users.
2. To properly achieve Encapsulation, how should you declare your class attributes and methods?
Declare attributes as protected and provide abstract methods.
Declare attributes as public and provide default methods.
Declare attributes as default and provide protected methods.
Declare attributes as private and provide public getter and setter methods.
Answer: D. Declare attributes as private and provide public getter and setter methods.
3. Which access modifier ensures that an attribute or method is accessible ONLY within the exact same class it is declared in?
public
protected
default
private
Answer: D. private
Question 2: Code Tracing & Logic (8 points)
Analyze the following code snippets. Determine the exact output, or state if there is a compile-time error and explain why.
1. What happens when the following code is compiled and run?
public class Person {
private String name;
}
public class Main {
public static void main(String[] args) {
Person myObj = new Person();
myObj.name = "John";
System.out.println(myObj.name);
}
}
Output: ERROR (Compile-time error).
Explanation: The 'name' variable in the Person class is declared as 'private'. Because of encapsulation, private variables cannot be accessed directly from outside the class (in the Main class).
2. What is the output of the following correctly encapsulated code?
public class BankAccount {
private double balance = 100.0;
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.deposit(50.5);
System.out.println(acc.getBalance());
}
}
Output: 150.5
Explanation: The initial balance is 100.0. The deposit method is called with 50.5, which safely adds to the private balance. The getter then retrieves the updated balance.
Question 3: Programming Tasks (10 points)
Write complete Java code to solve the following standard programming problem.
1. The Product Inventory System:
Write a complete Java class named Product that demonstrates proper Encapsulation. Follow these requirements carefully:
Create two private attributes: a String named productName and a double named price.
Write a public getter and a public setter for the productName.
Write a public getter for the price.
Write a public setter for the price. Inside this setter, add validation logic: the price should only be updated if the passed value is greater than 0. (This prevents negative prices).
public class Product {
// Private attributes
private String productName;
private double price;
// Getter for productName
public String getProductName() {
return productName;
}
// Setter for productName
public void setProductName(String pName) {
this.productName = pName;
}
// Getter for price
public double getPrice() {
return price;
}
// Setter for price with validation
public void setPrice(double pPrice) {
if (pPrice > 0) {
this.price = pPrice;
} else {
System.out.println("Invalid price. Must be greater than 0.");
}
}
}