You are on page 1of 5

Group Activity Experiment

I. Objective

1. To understand the concept of parameterized constructor.


2. To learn how to use the following constructor method.

Parameterized Constructor

• A parameterized constructor accepts parameters with which you can initialize the
instance variables. Using parameterized constructor, you can initialize the class variables
dynamically at the time of instantiating the class with distinct values.

Syntax

public class Sample{


Int i;
public sample(int i){
this.i = i;
}
}

Example

public class Test {


String val;
Test(String val){
this.val = val;
}
public static void main(String args[]){
Test obj = new Test("test");
System.out.println(obj.val);
}
}

Output

test

II. Procedure/s

• Write a program in JAVA to create a class Bird also declares the different parameterized
constructor to display the name of Birds.

public class Bird


{
int age;
String name;
Bird()
{
System.out.println("this is the perrot");
}
Bird(String x)
{
name=x;
System.out.println("this is the "+name);
}
Bird(int y,String z)
{
age=y;
name=z;
System.out.println("this is the "+age+"years\t"+name);
}
public static void main(String arr[])
{
Bird a=new Bird();
Bird b=new Bird("maina");
Bird c=new Bird(20,"sparrow");
}
}
III. Data and results
IV. Conclusion

The purpose of this experiment is to understand the concept of parameterized


constructors. Thus, the Java program to implement class and objects was written,
executed and the output was verified successfully. Parameterized Constructors can
exist even without the existence of Default Constructors and its advantage can
initialize each instance of the class to different values.

V. Activity Report

Section: BET-CPET-1D (STEM) Date Performed: 0706//2022


Course Code: CPET2L Date Submitted: 07/06/2022
Course Title: Object Oriented Programming
Instructor: Marjorie Villanueva
Group No.: 4 Experiment No. : 1
Name of Group members: Signature:
Alexander, Kurt

Aguilos, Mervin

Martinez, Mark Jay

Mendoza, Aldrin Daniel

Rumbaoa, Carlos

Valenzuela, Leoniel

You might also like