100 Java OOP questions
: 100Java OOP code questions for you
1. Create a class named "Person" with attributes name and age.
2. Create a constructor for the "Person" class that accepts name and age as parameters.
3. Create a method in the "Person" class named "printDetails" that prints the name and age of the person.
4. Create an object of the "Person" class and call the "printDetails" method.
5. Create a class named "Circle" with attribute radius.
6. Create a constructor for the "Circle" class that accepts the radius as a parameter.
7. Create a method in the "Circle" class named "calculateArea" that calculates and returns the area of the circle.
8. Create an object of the "Circle" class and call the "calculateArea" method.
9. Create a class named "Rectangle" with attributes length and width.
10. Create a constructor for the "Rectangle" class that accepts length and width as parameters.
11. Create a method in the "Rectangle" class named "calculateArea" that calculates and returns the area of the rectangle.
12. Create an object of the "Rectangle" class and call the "calculateArea" method.
13. Create a class named "BankAccount" with attributes accountNumber and balance.
14. Create a constructor for the "BankAccount" class that accepts accountNumber and initialBalance as parameters.
15. Create methods in the "BankAccount" class named "deposit" and "withdraw" that update the balance accordingly.
16. Create an object of the "BankAccount" class and test the "deposit" and "withdraw" methods.
17. Create a class named "Student" with attributes name, age, and grade.
18. Create a constructor for the "Student" class that accepts name, age, and grade as parameters.
19. Create a method in the "Student" class named "printDetails" that prints the name, age, and grade of the student.
20. Create an object of the "Student" class and call the "printDetails" method.
21. Create a class named "Car" with attributes make, model, and year.
22. Create a constructor for the "Car" class that accepts make, model, and year as parameters.
23. Create a method in the "Car" class named "startEngine" that prints "Engine started!".
24. Create an object of the "Car" class and call the "startEngine" method.
25. Create a class named "Book" with attributes title, author, and year.
26. Create a constructor for the "Book" class that accepts title, author, and year as parameters.
27. Create a method in the "Book" class named "printDetails" that prints the title, author, and year of the book.
28. Create an object of the "Book" class and call the "printDetails" method.
29. Create a class named "Employee" with attributes name, age, and salary.
30. Create a constructor for the "Employee" class that accepts name, age, and salary as parameters.
31. Create a method in the "Employee" class named "printDetails" that prints the name, age, and salary of the employee.
32. Create an object of the "Employee" class and call the "printDetails" method.
33. Create a class named "Triangle" with attributes base and height.
34. Create a constructor for the "Triangle" class that accepts base and height as parameters.
35. Create a method in the "Triangle" class named "calculateArea" that calculates and returns the area of the triangle.
36. Create an object of the "Triangle" class and call the "calculateArea" method.
37. Create a class named "Dog" with attributes name and breed.
38. Create a constructor for the "Dog" class that accepts name and breed as parameters.
39. Create a method in the "Dog" class named "bark" that prints "Woof!".
40. Create an object of the "Dog" class and call the "bark" method.
41. Create a class named "Cat" with attributes name and color.
42. Create a constructor for the "Cat" class that accepts name and color as parameters.
43. Create a method in the "Cat" class named "meow" that prints "Meow!".
44. Create an object of the "Cat" class and call the "meow" method.
45. Create a class named "Bank" with an attribute "accounts" that is an array of "BankAccount" objects.
46. Create a method in the "Bank" class named "addAccount" that adds a new "BankAccount" to the "accounts" array.
47. Create a method in the "Bank" class named "getTotalBalance" that returns the total balance of all "BankAccount" objects.
48. Create an object of the "Bank" class and testthe "addAccount" and "getTotalBalance" methods.
49. Create a class named "Shape" with an abstract method named "calculateArea".
50. Create a class named "Circle" that extends the "Shape" class and implements the "calculateArea" method for calculating the area of a circle.
51. Create a class named "Rectangle" that extends the "Shape" class and implements the "calculateArea" method for calculating the area of a rectangle.
52. Create objects of the "Circle" and "Rectangle" classes and call the "calculateArea" method on each object.
53. Create an interface named "Playable" with a method named "play".
54. Create a class named "Guitar" that implements the "Playable" interface and defines the "play" method to print "Playing the guitar".
55. Create a class named "Piano" that implements the "Playable" interface and defines the "play" method to print "Playing the piano".
56. Create objects of the "Guitar" and "Piano" classes and call the "play" method on each object.
57. Create a class named "Animal" with an abstract method named "makeSound".
58. Create a class named "Dog" that extends the "Animal" class and implements the "makeSound" method to print "Woof!".
59. Create a class named "Cat" that extends the "Animal" class and implements the "makeSound" method to print "Meow!".
60. Create objects of the "Dog" and "Cat" classes and call the "makeSound" method on each object.
61. Create a class named "Shape" with a protected attribute named "color" and a method named "getColor" that returns the color.
62. Create a class named "Circle" that extends the "Shape" class and defines a constructor that accepts the color as a parameter and sets it using the "setColor" method inherited from the "Shape" class.
63. Create an object of the "Circle" class and call the "getColor" method to get the color.
64. Create a class named "Vehicle" with a private attribute named "speed" and public methods named "getSpeed" and "setSpeed" to get and set the speed.
65. Create a class named "Car" that extends the "Vehicle" class and defines a constructor that accepts the speed as a parameter and sets it using the "setSpeed" method inherited from the "Vehicle" class.
66. Create an object of the "Car" class and call the "getSpeed" method to get the speed.
67. Create a class named "Employee" with a private attribute named "salary" and public methods named "getSalary" and "setSalary" to get and set the salary.
68. Create a class named "Manager" that extends the "Employee" class and defines a constructor that accepts the salary as a parameter and sets it using the "setSalary" method inherited from the "Employee" class.
69. Create an object of the "Manager" class and call the "getSalary" method to get the salary.
70. Create a class named "Bank" with a private attribute named "balance" and public methods named "getBalance" and "deposit" to get the balance and deposit money, respectively.
71. Create a class named "SavingsAccount" that extends the "Bank" class and defines a constructor that accepts the initial balance as a parameter and sets it using the "deposit" method inherited from the "Bank" class.
72. Create an object of the "SavingsAccount" class and call the "getBalance" method to get the balance.
73. Create a class named "MathUtils" with a static method named "add" that accepts two integers as parameters and returns their sum.
74. Call the "add" method of the "MathUtils" class with two integers and print the result.
75. Create a class named "StringUtils" with a static method named "reverse" that accepts a string as a parameter and returns the reverse of the string.
76. Call the "reverse" method of the "StringUtils" class with a string and print the result.
77. Create a class named "DateUtils" with a static method named "isLeapYear" that accepts a year as a parameter and returns true if it's a leap year, false otherwise.
78. Call the "isLeapYear" method of the "DateUtils" class with a year and print the result.
79. Create a class named "FileUtils" with a static method named "readFile" that accepts a file name as a parameter and reads the contents of the file, returning them as a string.
80. Call the "readFile" method of the "FileUtils" class with a file name and print the result.
81. Create a class named "ArrayUtils" with a static method named "findMax" that accepts an array of integers as a parameter and returns the maximum value in the array.
82. Call the "findMax" method of the "ArrayUtils" class with an array of integers and print the result.
83. Create a class named "LinkedList" with a private attribute named "head" and public methods named "insert" and "print" to insert elements into the linked list and print its contents, respectively.
84. Create an object of the "LinkedList" class, insert elements, and call the "print" method to print the contents.
85. Create a class named "Stack" with a private attribute named "elements" (an array) and public methods named "push" and "pop" to add and remove elements from the stack, respectively.
86. Create an object of the "Stack" class, push elements, and call the "pop" method to remove and print the elements.
87. Create a class named "Queue" with a private attribute named "elements" (an array) and public methods named "enqueue" and "dequeue" to add and remove elements from the queue, respectively.
88. Create an object of the "Queue" class, enqueue elements, and call the "dequeue" method to remove and print the elements.
89. Create a class named "BinarySearchTree" with a private attribute named "root" and public methods named "insert" and "search" to insert elements into the binary search tree and search for a given element, respectively.
90. Create an object of the "BinarySearchTree" class, insert elements, and call the "search" method to search for an element.
91. Create a class named "HashTable" with a private attribute named "table" (an array) and public methods named "insert" and "search" to insert elements into the hash table and search for a given element, respectively.
92. Create an object of the "HashTable" class, insert elements, and call the "search" method to search for an element.
93. Create a class named "Graph" with a private attribute named "adjacencyMatrix" (a 2D array) and public methods named "addEdge" and "removeEdge" to add and remove edges in the graph, respectively.
94. Create an object of the "Graph" class, add edges, and call the "removeEdge" method to remove an edge.
95. Create a class named "SortingUtils" with static methods named "bubbleSort," "selectionSort," and "insertionSort" that accept an array of integers and sort it using the respective sorting algorithms.
96. Call the sorting methods of the "SortingUtils" class with an array of integers and print the sorted array.
97. Create a class named "FileUtils" with a static method named "writeFile" that accepts a file name and content as parameters and writes the content to the file.
98. Call the "writeFile" method of the "FileUtils" class with a file name and content to write to the file.
99. Create a class named "EncryptionUtils" with static methods named "encrypt" and "decrypt" that accept a string and perform encryption and decryption operations, respectively.
100. Call the encryption and decryption methods of the "EncryptionUtils" class with a string and print the results.
These questions cover a wide range of Java OOP concepts and should provide you with a good starting point for practicing your Java programming skills.
genereted by مصطفى ضياء
