Creating a Java Product Class with Encapsulation and discount Method
Java Encapsulatiion: Exercise-14 with Solution
Write a Java program to create a class called Product with private instance variables productName, productCode, and price. Provide public getter and setter methods to access and modify these variables. Add a method called applyDiscount() that takes a percentage and reduces the price by that percentage.
Sample Solution:
Java Code:
Product.java
// Define the Product class
public class Product {
// Private instance variables
private String productName;
private String productCode;
private double price;
// Public getter for the productName variable
public String getProductName() {
return productName;
}
// Public setter for the productName variable
public void setProductName(String productName) {
this.productName = productName;
}
// Public getter for the productCode variable
public String getProductCode() {
return productCode;
}
// Public setter for the productCode variable
public void setProductCode(String productCode) {
this.productCode = productCode;
}
// Public getter for the price variable
public double getPrice() {
return price;
}
// Public setter for the price variable
public void setPrice(double price) {
this.price = price;
}
// Method to apply a discount percentage to the price
public void applyDiscount(double percentage) {
if (percentage > 0 && percentage <= 100) {
this.price -= this.price * (percentage / 100);
}
}
}
Main.java
public class Main {
// Main method to test the Product class
public static void main(String[] args) {
// Create a new Product object
Product product = new Product();
// Set the product name, code, and initial price
product.setProductName("Laptop");
product.setProductCode("LT01233");
product.setPrice(1100.00);
// Apply a 8% discount to the product
product.applyDiscount(8);
// Print the details of the product
System.out.println("Product Name: " + product.getProductName());
System.out.println("Product Code: " + product.getProductCode());
System.out.println("Price after discount: $" + product.getPrice());
}
}
Output:
Product Name: Laptop Product Code: LT01233 Price after discount: $1012.0
Explanation:
- Private Instance Variables: The productName, productCode, and price variables are declared as private to ensure encapsulation.
- Public Getters and Setters: These methods provide controlled access to the private variables.
- getProductName(): Returns the product name.
- setProductName(String productName): Sets the product name.
- getProductCode(): Returns the product code.
- setProductCode(String productCode): Sets the product code.
- getPrice(): Returns the price.
- setPrice(double price): Sets the price.
- applyDiscount Method: Takes a percentage and reduces the price by that percentage if the percentage is valid.
- Main Method: Tests the functionality of the Product class by creating an instance, setting its properties, applying a discount, and printing the details.
Note on Encapsulation
Encapsulation works in the above exercise by:
- Hiding Data: The private instance variables productName, productCode, and price 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 Movie Class with Encapsulation and Details Method.
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-14.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics