Creating a Java Desktop Class with Encapsulation and RAM Upgrade
Java Encapsulatiion: Exercise-10 with Solution
Write a Java program to create a class called Desktop with private instance variables brand, processor, and ramSize. Provide public getter and setter methods to access and modify these variables. Add a method called upgradeRam() that takes an integer value and increases the ramSize by that value.
Sample Solution:
Java Code:
Desktop.java
// Define the Desktop class
public class Desktop {
// Private instance variables
private String brand;
private String processor;
private int ramSize;
// 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 processor variable
public String getProcessor() {
return processor;
}
// Public setter for the processor variable
public void setProcessor(String processor) {
this.processor = processor;
}
// Public getter for the ramSize variable
public int getRamSize() {
return ramSize;
}
// Public setter for the ramSize variable
public void setRamSize(int ramSize) {
this.ramSize = ramSize;
}
// Method to upgrade the RAM size by a given value
public void upgradeRam(int additionalRam) {
if (additionalRam > 0) {
this.ramSize += additionalRam;
}
}
}
Main.java
public class Main {
// Main method to test the Desktop class
public static void main(String[] args) {
// Create a new Desktop object
Desktop desktop = new Desktop();
// Set the brand, processor, and initial RAM size of the desktop
desktop.setBrand("ComputeMaster");
desktop.setProcessor("Intel Core i7");
desktop.setRamSize(32);
// Upgrade the RAM size by 32 GB
desktop.upgradeRam(32);
// Print the details of the desktop
System.out.println("Brand: " + desktop.getBrand());
System.out.println("Processor: " + desktop.getProcessor());
System.out.println("RAM Size: " + desktop.getRamSize() + "GB");
}
}
Output:
Brand: ComputeMaster Processor: Intel Core i7 RAM Size: 64GB
Explanation:
- Private Instance Variables: The brand, processor, and ramSize 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.
- getProcessor(): Returns the processor.
- setProcessor(String processor): Sets the processor.
- getRamSize(): Returns the RAM size.
- setRamSize(int ramSize): Sets the RAM size.
- upgradeRam Method: Takes an integer value and increases the ramSize by that value if it's positive.
- Main Method: Tests the functionality of the Desktop class by creating an instance, setting its properties, upgrading the RAM, and printing the details.
Note on Encapsulation
Encapsulation works in the above exercise by:
- Hiding Data: The private instance variables brand, processor, and ramSize 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 Smartphone Class with Encapsulation and Storage Increase.
Next: Creating a Java House Class with Encapsulation and Price Calculation.
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-10.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics