You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+130-1Lines changed: 130 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,7 @@ This repository contains a collection of Java programming exercises, structured
5
5
## Table of Contents
6
6
-[Level 1 Tasks](#level-1-tasks)
7
7
-[Level 2 Tasks](#level-2-tasks)
8
+
-[Level 3 Tasks](#level-3-tasks)
8
9
-[Mini Projects](#mini-projects)
9
10
10
11
## Level 1 Tasks
@@ -263,7 +264,135 @@ This repository contains a collection of Java programming exercises, structured
263
264
- For the double version, calculate and return both the total cost after applying a discount (e.g., a 10% discount on the original price) and the total tax (e.g., 5% tax) on that amount.
264
265
2. In the main method, call the integer version of `calculateResult` with a sample integer (e.g., 5) and print the results.
265
266
3. Call the double version of `calculateResult` with a sample double (e.g., 200.0) and print the results.
266
-
4. Ensure that the output is clear and well-structured, providing context for each result.
267
+
4. Ensure that the output is clear and well-structured, providing context for each result.
268
+
269
+
## Level 3 Tasks
270
+
271
+
Exercise 1: Simple Encapsulation Example
272
+
Task:
273
+
Create a class Person with the following private attributes:
274
+
• name (String)
275
+
• age (int)
276
+
Write setter and getter methods to access and modify these attributes. Ensure that the age cannot be set to a negative number using validation logic in the setter method.
277
+
In the main method, create a Person object, set the name and age using the setter methods, and then print the values using the getter methods.
278
+
Expected Output:
279
+
The details of the Person object, with validation ensuring the age is non-negative.
280
+
281
+
Exercise 2: Creating and Using Classes
282
+
Task:
283
+
Create a Car class with the following attributes:
284
+
• model (String)
285
+
• color (String)
286
+
• speed (int)
287
+
In the main method, create three objects of the Car class representing different car models. Use the constructor to initialize the attributes. Print the details of each car.
288
+
Expected Output:
289
+
Details of each car (model, color, and speed).
290
+
________________________________________
291
+
Exercise 3: Implementing Setters and Getters
292
+
Task:
293
+
Modify the Car class from Exercise 2 to use private attributes and provide setter and getter methods for each. Use the setter method to validate that the speed cannot be negative. Print the car details using the getter methods.
294
+
Expected Output:
295
+
Car details printed using getter methods, with validation ensuring speed is non-negative.
296
+
________________________________________
297
+
Exercise 4: Constructor Overloading
298
+
Task:
299
+
Create a class BankAccount with the following private attributes:
300
+
• accountNumber (String)
301
+
• balance (double)
302
+
• accountHolder (String)
303
+
Implement two constructors:
304
+
• A constructor with all three attributes as parameters.
305
+
• A constructor that only takes accountNumber and sets the default balance to 0 and accountHolder to "Unknown".
306
+
Create objects using both constructors and print the account details.
307
+
Expected Output:
308
+
Account details printed for objects created with both constructors.
309
+
________________________________________
310
+
Exercise 5: Using ‘this’ Keyword
311
+
Task:
312
+
Modify the BankAccount class from Exercise 4 to include setter methods. Use the this keyword within the setters to assign values to the attributes. Add a condition to check that the balance is not negative when setting the value. Print the account details using getter methods.
313
+
Expected Output:
314
+
Account details printed with valid values using getter methods, with this keyword used in setters.
315
+
________________________________________
316
+
Exercise 6: Memory Concepts and Multiple Objects
317
+
Task:
318
+
Write a Java program that creates multiple Student objects from the Student class. Each object should have unique attributes such as name, studentID, and GPA. Demonstrate that each object occupies a unique place in memory by printing the memory address of each object.
319
+
Expected Output:
320
+
Details of each student object, along with their unique memory addresses.
321
+
________________________________________
322
+
Exercise 7: Account Management Application
323
+
Task:
324
+
Extend the BankAccount class from the previous exercises by adding methods for depositing and withdrawing funds:
325
+
• deposit(double amount)
326
+
• withdraw(double amount)
327
+
Ensure the withdraw method checks if there are sufficient funds before making a withdrawal. Print a message if the withdrawal is successful or fails due to insufficient funds.
328
+
Expected Output:
329
+
Account balance after deposits and withdrawals, with a message if funds are insufficient.
330
+
________________________________________
331
+
Exercise 8: Constructor Chaining
332
+
Task:
333
+
In the BankAccount class, implement constructor chaining. If only the accountNumber is provided, call the constructor that sets the default balance and accountHolder. Create objects using different constructors to demonstrate this feature.
334
+
Expected Output:
335
+
Account details printed, showing how constructors are called through chaining.
336
+
________________________________________
337
+
Exercise 9: VIP Customer Class
338
+
Task:
339
+
Create a VIPCustomer class with the following attributes:
340
+
• name (String)
341
+
• creditLimit (double)
342
+
• email (String)
343
+
Implement three overloaded constructors:
344
+
1. A constructor that takes all three attributes.
345
+
2. A constructor that takes two attributes (name and creditLimit) and sets a default email.
346
+
3. A constructor that takes only the name and sets default values for the other two attributes.
347
+
Create objects using each constructor and print their details.
348
+
Expected Output:
349
+
Details of VIP customers, demonstrating how different constructors initialize the objects.
350
+
________________________________________
351
+
Exercise 10: Bank Application with Constructors
352
+
Task:
353
+
Create a Bank application where the user can create a BankAccount object using the constructor. The program should automatically generate an account number (use a random number generator for this), and the user should input the account holder's name and an initial deposit. Print the account details once the account is created.
354
+
Expected Output:
355
+
New bank account details with auto-generated account number and initial deposit.
356
+
________________________________________
357
+
Exercise 11: Implementing a Simple Class for Employee
358
+
Task:
359
+
Create an Employee class with attributes like name, ID, and salary. Implement getter and setter methods for each attribute and a method increaseSalary that increases the employee's salary by a certain percentage. Create an object of the Employee class and call the increaseSalary method. Print the employee details before and after the salary increase.
360
+
Expected Output:
361
+
Employee details before and after salary increase.
362
+
________________________________________
363
+
Exercise 12: Handling Multiple Objects in a Loop
364
+
Task:
365
+
Create an array of Student objects in the main method. Use a loop to initialize the objects with different names and grades, then print their details.
366
+
Expected Output:
367
+
Details of all students, initialized and printed using a loop.
368
+
________________________________________
369
+
Exercise 13: Creating a Method to Calculate GPA
370
+
Task:
371
+
Create a method calculateGPA in the Student class that takes an array of marks (integers) as input and calculates the GPA based on a given formula. Call this method from the main method to calculate and print the GPA for a student.
372
+
Expected Output:
373
+
The GPA for a student based on their marks.
374
+
________________________________________
375
+
Exercise 14: Validating Data with Setters
376
+
Task:
377
+
Modify the BankAccount class to validate the input data for balance. Ensure that the balance cannot be negative using the setter method. Print appropriate messages if invalid data is entered.
378
+
Expected Output:
379
+
Validation messages if invalid data is provided, with valid account details printed.
380
+
________________________________________
381
+
Exercise 15: Constructor Validation
382
+
Task:
383
+
Modify the VIPCustomer class to validate the creditLimit in the constructor. If the credit limit is negative, set it to 0 and print a warning message. Create objects with different credit limits and demonstrate this validation.
384
+
Expected Output:
385
+
Credit limit validation messages, with details of the VIPCustomer objects.
386
+
________________________________________
387
+
Exercise 16: Final Bank Application with All Features
388
+
Task:
389
+
Combine all features from the previous exercises into a single Bank application. The program should:
390
+
1. Allow the user to create a BankAccount object with an auto-generated account number.
391
+
2. Use constructors to initialize the account.
392
+
3. Implement methods to deposit and withdraw funds.
393
+
4. Validate the data entered for balance and withdrawals.
394
+
Expected Output:
395
+
A fully functional bank application with user interactions, account creation, deposits, withdrawals, and validation.
0 commit comments