0% found this document useful (0 votes)
15 views

Practical 4- Programs on Class, Methods and Constructors

The document outlines five practical programming tasks in Java focusing on classes, methods, and constructors. Each task includes logic descriptions, class definitions, and methods for creating and manipulating objects, such as a Patient's details, sorting an array, calculating rectangle properties, checking number positivity, and creating a student profile. Sample code and expected outputs are provided for each task to demonstrate functionality.

Uploaded by

Madhur Miradwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Practical 4- Programs on Class, Methods and Constructors

The document outlines five practical programming tasks in Java focusing on classes, methods, and constructors. Each task includes logic descriptions, class definitions, and methods for creating and manipulating objects, such as a Patient's details, sorting an array, calculating rectangle properties, checking number positivity, and creating a student profile. Sample code and expected outputs are provided for each task to demonstrate functionality.

Uploaded by

Madhur Miradwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Journal Date of Performance: 11-02-2025

Practical 4
Programs on Class, Methods and Constructors

1. Write a program to create a Patient’s details such as Name, Age, Diagnosis, and
Phone number. Use class for the demonstration.

Logic:
• A class named `Patient` is created with attributes for name, age, diagnosis, and
phone number.
• A constructor initializes these attributes when an object is created.
• The `displayDetails` method prints the patient’s details.
• In the `main` method, a `Patient` object is created with sample data.
• The `displayDetails` method is called to print the patient’s information.
Code:

DY22SBBUOBID028 51
Java Journal Date of Performance: 11-02-2025

Output:

DY22SBBUOBID028 52
Java Journal Date of Performance: 11-02-2025

2. Write a program to demonstrate the use of class for sorting an array using selection
sort approach.

Logic:
• A class named `SelectionSort` is created to implement the selection sort algorithm.
• The `sort` method sorts an array by finding the smallest element and swapping it
with the current index.
• The `display` method prints an array with a given message.
• In the `main` method, an integer array is initialized with values.
• The `display` method is called to print the unsorted array.
• The `sort` method sorts the array using the selection sort algorithm.
• The `display` method is called again to print the sorted array.
Code:

DY22SBBUOBID028 53
Java Journal Date of Performance: 11-02-2025

Output:

DY22SBBUOBID028 54
Java Journal Date of Performance: 11-02-2025

3. Write a program to calculate the Area and Perimeter of a rectangle. Use different
methods (Area and Perimeter) for the demonstration.

Logic:
• A class named `Rectangle` is created with attributes for length and width.
• A constructor initializes these attributes when an object is created.
• The `calculateArea` method computes the area by multiplying length and width.
• The `calculatePerimeter` method computes the perimeter using the formula `2 *
(length + width)`.
• The `display` method prints the rectangle’s details, including length, width, area,
and perimeter.
• In the `main` method, a `Rectangle` object is created with given dimensions.
• The `display` method is called to print the rectangle’s details.
Code:

DY22SBBUOBID028 55
Java Journal Date of Performance: 11-02-2025

Output:

DY22SBBUOBID028 56
Java Journal Date of Performance: 11-02-2025

4. Write a program to determine where the number is a positive number or negative


number. Use if else statement and different methods (Positive and Negative) for the
demonstration.

Logic:
• A class named `NumberCheck` is created with an attribute to store a number.
• A constructor initializes the number when an object is created.
• The `checkPositive` method checks if the number is greater than zero and prints
that it is positive.
• The `checkNegative` method checks if the number is less than zero and prints that
it is negative.
• In the `main` method, two `NumberCheck` objects are created with different values.
• Both methods are called on each object to determine and display whether the
number is positive or negative.
Code:

DY22SBBUOBID028 57
Java Journal Date of Performance: 11-02-2025

Output:

DY22SBBUOBID028 58
Java Journal Date of Performance: 11-02-2025

5. Write a program to demonstrate the use of constructor for creating students’ profile
consist of First Name, Last Name, Department, Marks. Use this keyword for the
demonstration.

Logic:
• A class named `Student` is created with attributes: first name, last name,
department, and marks.
• A constructor is used to initialize these attributes when an object is created.
• The `displayProfile` method prints the student’s details.
• In the `main` method, an object of the `Student` class is created with sample data.
• The `displayProfile` method is called to show the student’s details.
Code:

DY22SBBUOBID028 59
Java Journal Date of Performance: 11-02-2025

Output:

DY22SBBUOBID028 60

You might also like