Creating a Java Smartphone Class with Encapsulation and Storage Increase
Java Encapsulatiion: Exercise-9 with Solution
Write a Java program to create a class called Smartphone with private instance variables brand, model, and storageCapacity. Provide public getter and setter methods to access and modify these variables. Add a method called increaseStorage() that takes an integer value and increases the storageCapacity by that value.
Sample Solution:
Java Code:
Smartphone.java
// Define the Smartphone class
public class Smartphone {
// Private instance variables
private String brand;
private String model;
private int storageCapacity;
// Public getter for the brand variable
public String getBrand() {
return brand;
}
// Public setter for the brand variable
public void setBrand(String brand) {
this.brand = brand;
}
// Public getter for the model variable
public String getModel() {
return model;
}
// Public setter for the model variable
public void setModel(String model) {
this.model = model;
}
// Public getter for the storageCapacity variable
public int getStorageCapacity() {
return storageCapacity;
}
// Public setter for the storageCapacity variable
public void setStorageCapacity(int storageCapacity) {
this.storageCapacity = storageCapacity;
}
// Method to increase the storage capacity by a given value
public void increaseStorage(int additionalStorage) {
if (additionalStorage > 0) {
this.storageCapacity += additionalStorage;
}
}
}
Main.java
// Main method to test the Book class
public class Main {
// Main method to test the Smartphone class
public static void main(String[] args) {
// Create a new Smartphone object
Smartphone phone = new Smartphone();
// Set the brand, model, and initial storage capacity of the phone
phone.setBrand("SmartMobile");
phone.setModel("W1000");
phone.setStorageCapacity(60);
// Increase the storage capacity by 30
phone.increaseStorage(30);
// Print the details of the phone
System.out.println("Brand: " + phone.getBrand());
System.out.println("Model: " + phone.getModel());
System.out.println("Storage Capacity: " + phone.getStorageCapacity() + "GB");
}
}
Output:
Brand: SmartMobile Model: W1000 Storage Capacity: 90GB
Explanation:
- Private Instance Variables: The brand, model, and storageCapacity variables are declared as private to ensure encapsulation.
- Public Getters and Setters: These methods provide controlled access to the private variables.
- getBrand(): Returns the brand.
- setBrand(String brand): Sets the brand.
- getModel(): Returns the model.
- setModel(String model): Sets the model.
- getStorageCapacity(): Returns the storage capacity.
- setStorageCapacity(int storageCapacity): Sets the storage capacity.
- increaseStorage Method: Takes an integer value and increases the storageCapacity by that value if it's positive.
- Main Method: Tests the functionality of the Smartphone class by creating an instance, setting its properties, increasing the storage, and printing the details.
Note on Encapsulation
Encapsulation works in the above exercise by:
- Hiding Data: The private instance variables brand, model, and storageCapacity are not accessible directly from outside the class.
- Controlled Access: The public getter and setter methods provide controlled access to the private variables, allowing for validation and modification when necessary.
- Data Integrity: Encapsulation helps maintain data integrity by ensuring that the internal state of the object can only be changed through well-defined methods.
Java Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Creating a Java Book Class with Encapsulation and Discount Method.
Next: Creating a Java Desktop Class with Encapsulation and RAM Upgrade.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/encapsulation/java-encapsulation-exercise-9.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics