Task 3 - Constructors and Constructor Overloading

Updated On: Oct. 5, 2020

Task 3.1 
Create a Circle class such that it shall be mandatory for the user to pass the radius when a new object of  type Circle is instantiated. Otherwise, the object shall not be instantiated. Fully encapulsulate the radius field and expose its via getter and setter methods for public access to read and update the attribute. Make sure, its value must be greater than 0. Define calculateArea and calculatePerimeter methods with public access modifiers.


Task 3.2
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 constructors for Account class in such a way that, the object of account shall be created by passing accountId(for existing accounts) or CNIC, name and age (for new account). Otherwise the Account object shall not be created. Define the constructors in such a way that they shall enforce this policy.
  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 formula 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. User below 20 shall not be able to open the account.
  3. Make deposit and withdraw methods to handle deposit and withdraw operations.
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 on balance field, as the balance shall be updated via deposit and withdraw methods and not directly. Write main method to demonstrate how it works for given input.


Task 3.3
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.4
Some oldmen in our society don't know their date of birth but only an approximate age. Define a Person class, such that its object shall be created passing name and age and another by passing name and date of birth (as LocalDate object). The issue is, if we stored their age in an integer type instance attribute, it would get outdated after some years, so being a good programmer, you should convert the age into date of birth and store the date of birth even if age is passed in constructor. So declare only one instance attribute i.e. dateOfBirth of type LocalDate and initialize its value, no matter which constructor was used to instantiate the object. Definitely, to convert age to LocalDate, you have to do some processing.

You can use LocalDate class to store date. Below two articles are good to explore how to use LocalDate and how we can convert age to LocalDate object etc.

Also define a public int getAge() method that shall calculate age from dateOfBrith instance variable and return it.

Add height and weight instance variables of type double to store person height and weight in kg and cm, respectively. Encapsulate them properly to make them hold valid data only.  Define public double getBMI() method in Person class that shall calculate BMI and return it, check this site for informaton regarding BMI e.g. formula/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)

Task 3.4 Objective: The objective of this task it to help you understand, sometime we may need to process the user provided data to make it compatible with how we represent our object state. In such cases, we expose the easier version to make it easy for others to create objects of our class and we do the heavy lifting ourselves. In same way, we can define certain behavior to outer world that serve them some data but that is not stored in object state directly. We computer it on the fly and return as we did for getAge and getBMI methods in above example.


Comments