Task 3 - Constructors and Constructor Overloading

Updated On: Sep. 22, 2021

Task Learning Objectives:
  1. Data encapsulation using get and set methods and how to protect from outside access.
  2. How to define one or more constructors in a Java class
  3. How to initialize fields at the time of object instantiation
  4. How to call a constructor from another constructor
  5. How to calculate a property  is calculated at runtime, think of it like derived fields.

Task 3.1
Create a Circle class such that it shall be mandatory for the user to pass the radius when its obbject is instantiated. Encapsulate radius field i.e. expose via getter and setter methods for public access to read and update. Make sure, its value is updated only when new value is greater than 0. Define calculateArea and calculatePerimeter methods with public access modifiers. Demonstrate the implemented functionality from main method of TestCircle class.


Task 3.2
Make a class Employee with attributes cnic, name and salary. Make all fields private and expose them via proper getter and setter methods. Choose appropriate data type for each property yourself. In Employee class, make following constructors:
  1. Constructor that takes no parameter. It shall print, "No-argument constructor called."
  2. Constructor that takes CNIC and name only. It shall intialize both fields using setter methods.
  3. Fully parameterized constructor, that means, that takes all parameters and initialize associated instance attributes. From this constructor, call the 2nd construction to intialize CNIC and name instance attributes.
Make a class EmployeeTest, in main method, instantiate three different Employee type objects that make use of different constructors.


Task 3.3
Junaid has decided to open new bank that would encourage savings and discourage expenses or spending; his target is to attract younger people. He has seed/surplus money to spend to gain customers. He has made following initial policies about the bank account:
  1. Create two constructor of Account class to enforce that object shall  be created by only two ways i.e. passing accountId (for existing accounts) or by passing CNIC, name and age (for new account). There should be no other way to create the Account class object.
  2. When a new account is created, if age is greater than 35, the balance shall be initialized to 0 because Junaid decided to give no money to people greater than 35. For rest, Junaid want to apply following policy for initial balance: 50,000 PKR initial balance for customers who have age between [20-25} years, 25000 PKR for age between [25-30} years and only 10,000 for age between [30-35] years. Account should not be created for age below 20.
  3. Make deposit and withdraw methods to handle deposit and withdraw operations. Standard way, no special policy to enforce.
Create an Account class with above listed attributes, constructors, deposit, withdraw methods.  Encapsulate all fields and provide access via get/set methods but do not provide write access to balance field, as the balance shall be updated via deposit and withdraw methods only. Write main method to demonstrate how it works for given input.


Task 3.4
Often oldage people forget their year of birth but remember an approximate age. Define a Person class having two constructors where first takes name and age and second takes name and date of birth (of LocalDate type). The issue is, if you stored age in an integer type variable, it would get outdated after a year, so being a good programmer, you should convert the age into date of birth to store it (i.e. first constructor case). So, declare only two instance attribute i.e. String name and LocalDate dateOfBirth and initialize dateOfBirth no matter which constructor was used to instantiate the object. Make some logic to convert age to LocalDate. Also define a public int getAge() method that shall calculate age from dateOfBrith instance variable and return it.

Visit below articles to look into how to use LocalDate and convert age to LocalDate.

Add height and weight instance variables of type double to store person height and weight in kg and cm, respectively. Encapsulate them to ensure they always carry valid data. Define public double getBMI() method in Person class that shall calculate BMI and return it, check this site for informaton regarding BMI formula and other details.

Add another method getBMIHelp(), it shall return String to help the user in getting understanding about his/her BMI value, like:
Underweight = <18.5
Normal weight = 18.5–24.9
Overweight = 25–29.9
Obesity = BMI of 30 or greater
(Ref: here)

Comments