Southeast University
Department of Computer Science & Engineering
Program: B.Sc. in CSE
Course Code: CSE 282.2
Course Title: Programming Language II Java Lab
Course Teacher: Mohammed Ashikur Rahman, PhD
Submitted by,
Name: Md.Abdullah Al Sajid
ID: 2023200000727
Section: .3
Lab Task No: 02
Lab Task Name: Declaration of methods and constructors, writing the program in JAVA.
Problem 01:
Write a Java program to create an Employee class with properties such as name, age,
designation, salary (object variables), and company name, company address (class
variable). Implement a parameterized constructor to initialize 3 objects. Include an
object method to display the employee details and a class method to display the total
number of employees.
Problem 02:
Write a Java program to create a Book class with properties such as title, author, year
(object variables), and genre (class variable). Implement a parameterized constructor to
initialize 3 objects. Include an object method to display the book's details and a class
method to display the total number of books.
Problem 03:
Write a Java program to create a Student class with properties such as id, name,
department, cgpa (object variables), and university name (class variable). Implement a
parameterized constructor to initialize 3 objects. Include an object method to display the
student’s details and a class method to display the total number of students.
Background Theory:
● Constructors are special methods used to initialize object properties when an
object is created. It has the same name as the class name, and it returns nothing. It can
have parameters.
● Object methods perform actions specific to each object, whereas class methods are
associated with the class and can be called without creating an object.
● Class methods are accessed using the class name. They are declared with the ‘static’
keyword.
● Object methods are accessed using the object instance.
● Class variables are shared among all instances of a class. They are declared with the
‘static’ keyword.
● Object variables have unique values for each instance.
● The main method serves as the entry point of the program
.
Problem Analysis For Employee:
The program aims to create an Employee class and demonstrate the usage of class
variables, object variables, constructors, class methods, and object methods. It sets the
properties of employees using a parameterized constructor and displays the employee
details using an object method. Additionally, it calculates and displays the total number
of employees using a class method.
Algorithm Design For Employee Class:
1.Declare Class: Employee
2.Declare object variables.Such as:{
name(String),age(int),designation(String),salary(int) }
3.Declare class variables.Such as(companyname,companyaddress,employecount).
4.Declare Constructor: Employee(String name, int age, String designation, int salary).
5.Declare an object method.Such as-employeedetails.
6.Declare an object method.Such as-totalemployee.
7. The main function, create employee objects (name: obj ) using the constructor and
set their properties.
8. Use object methods to display all employee details.
9. Use the class method to display the total number of employees.
Code-
Output-
Problem Analysis For Book :
The program aims to create a Book class and demonstrate the use of object variables,
class variables, constructors, object methods, and class methods. It sets the properties
of books such as title, author, and year using a parameterized constructor, and displays
the book details using an object method. The program also defines a class variable for
the genre, which is common to all books. Additionally, it calculates and displays the total
number of book objects using a class method.
Algorithm Design For Book Class:
1. Create a Book class.
2. Declare object variables. such as- title, author, and year.
3. Declare class variables, such as- genre and bookscount.
4. Implement a parameterized constructor to set the book properties and increment the
total number of books.
5. Define an object method, such as bookDetails(), to display the book detail properties.
6. Implement a class method, such as totalbook(), to return the total number of books.
7. The main function, create book objects (name: obj ) using the constructor and set
their properties.
8. Use object methods to display all book details.
9. Use the class method to display the total number of books.
Code-
Output-
Problem Analysis for Student:
The program aims to create a Student class and demonstrate the use of object
variables, class variables, constructors, object methods, and class methods. It sets the
properties of students such as id, name, department, and CGPA using a parameterized
constructor, and displays the student details using an object method. The class also
defines a class variable for the university, which is common to all student objects.
Additionally, the program calculates and displays the total number of students created
using a class method.
Algorithm Design for Student:
1.Create a Student class.
2.Declare class variables, such as university and studentscount, to store the
university name and keep track of the number of students.
3.Declare object variables, such as name, id, department, and cgpa, to store the
student properties.
4.Implement a parameterized constructor to set the student properties and increment
the studentscount.
5.Define an object method, such as studentdetails(), to display the student
properties.
6.Implement a class method, such as totalstudent(), to return the total number of
student objects created.
7.In the main function, create student objects using the constructor and set their
properties.
8.Use the object method to display each student's details.
9.Use the class variable or method to display the total number of students.
Code-
Output-