0% found this document useful (0 votes)
4 views39 pages

? Java Encapsulation Masterrrrrrr

The document provides Java programming examples demonstrating the concept of encapsulation through various classes such as Person, BankAccount, Rectangle, Employee, Circle, Car, Student, Book, Smartphone, Desktop, and House. Each class includes private instance variables with public getter and setter methods, along with specific methods for additional functionality. The examples illustrate how to create objects, set values, and display the results in a main method.

Uploaded by

grao23456
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views39 pages

? Java Encapsulation Masterrrrrrr

The document provides Java programming examples demonstrating the concept of encapsulation through various classes such as Person, BankAccount, Rectangle, Employee, Circle, Car, Student, Book, Smartphone, Desktop, and House. Each class includes private instance variables with public getter and setter methods, along with specific methods for additional functionality. The examples illustrate how to create objects, set values, and display the results in a main method.

Uploaded by

grao23456
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🔐✨ Java Encapsulation Mastery 🚀📦

🌟 1️⃣ Person Class


❓ Question
Write a Java program to create a class called Person with private instance variables name,
age, and country. Provide public getter and setter methods to access and modify these
variables.

// Person class demonstrating encapsulation

class Person {

// Private instance variables

private String name;

private int age;

private String country;

// Getter method for name

public String getName() {

return name;

// Setter method for name

Soumik Karmakar - Object Oriented Programming with Java


public void setName(String name) {

[Link] = name;

// Getter method for age

public int getAge() {

return age;

// Setter method for age

public void setAge(int age) {

[Link] = age;

// Getter method for country

public String getCountry() {

return country;

// Setter method for country

public void setCountry(String country) {

[Link] = country;

Soumik Karmakar - Object Oriented Programming with Java


public class Main {

public static void main(String[] args) {

// Creating Person object

Person p = new Person();

// Setting values

[Link]("Soumik");

[Link](22);

[Link]("India");

// Displaying values

[Link]("Name: " + [Link]());

[Link]("Age: " + [Link]());

[Link]("Country: " + [Link]());

🌟 2️⃣ BankAccount Class


❓ Question

Soumik Karmakar - Object Oriented Programming with Java


Write a Java program to create a class called BankAccount with private instance variables
accountNumber and balance. Provide public getter and setter methods to access and modify
these variables.

// BankAccount class demonstrating encapsulation

class BankAccount {

// Private variables

private String accountNumber;

private double balance;

// Getter for account number

public String getAccountNumber() {

return accountNumber;

// Setter for account number

public void setAccountNumber(String accountNumber) {

[Link] = accountNumber;

// Getter for balance

public double getBalance() {

return balance;

Soumik Karmakar - Object Oriented Programming with Java


// Setter for balance

public void setBalance(double balance) {

[Link] = balance;

public class Main {

public static void main(String[] args) {

// Creating object

BankAccount b = new BankAccount();

// Setting values

[Link]("ACC12345");

[Link](50000);

// Displaying values

[Link]("Account Number: " + [Link]());

[Link]("Balance: " + [Link]());

Soumik Karmakar - Object Oriented Programming with Java


🌟 3️⃣ Rectangle Class
❓ Question
Write a Java program to create a class called Rectangle with private instance variables
length and width. Provide public getter and setter methods to access and modify these
variables.

// Rectangle class

class Rectangle {

// Private variables

private double length;

private double width;

// Getter for length

public double getLength() {

return length;

// Setter for length

public void setLength(double length) {

[Link] = length;

// Getter for width

public double getWidth() {

Soumik Karmakar - Object Oriented Programming with Java


return width;

// Setter for width

public void setWidth(double width) {

[Link] = width;

public class Main {

public static void main(String[] args) {

// Creating object

Rectangle r = new Rectangle();

// Setting values

[Link](10);

[Link](5);

// Displaying values

[Link]("Length: " + [Link]());

[Link]("Width: " + [Link]());

Soumik Karmakar - Object Oriented Programming with Java


🌟 4️⃣ Employee Class 👨‍💼
❓ Question
Write a Java program to create a class called Employee with private instance variables
employee_id, employee_name, and employee_salary. Provide public getter and setter
methods to access and modify the employee_id and employee_name variables, but provide
only a getter method for the employee_salary variable that returns a formatted string.

// Employee class

class Employee {

// Private variables

private int employee_id;

private String employee_name;

private double employee_salary;

// Constructor

Employee(double salary) {

employee_salary = salary;

// Getter for employee id

public int getEmployeeId() {

return employee_id;

Soumik Karmakar - Object Oriented Programming with Java


}

// Setter for employee id

public void setEmployeeId(int employee_id) {

this.employee_id = employee_id;

// Getter for employee name

public String getEmployeeName() {

return employee_name;

// Setter for employee name

public void setEmployeeName(String employee_name) {

this.employee_name = employee_name;

// Getter for formatted salary

public String getEmployeeSalary() {

return "₹" + employee_salary;

public class Main {

Soumik Karmakar - Object Oriented Programming with Java


public static void main(String[] args) {

// Creating object

Employee e = new Employee(75000);

// Setting values

[Link](101);

[Link]("Rahul");

// Displaying values

[Link]("ID: " + [Link]());

[Link]("Name: " + [Link]());

[Link]("Salary: " + [Link]());

🌟 5️⃣ Circle Class ⭕


❓ Question
Write a Java program to create a class called Circle with a private instance variable radius.
Provide public getter and setter methods to access and modify the radius variable. Also, provide
two methods called calculateArea() and calculatePerimeter() that return the
calculated area and perimeter based on the current radius value.

// Circle class

Soumik Karmakar - Object Oriented Programming with Java


class Circle {

// Private variable

private double radius;

// Getter method

public double getRadius() {

return radius;

// Setter method

public void setRadius(double radius) {

[Link] = radius;

// Method to calculate area

public double calculateArea() {

return [Link] * radius * radius;

// Method to calculate perimeter

public double calculatePerimeter() {

return 2 * [Link] * radius;

Soumik Karmakar - Object Oriented Programming with Java


}

public class Main {

public static void main(String[] args) {

// Creating object

Circle c = new Circle();

// Setting radius

[Link](7);

// Displaying results

[Link]("Radius: " + [Link]());

[Link]("Area: " + [Link]());

[Link]("Perimeter: " + [Link]());

🌟 6️⃣ Car Class 🚗


❓ Question
Write a Java program to create a class called Car with private instance variables
company_name, model_name, year, and mileage. Provide public getter and setter methods

Soumik Karmakar - Object Oriented Programming with Java


to access and modify the company_name, model_name, and year variables. However, provide
only a getter method for the mileage variable.

// Car class

class Car {

// Private variables

private String company_name;

private String model_name;

private int year;

private double mileage;

// Constructor

Car(double mileage) {

[Link] = mileage;

// Getter for company name

public String getCompanyName() {

return company_name;

// Setter for company name

public void setCompanyName(String company_name) {

this.company_name = company_name;

Soumik Karmakar - Object Oriented Programming with Java


// Getter for model name

public String getModelName() {

return model_name;

// Setter for model name

public void setModelName(String model_name) {

this.model_name = model_name;

// Getter for year

public int getYear() {

return year;

// Setter for year

public void setYear(int year) {

[Link] = year;

// Getter only for mileage

public double getMileage() {

return mileage;

Soumik Karmakar - Object Oriented Programming with Java


}

public class Main {

public static void main(String[] args) {

// Creating object

Car c = new Car(18.5);

// Setting values

[Link]("BMW");

[Link]("X6");

[Link](2025);

// Displaying values

[Link]("Company: " + [Link]());

[Link]("Model: " + [Link]());

[Link]("Year: " + [Link]());

[Link]("Mileage: " + [Link]());

Soumik Karmakar - Object Oriented Programming with Java


🌟 7️⃣ Student Class 🎓
❓ Question
Write a Java program to create a class called Student with private instance variables
student_id, student_name, and grades. Provide public getter and setter methods to
access and modify the student_id and student_name variables. Also, provide a method
called addGrade() that allows adding a grade to the grades variable while performing
additional validation.

import [Link];

// Student class

class Student {

// Private variables

private int student_id;

private String student_name;

private ArrayList<Integer> grades = new ArrayList<>();

// Getter for student id

public int getStudentId() {

return student_id;

// Setter for student id

public void setStudentId(int student_id) {

this.student_id = student_id;

Soumik Karmakar - Object Oriented Programming with Java


}

// Getter for student name

public String getStudentName() {

return student_name;

// Setter for student name

public void setStudentName(String student_name) {

this.student_name = student_name;

// Method to add grade with validation

public void addGrade(int grade) {

// Checking valid grade

if (grade >= 0 && grade <= 100) {

[Link](grade);

[Link]("Grade added successfully");

} else {

[Link]("Invalid grade");

Soumik Karmakar - Object Oriented Programming with Java


// Method to display grades

public void displayGrades() {

[Link](grades);

public class Main {

public static void main(String[] args) {

// Creating object

Student s = new Student();

// Setting values

[Link](1);

[Link]("Amit");

// Adding grades

[Link](90);

[Link](105);

// Displaying grades

[Link]();

Soumik Karmakar - Object Oriented Programming with Java


🌟 8️⃣ Book Class 📚
❓ Question
Write a Java program to create a class called Book with private instance variables title,
author, and price. Provide public getter and setter methods to access and modify these
variables. Add a method called applyDiscount() that takes a percentage as a parameter
and reduces the price by that percentage.

// Book class

class Book {

// Private variables

private String title;

private String author;

private double price;

// Getter for title

public String getTitle() {

return title;

// Setter for title

public void setTitle(String title) {

[Link] = title;

Soumik Karmakar - Object Oriented Programming with Java


}

// Getter for author

public String getAuthor() {

return author;

// Setter for author

public void setAuthor(String author) {

[Link] = author;

// Getter for price

public double getPrice() {

return price;

// Setter for price

public void setPrice(double price) {

[Link] = price;

// Method to apply discount

public void applyDiscount(double percentage) {

Soumik Karmakar - Object Oriented Programming with Java


price = price - (price * percentage / 100);

public class Main {

public static void main(String[] args) {

// Creating object

Book b = new Book();

// Setting values

[Link]("Java Programming");

[Link]("James Gosling");

[Link](1000);

// Applying discount

[Link](20);

// Displaying final price

[Link]("Price after discount: " + [Link]());

Soumik Karmakar - Object Oriented Programming with Java


🌟 9️⃣ Smartphone Class 📱
❓ Question
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.

// Smartphone class

class Smartphone {

// Private variables

private String brand;

private String model;

private int storageCapacity;

// Getter for brand

public String getBrand() {

return brand;

// Setter for brand

public void setBrand(String brand) {

[Link] = brand;

Soumik Karmakar - Object Oriented Programming with Java


// Getter for model

public String getModel() {

return model;

// Setter for model

public void setModel(String model) {

[Link] = model;

// Getter for storage capacity

public int getStorageCapacity() {

return storageCapacity;

// Setter for storage capacity

public void setStorageCapacity(int storageCapacity) {

[Link] = storageCapacity;

// Method to increase storage

public void increaseStorage(int extraStorage) {

storageCapacity += extraStorage;

Soumik Karmakar - Object Oriented Programming with Java


}

public class Main {

public static void main(String[] args) {

// Creating object

Smartphone s = new Smartphone();

// Setting values

[Link]("Samsung");

[Link]("S25 Ultra");

[Link](256);

// Increasing storage

[Link](128);

// Displaying updated storage

[Link]("Updated Storage: " + [Link]() + " GB");

🌟 🔟 Desktop Class 🖥️
Soumik Karmakar - Object Oriented Programming with Java
❓ Question
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.

// Desktop class

class Desktop {

// Private variables

private String brand;

private String processor;

private int ramSize;

// Getter for brand

public String getBrand() {

return brand;

// Setter for brand

public void setBrand(String brand) {

[Link] = brand;

// Getter for processor

public String getProcessor() {

Soumik Karmakar - Object Oriented Programming with Java


return processor;

// Setter for processor

public void setProcessor(String processor) {

[Link] = processor;

// Getter for RAM size

public int getRamSize() {

return ramSize;

// Setter for RAM size

public void setRamSize(int ramSize) {

[Link] = ramSize;

// Method to upgrade RAM

public void upgradeRam(int extraRam) {

ramSize += extraRam;

Soumik Karmakar - Object Oriented Programming with Java


public class Main {

public static void main(String[] args) {

// Creating object

Desktop d = new Desktop();

// Setting values

[Link]("HP");

[Link]("Intel i7");

[Link](16);

// Upgrading RAM

[Link](16);

// Displaying updated RAM

[Link]("Updated RAM: " + [Link]() + " GB");

🌟 1️⃣1️⃣House Class 🏠
❓ Question

Soumik Karmakar - Object Oriented Programming with Java


Write a Java program to create a class called House with private instance variables address,
numberOfRooms, and area. Provide public getter and setter methods to access and modify
these variables. Add a method called calculatePrice() that returns the price of the house
based on its area and a price per square meter.

// House class

class House {

// Private variables

private String address;

private int numberOfRooms;

private double area;

// Getter for address

public String getAddress() {

return address;

// Setter for address

public void setAddress(String address) {

[Link] = address;

// Getter for number of rooms

public int getNumberOfRooms() {

return numberOfRooms;

Soumik Karmakar - Object Oriented Programming with Java


// Setter for number of rooms

public void setNumberOfRooms(int numberOfRooms) {

[Link] = numberOfRooms;

// Getter for area

public double getArea() {

return area;

// Setter for area

public void setArea(double area) {

[Link] = area;

// Method to calculate house price

public double calculatePrice(double pricePerSquareMeter) {

return area * pricePerSquareMeter;

public class Main {

public static void main(String[] args) {

Soumik Karmakar - Object Oriented Programming with Java


// Creating object

House h = new House();

// Setting values

[Link]("Kolkata");

[Link](4);

[Link](1200);

// Displaying house price

[Link]("House Price: " + [Link](5000));

🌟 1️⃣2️⃣Account Class 💳
❓ Question
Write a Java program to create a class called Account with private instance variables
accountNumber, accountHolder, and balance. Provide public getter and setter methods to
access and modify these variables. Add a method called deposit() that takes an amount and
increases the balance by that amount, and a method called withdraw() that takes an amount
and decreases the balance by that amount.

// Account class

class Account {

Soumik Karmakar - Object Oriented Programming with Java


// Private variables

private String accountNumber;

private String accountHolder;

private double balance;

// Getter for account number

public String getAccountNumber() {

return accountNumber;

// Setter for account number

public void setAccountNumber(String accountNumber) {

[Link] = accountNumber;

// Getter for account holder

public String getAccountHolder() {

return accountHolder;

// Setter for account holder

public void setAccountHolder(String accountHolder) {

[Link] = accountHolder;

Soumik Karmakar - Object Oriented Programming with Java


}

// Getter for balance

public double getBalance() {

return balance;

// Setter for balance

public void setBalance(double balance) {

[Link] = balance;

// Method to deposit money

public void deposit(double amount) {

balance += amount;

// Method to withdraw money

public void withdraw(double amount) {

// Checking sufficient balance

if (amount <= balance) {

balance -= amount;

} else {

Soumik Karmakar - Object Oriented Programming with Java


[Link]("Insufficient Balance");

public class Main {

public static void main(String[] args) {

// Creating object

Account a = new Account();

// Setting values

[Link]("ACC1001");

[Link]("Rahul");

[Link](10000);

// Performing transactions

[Link](5000);

[Link](3000);

// Displaying balance

[Link]("Final Balance: " + [Link]());

Soumik Karmakar - Object Oriented Programming with Java


🌟 1️⃣3️⃣Movie Class 🎬
❓ Question
Write a Java program to create a class called Movie with private instance variables title,
director, and duration. Provide public getter and setter methods to access and modify
these variables. Add a method called getMovieDetails() that returns a formatted string
containing the movie details.

// Movie class

class Movie {

// Private variables

private String title;

private String director;

private int duration;

// Getter for title

public String getTitle() {

return title;

// Setter for title

public void setTitle(String title) {

[Link] = title;

Soumik Karmakar - Object Oriented Programming with Java


}

// Getter for director

public String getDirector() {

return director;

// Setter for director

public void setDirector(String director) {

[Link] = director;

// Getter for duration

public int getDuration() {

return duration;

// Setter for duration

public void setDuration(int duration) {

[Link] = duration;

// Method to return movie details

public String getMovieDetails() {

Soumik Karmakar - Object Oriented Programming with Java


return "Movie: " + title +

"\nDirector: " + director +

"\nDuration: " + duration + " minutes";

public class Main {

public static void main(String[] args) {

// Creating object

Movie m = new Movie();

// Setting values

[Link]("Inception");

[Link]("Christopher Nolan");

[Link](148);

// Displaying movie details

[Link]([Link]());

Soumik Karmakar - Object Oriented Programming with Java


🌟 1️⃣4️⃣Product Class 🛍️
❓ Question
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
as a parameter and reduces the price by that percentage.

// Product class

class Product {

// Private variables

private String productName;

private String productCode;

private double price;

// Getter for product name

public String getProductName() {

return productName;

// Setter for product name

public void setProductName(String productName) {

[Link] = productName;

Soumik Karmakar - Object Oriented Programming with Java


// Getter for product code

public String getProductCode() {

return productCode;

// Setter for product code

public void setProductCode(String productCode) {

[Link] = productCode;

// Getter for price

public double getPrice() {

return price;

// Setter for price

public void setPrice(double price) {

[Link] = price;

// Method to apply discount

public void applyDiscount(double percentage) {

price = price - (price * percentage / 100);

Soumik Karmakar - Object Oriented Programming with Java


}

public class Main {

public static void main(String[] args) {

// Creating object

Product p = new Product();

// Setting values

[Link]("Laptop");

[Link]("LP101");

[Link](80000);

// Applying discount

[Link](10);

// Displaying final price

[Link]("Final Price: " + [Link]());

Soumik Karmakar - Object Oriented Programming with Java

You might also like