0% found this document useful (0 votes)
4 views6 pages

Java Section-5 (Exp 1,2)

The document outlines two Java programming experiments: one for creating a Rectangle class to calculate area and perimeter, and another for swapping data members of an object. Each experiment includes aims, objectives, problem statements, algorithms, code implementations, execution discussions, and conclusions. The first experiment demonstrates object-oriented principles with encapsulation, while the second focuses on object manipulation and method calling.

Uploaded by

animapanda654
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Java Section-5 (Exp 1,2)

The document outlines two Java programming experiments: one for creating a Rectangle class to calculate area and perimeter, and another for swapping data members of an object. Each experiment includes aims, objectives, problem statements, algorithms, code implementations, execution discussions, and conclusions. The first experiment demonstrates object-oriented principles with encapsulation, while the second focuses on object manipulation and method calling.

Uploaded by

animapanda654
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SECTION-5

EXPERIMENT NO.–1
1. Aim of the Experiment:
To write a Java program to create a class Rectangle with attributes length and breadth,
and methods to calculate area and perimeter.

2. Objective:
• Understand how to define classes and objects in Java.
• Implement methods to calculate area and perimeter.
• Demonstrate encapsulation and object-oriented programming.

3. Problem Statement:
Develop a Java program that defines a Rectangle class with attributes for length and
breadth. Include methods to calculate and return the area and perimeter of the rectangle.
Create an object of the class and display the results.

4. Algorithm:
1. Start.
2. Create a class Rectangle with attributes length and breadth.
3. Define methods getArea() and getPerimeter() in the class.
4. In the main method, create an object of Rectangle.
5. Assign values to length and breadth.
6. Call the methods to calculate area and perimeter.
7. Display the results.
8. End.

5. Code Implementation:
import [Link];
class Rectangle {
double length;
double breadth;
double getArea() {
return length * breadth;
}
double getPerimeter() {
return 2 * (length + breadth);
}
}

public class RectangleDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Rectangle rect = new Rectangle();
[Link]("Enter length of the rectangle: ");
[Link] = [Link]();
[Link]("Enter breadth of the rectangle: ");
[Link] = [Link]();
[Link]("Area of rectangle: " + [Link]());
[Link]("Perimeter of rectangle: " + [Link]());
}
}

6. Execution and Output Discussion:


Input:
Enter length of the rectangle: 5
Enter breadth of the rectangle: 3
Output:
Area of rectangle: 15.0
Perimeter of rectangle: 16.0
The program calculates and displays the area and perimeter based on user input.

7. Conclusion:
This program demonstrates the use of classes, attributes, and methods in Java. It
encapsulates rectangle properties and operations in a class and applies object-oriented
principles.
EXPERIMENT NO.–2
1. Aim of the Experiment:
To write a Java program to swap the data members of an object.

2. Objective:
• Understand the concept of classes and objects in Java.
• Learn how to pass objects to methods.
• Perform swapping of object data using a separate method.

3. Problem Statement:
Develop a Java program to create a class with two data members. Define a method
that swaps the values of the data members of an object. Demonstrate swapping using
another object or method.

4. Algorithm:
1. Start.
2. Create a class with two data members.
3. Define a method to swap data members using a temporary variable.
4. In the main method, create an object and input values.
5. Call the swap method.
6. Display values before and after swapping.
7. End.

5. Code Implementation:
import [Link];
class Data {
int a, b;
void display() {
[Link]("a = " + a + ", b = " + b);
}

void swap() {
int temp = a;
a = b;
b = temp;
}
}
public class SwapDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Data obj = new Data();
[Link]("Enter value of a: ");
obj.a = [Link]();
[Link]("Enter value of b: ");
obj.b = [Link]();
[Link]("Before swapping:");
[Link]();
[Link]();
[Link]("After swapping:");
[Link]();
}
}

6. Execution and Output Discussion:


Input:
Enter value of a: 10
Enter value of b: 20
Output:
Before swapping:
a = 10, b = 20
After swapping:
a = 20, b = 10
The program successfully swaps the values of data members using a method inside the
class.

7. Conclusion:
This program demonstrates how to swap the data members of an object in Java using
class methods. It emphasizes object manipulation and method calling concepts.

You might also like