Docsity

Object-Oriented Programming Lab: Constructors in Java, Study notes of Object Oriented Programming

Partial preview of the text

Download Object-Oriented Programming Lab: Constructors in Java and more Study notes Object Oriented Programming in PDF only on Docsity!

Department of Computing CS 212: Object Oriented Programming Lab 05: Constructors Class: DS- Lab Engineer: Bareera Anam

Objective: To understand and implement constructors in Java, including default, parameterised, and copy constructors. Software/Tool: Visual Studio, Netbeans Introduction A constructor in Java is a special method that initialises an object when it is created. It has the same name as the class and does not have a return type. Types of Constructors in Java I. Default Constructor A default constructor is a constructor that takes no parameters. If no constructor is explicitly defined in a class, Java automatically provides a default constructor. Syntax:

Key Points About Constructors โ— Same Name as Class : A constructor must have the same name as the class. โ— No Return Type : Unlike methods, constructors do not return any value. โ— Overloading : Multiple constructors with different parameters can exist in the same class. โ— Automatic Invocation : Called when an object is created using new. โ— Copy Constructor : Not built-in; must be explicitly defined.

Lab Exercises Task 1: Implement a "BankAccount" Class with Different Constructors Design a Java class named BankAccount that simulates a bank account. The class should have instance variables: accountNumber, accountHolderName, and balance. It must include three constructors: a default constructor that initializes the account with a balance of 0 , a parameterized constructor to initialize accountNumber, accountHolderName, and balance, and a copy constructor that creates a duplicate account with the same details as an existing account. Additionally, implement a method displayAccountDetails() to print account information. The expected outcome is that users should be able to create an account using different constructors, and the copy constructor should correctly duplicate account details. Task 2: Design Vehicle Management System by using Constructor Overloading Create a Vehicle class that stores vehicle details. The class should contain instance variables: brand, model, year, price, and fuelType. It should include three constructors: a default constructor that sets brand to "Unknown", model to "N/A", and year to 2020 , a parameterized constructor to initialize all attributes, and an overloaded constructor with three parameters (brand, model, and year) while assigning fuelType as "Petrol" and price as 5000. Implement a method showVehicleInfo() to display vehicle details. The expected outcome is that different objects should be initialized using different constructors, and constructor overloading should work efficiently. Task 3: Design Employee Management System by using Copy Constructor & Object Cloning

replicate student details, and the calculateAverage() method should return the correct average marks.