Practice 70 Java Encapsulation Programming Questions, TechnoVlogs

Practice 70 Java Encapsulation Programming Questions


Q1. Write a Java program to create a class Person with private fields name and age. Provide public getter and setter methods to access and modify these fields.  
Input:  
Set name = "John" and age = 25  
Expected Output:  
Name: John  
Age: 25  

Q2. Write a Java program to create a class BankAccount with private fields balance and accountNumber. Provide methods to deposit, withdraw, and check the balance.  
Input:  
Deposit: ₹5000, Withdraw: ₹1000  
Expected Output:  
Balance after deposit: ₹5000  
Balance after withdrawal: ₹4000  

Q3. Write a Java program to create a class Employee with private fields employeeId and salary. Provide methods to get and set these values. 
Input:  
Set employeeId = 101 and salary = ₹50000  
Expected Output:  
Employee ID: 101  
Salary: ₹50000  

Q4. Write a Java program to create a class Rectangle with private fields length and width. Provide public methods to calculate and return the area of the rectangle.  
Input:  
Set length = 5 and width = 3  
Expected Output:  
Area of Rectangle: 15  

Q5. Write a Java program to create a class Student with private fields name, rollNo, and marks. Provide public methods to get and set these values and calculate the average marks.  
Input:  
Set name = "Alice", rollNo = 10, marks = 85, 90, 78  
Expected Output:  
Name: Alice  
Roll No: 10  
Average Marks: 84.33  

Q6. Write a Java program to create a class Product with private fields productName, price, and quantity. Provide public methods to set and get these fields, and calculate the total value of the product.  
Input:  
Set productName = "Laptop", price = ₹50000, quantity = 2  
Expected Output:  
Product Name: Laptop  
Total Value: ₹100000  

Q7. Write a Java program to create a class Car with private fields brand, model, and year. Provide public getter and setter methods to access and modify these fields.  
Input:  
Set brand = "Toyota", model = "Camry", year = 2020  
Expected Output:  
Brand: Toyota  
Model: Camry  
Year: 2020  

Q8. Write a Java program to create a class Book with private fields title, author, and price. Provide getter and setter methods for these fields and a method to calculate the discounted price.  
Input:  
Set title = "Java Programming", author = "John Doe", price = ₹300, discount = 10%  
Expected Output:  
Discounted Price: ₹270  

Q9. Write a Java program to create a class Movie with private fields title, director, and rating. Provide getter and setter methods and a method to display movie details.  
Input:  
Set title = "Inception", director = "Christopher Nolan", rating = 8.8  
Expected Output:  
Movie Title: Inception  
Director: Christopher Nolan  
Rating: 8.8  

Q10. Write a Java program to create a class Account with private fields accountHolderName and balance. Provide methods to deposit and withdraw money, ensuring the balance never goes below zero.  
Input:  
Deposit: ₹1000, Withdraw: ₹2000  
Expected Output:  
Balance after deposit: ₹1000  
Balance after withdrawal: ₹0  

Q11. Write a Java program to create a class Circle with a private field radius. Provide a public method to calculate and return the area of the circle.  
Input:  
Set radius = 7  
Expected Output:  
Area of Circle: 153.94  

Q12. Write a Java program to create a class BankAccount with private fields accountHolder and balance. Provide public methods to deposit, withdraw, and check balance, ensuring that balance cannot be negative.  
Input:  
Deposit: ₹5000, Withdraw: ₹7000  
Expected Output:  
Balance after deposit: ₹5000  
Balance after withdrawal: ₹0  

Q13. Write a Java program to create a class Date with private fields day, month, and year. Provide public methods to set and get the date, ensuring the day is valid for the given month.  
Input:  
Set day = 30, month = 2, year = 2022  
Expected Output:  
Invalid Date (February doesn't have 30 days in 2022)  

Q14. Write a Java program to create a class Person with private fields age and name. Provide a public method to check if the person is eligible to vote (age >= 18).  
Input:  
Set name = "John", age = 20  
Expected Output:  
John is eligible to vote  

Q15. Write a Java program to create a class Library with private fields bookTitle, authorName, and availability. Provide public methods to get and set these fields, and check if the book is available.  
Input:  
Set bookTitle = "Java Basics", authorName = "Alice", availability = true  
Expected Output:  
Java Basics by Alice is available  

Q16. Write a Java program to create a class Account with private fields accountNumber and accountHolderName. Provide getter and setter methods and a method to display account details.  
Input:  
Set accountNumber = 12345, accountHolderName = "John Doe"  
Expected Output:  
Account Number: 12345  
Account Holder: John Doe  

Q17. Write a Java program to create a class Employee with private fields employeeId and name. Provide setter and getter methods for these fields and display the employee details.  
Input:  
Set employeeId = 1001, name = "David"  
Expected Output:  
Employee ID: 1001  
Name: David  

Q18. Write a Java program to create a class Vehicle with private fields make and model. Provide setter and getter methods for these fields and a method to display vehicle details.  
Input:  
Set make = "Honda", model = "Civic"  
Expected Output:  
Make: Honda  
Model: Civic  

Q19. Write a Java program to create a class Person with private fields name, age, and address. Provide getter and setter methods for each field and display the person's details.  
Input:  
Set name = "Sam", age = 30, address = "123 Main St."  
Expected Output:  
Name: Sam  
Age: 30  
Address: 123 Main St.  

Q20. Write a Java program to create a class BankAccount with private fields accountHolderName and balance. Provide methods to check if there are sufficient funds before withdrawal.  
Input:  
Deposit: ₹5000, Withdraw: ₹7000  
Expected Output:  
Insufficient funds for withdrawal  

Q21. Write a Java program to create a class Person with private fields firstName, lastName, and age. Provide getter and setter methods for these fields and display the full name of the person.  
Input:  
Set firstName = "John", lastName = "Doe", age = 30  
Expected Output:  
Full Name: John Doe  

Q22. Write a Java program to create a class Circle with a private field radius. Provide getter and setter methods for radius, and a method to calculate the circumference of the circle.  
Input:  
Set radius = 7  
Expected Output:  
Circumference: 43.98  

Q23. Write a Java program to create a class Car with private fields make, model, year, and price. Provide setter and getter methods and a method to display car details.  
Input:  
Set make = "Toyota", model = "Corolla", year = 2020, price = ₹1500000  
Expected Output:  
Make: Toyota  
Model: Corolla  
Year: 2020  
Price: ₹1500000  

Q24. Write a Java program to create a class Book with private fields title, author, and price. Provide setter and getter methods and a method to display book details.  
Input:  
Set title = "Java Programming", author = "John Doe", price = ₹500  
Expected Output:  
Title: Java Programming  
Author: John Doe  
Price: ₹500  

Q25. Write a Java program to create a class Student with private fields name, rollNo, and marks. Provide setter and getter methods and a method to calculate total marks.  
Input:  
Set name = "Alice", rollNo = 12, marks = 85, 90, 78  
Expected Output:  
Name: Alice  
Roll No: 12  
Total Marks: 253  

Q26. Write a Java program to create a class Account with private fields accountNumber and balance. Provide setter and getter methods and a method to check the balance before withdrawing.  
Input:  
Set accountNumber = 101, balance = ₹10000  
Withdraw: ₹2000  
Expected Output:  
Balance after withdrawal: ₹8000  

Q27. Write a Java program to create a class Laptop with private fields brand, model, and price. Provide getter and setter methods and a method to apply a discount to the price.  
Input:  
Set brand = "Dell", model = "XPS 13", price = ₹80000  
Discount: 10%  
Expected Output:  
Discounted Price: ₹72000  

Q28. Write a Java program to create a class Movie with private fields title, director, and releaseYear. Provide setter and getter methods and a method to display movie details.  
Input:  
Set title = "Inception", director = "Christopher Nolan", releaseYear = 2010  
Expected Output:  
Title: Inception  
Director: Christopher Nolan  
Release Year: 2010  

Q29. Write a Java program to create a class Employee with private fields employeeId, name, and salary. Provide getter and setter methods and a method to calculate annual salary.  
Input:  
Set employeeId = 101, name = "David", salary = ₹50000  
Expected Output:  
Annual Salary: ₹600000  

Q30. Write a Java program to create a class BankAccount with private fields accountHolderName, accountNumber, and balance. Provide methods to deposit and withdraw money. Ensure that the balance is updated properly after each transaction.  
Input:  
Deposit: ₹2000, Withdraw: ₹500  
Expected Output:  
Balance after deposit: ₹2000  
Balance after withdrawal: ₹1500  

Q31. Write a Java program to create a class Person with private fields name and age. Provide setter and getter methods and a method to check if the person is a minor (age < 18).  
Input:  
Set name = "John", age = 16  
Expected Output:  
John is a minor  

Q32. Write a Java program to create a class Product with private fields productId, productName, and price. Provide getter and setter methods and a method to calculate discount on the product.  
Input:  
Set productId = 101, productName = "Mobile", price = ₹20000  
Discount: 15%  
Expected Output:  
Discounted Price: ₹17000  

Q33. Write a Java program to create a class Library with private fields bookTitle and authorName. Provide setter and getter methods for these fields and a method to check if the book is available in the library.  
Input:  
Set bookTitle = "Java Basics", authorName = "Alice", available = true  
Expected Output:  
Book "Java Basics" by Alice is available  

Q34. Write a Java program to create a class Shape with a private field color. Provide setter and getter methods for color and a method to display the color of the shape.  
Input:  
Set color = "Red"  
Expected Output:  
Shape color is Red  

Q35. Write a Java program to create a class Student with private fields name, rollNo, and marks. Provide setter and getter methods and a method to calculate the grade based on the marks.  
Input:  
Set name = "Alice", rollNo = 10, marks = 85  
Expected Output:  
Grade: A  

Q36. Write a Java program to create a class Account with private fields accountHolderName, accountNumber, and balance. Provide setter and getter methods for these fields and a method to check the balance after a deposit.  
Input:  
Set accountHolderName = "John", accountNumber = 101, balance = ₹5000  
Deposit: ₹2000  
Expected Output:  
Balance after deposit: ₹7000  

Q37. Write a Java program to create a class Car with private fields brand, model, and year. Provide getter and setter methods and a method to display car details.  
Input:  
Set brand = "BMW", model = "X5", year = 2022  
Expected Output:  
Brand: BMW  
Model: X5  
Year: 2022  

Q38. Write a Java program to create a class Person with private fields firstName, lastName, and age. Provide setter and getter methods and a method to check if the person is eligible for a senior citizen discount (age >= 60).  
Input:  
Set firstName = "John", lastName = "Doe", age = 65  
Expected Output:  
John Doe is eligible for a senior citizen discount  

Q39. Write a Java program to create a class Product with private fields productName and productPrice. Provide getter and setter methods for these fields and a method to calculate the total price of multiple products.  
Input:  
Set productName = "Laptop", productPrice = ₹50000  
Quantity = 3  
Expected Output:  
Total Price: ₹150000  

Q40. Write a Java program to create a class Movie with private fields title, director, and rating. Provide getter and setter methods and a method to display movie details.  
Input:  
Set title = "The Matrix", director = "Wachowski", rating = 8.7  
Expected Output:  
Title: The Matrix  
Director: Wachowski  
Rating: 8.7  

Q41. Write a Java program to create a class Library with private fields bookTitle and bookAuthor. Provide setter and getter methods and a method to display the book's details.  
Input:  
Set bookTitle = "Effective Java", bookAuthor = "Joshua Bloch"  
Expected Output:  
Book Title: Effective Java  
Author: Joshua Bloch  

Q42. Write a Java program to create a class Student with private fields name, age, and grade. Provide setter and getter methods and a method to check if the student is passing (grade >= 50).  
Input:  
Set name = "Alice", age = 18, grade = 60  
Expected Output:  
Alice is passing  

Q43. Write a Java program to create a class BankAccount with private fields accountHolderName and balance. Provide getter and setter methods and a method to check if the balance is positive.  
Input:  
Set accountHolderName = "John", balance = ₹2000  
Expected Output:  
Balance is positive: ₹2000  

Q44. Write a Java program to create a class Employee with private fields name, employeeId, and salary. Provide setter and getter methods and a method to calculate annual salary.  
Input:  
Set name = "David", employeeId = 1001, salary = ₹50000  
Expected Output:  
Annual Salary: ₹600000  

Q45. Write a Java program to create a class Product with private fields productName and productPrice. Provide setter and getter methods and a method to calculate discounted price.  
Input:  
Set productName = "Smartphone", productPrice = ₹20000  
Discount: 10%  
Expected Output:  
Discounted Price: ₹18000  

Q46. Write a Java program to create a class Person with private fields name, age, and address. Provide setter and getter methods and a method to display the full address.  
Input:  
Set name = "Alice", age = 25, address = "123 Main Street"  
Expected Output:  
Name: Alice  
Age: 25  
Address: 123 Main Street  

Q47. Write a Java program to create a class Student with private fields name, rollNo, and marks. Provide setter and getter methods and a method to calculate the average of marks.  
Input:  
Set name = "John", rollNo = 12, marks = 85, 90, 78  
Expected Output:  
Average Marks: 84.33  

Q48. Write a Java program to create a class Car with private fields make, model, year, and price. Provide getter and setter methods and a method to calculate the age of the car based on the current year.  
Input:  
Set make = "Toyota", model = "Camry", year = 2015, price = ₹1000000  
Current Year: 2025  
Expected Output:  
Age of the car: 10 years  

Q49. Write a Java program to create a class Rectangle with private fields length and width. Provide setter and getter methods and a method to calculate the perimeter of the rectangle.  
Input:  
Set length = 10, width = 5  
Expected Output:  
Perimeter of Rectangle: 30  

Q50. Write a Java program to create a class Movie with private fields title, director, releaseYear, and rating. Provide setter and getter methods and a method to display movie details.  
Input:  
Set title = "The Matrix", director = "Wachowski", releaseYear = 1999, rating = 8.7  
Expected Output:  
Title: The Matrix  
Director: Wachowski  
Release Year: 1999  
Rating: 8.7  

Q51. Write a Java program to create a class Employee with private fields name, employeeId, and department. Provide setter and getter methods and a method to display employee details.  
Input:  
Set name = "Sarah", employeeId = 101, department = "HR"  
Expected Output:  
Employee Name: Sarah  
Employee ID: 101  
Department: HR  

Q52. Write a Java program to create a class Product with private fields productName and productPrice. Provide setter and getter methods and a method to apply a discount to the price.  
Input:  
Set productName = "Headphones", productPrice = ₹1500  
Discount: 20%  
Expected Output:  
Discounted Price: ₹1200  

Q53. Write a Java program to create a class Customer with private fields name, address, and contactNumber. Provide setter and getter methods and a method to display customer details.  
Input:  
Set name = "James", address = "456 Oak Street", contactNumber = "1234567890"  
Expected Output:  
Name: James  
Address: 456 Oak Street  
Contact Number: 1234567890  

Q54. Write a Java program to create a class Circle with private fields radius and color. Provide setter and getter methods and a method to calculate the area of the circle.  
Input:  
Set radius = 5, color = "Blue"  
Expected Output:  
Area of Circle: 78.54  
Circle Color: Blue  

Q55. Write a Java program to create a class Account with private fields accountNumber, accountHolder, and balance. Provide setter and getter methods and a method to deposit money into the account.  
Input:  
Set accountNumber = 2021, accountHolder = "Tom", balance = ₹3000  
Deposit: ₹1000  
Expected Output:  
Balance after deposit: ₹4000  

Q56. Write a Java program to create a class Book with private fields title, author, and price. Provide setter and getter methods and a method to check if the book is available in stock.  
Input:  
Set title = "Java Basics", author = "Mark", price = ₹500  
Availability: true  
Expected Output:  
Book "Java Basics" by Mark is available  

Q57. Write a Java program to create a class Student with private fields name, rollNo, and marks. Provide setter and getter methods and a method to calculate the grade based on marks (>= 80 = A, 60 to 79 = B, < 60 = C).  
Input:  
Set name = "Anna", rollNo = 23, marks = 75  
Expected Output:  
Grade: B  

Q58. Write a Java program to create a class Shape with private fields color and type. Provide setter and getter methods and a method to display shape details.  
Input:  
Set color = "Red", type = "Circle"  
Expected Output:  
Shape: Circle  
Color: Red  

Q59. Write a Java program to create a class Employee with private fields name, employeeId, and salary. Provide getter and setter methods and a method to calculate the monthly salary after tax deduction (10% tax).  
Input:  
Set name = "David", employeeId = 101, salary = ₹50000  
Expected Output:  
Monthly Salary after tax: ₹45000  

Q60. Write a Java program to create a class Order with private fields orderId, customerName, and orderAmount. Provide setter and getter methods and a method to apply a discount to the order.  
Input:  
Set orderId = 1001, customerName = "John", orderAmount = ₹2000  
Discount: 15%  
Expected Output:  
Discounted Order Amount: ₹1700  

Q61. Write a Java program to create a class Transaction with private fields transactionId, amount, and transactionDate. Provide setter and getter methods and a method to display transaction details.  
Input:  
Set transactionId = 201, amount = ₹1500, transactionDate = "2025-01-02"  
Expected Output:  
Transaction ID: 201  
Amount: ₹1500  
Date: 2025-01-02  

Q62. Write a Java program to create a class Circle with private fields radius and color. Provide getter and setter methods and a method to calculate the diameter of the circle.  
Input:  
Set radius = 7, color = "Green"  
Expected Output:  
Diameter of Circle: 14  
Circle Color: Green  

Q63. Write a Java program to create a class Employee with private fields employeeId, employeeName, and employeeSalary. Provide setter and getter methods and a method to display employee details.  
Input:  
Set employeeId = 101, employeeName = "Sarah", employeeSalary = ₹40000  
Expected Output:  
Employee ID: 101  
Employee Name: Sarah  
Employee Salary: ₹40000  

Q64. Write a Java program to create a class Account with private fields accountHolderName and balance. Provide setter and getter methods and a method to withdraw money from the account, ensuring that the balance is sufficient.  
Input:  
Set accountHolderName = "Tom", balance = ₹3000  
Withdraw: ₹1000  
Expected Output:  
Balance after withdrawal: ₹2000  

Q65. Write a Java program to create a class Rectangle with private fields length and width. Provide getter and setter methods and a method to calculate the area and perimeter of the rectangle.  
Input:  
Set length = 10, width = 5  
Expected Output:  
Area: 50  
Perimeter: 30  

Q66. Write a Java program to create a class Movie with private fields movieTitle, director, and releaseYear. Provide setter and getter methods and a method to display movie details.  
Input:  
Set movieTitle = "Titanic", director = "James Cameron", releaseYear = 1997  
Expected Output:  
Movie Title: Titanic  
Director: James Cameron  
Release Year: 1997  

Q67. Write a Java program to create a class Person with private fields firstName, lastName, and age. Provide setter and getter methods and a method to display the full name and age.  
Input:  
Set firstName = "John", lastName = "Doe", age = 30  
Expected Output:  
Full Name: John Doe  
Age: 30  

Q68. Write a Java program to create a class Product with private fields productName and productPrice. Provide setter and getter methods and a method to display the product details.  
Input:  
Set productName = "Washing Machine", productPrice = ₹15000  
Expected Output:  
Product Name: Washing Machine  
Product Price: ₹15000  

Q69. Write a Java program to create a class Order with private fields orderId, productName, and quantity. Provide setter and getter methods and a method to calculate total price for the order.  
Input:  
Set orderId = 123, productName = "Shampoo", quantity = 3, price = ₹200  
Expected Output:  
Total Price: ₹600  

Q70. Write a Java program to create a class Customer with private fields customerName, address, and email. Provide setter and getter methods and a method to display customer details.  
Input:  
Set customerName = "James", address = "789 Pine Street", email = "james@email.com"  
Expected Output:  
Customer Name: James  
Address: 789 Pine Street  
Email: james@email.com  

Social Share

Bikki Singh Instructor TechnoVlogs

Bikki Singh

Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!