0% found this document useful (0 votes)
8 views3 pages

LAB3

The document contains code for two Java programs. The first program calculates the area and perimeter of a circle using a Circle class. The second program calculates the sum and product of two complex numbers using a ComplexNumber class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

LAB3

The document contains code for two Java programs. The first program calculates the area and perimeter of a circle using a Circle class. The second program calculates the sum and product of two complex numbers using a ComplexNumber class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Vidyush Singh

2100290100185
CSESec-C

Lab 3:
Exp 1: WRITE A JAVA PROGRAM TO FIND OUT THE AREA AND PERIMETER OF CIRCLE
USING CLASS AND OBJECT

import java.util.Scanner;

class Circle {
private double radius;

// Constructor
Circle(double r) {
radius = r;
}

// Method to calculate area


double calculateArea() {
return Math.PI * radius * radius;
}

// Method to calculate perimeter


double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}

public class exmpl2 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();

// Create a Circle object


Circle circle = new Circle(radius);

// Calculate and display area and perimeter


System.out.println("Area of the circle: " + circle.calculateArea());
System.out.println("Perimeter of the circle: " + circle.calculatePerimeter());
}
}
Exp 2: WRITE A JAVA PROGRAM TO FIND OUT THE SUM AND MULTIPLICATION OF TWO
COMPLEX NUMBER USING CLASS AND OBJECT

class ComplexNumber {
double real, imaginary;

// Constructor
ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}

// Method to add two complex numbers


ComplexNumber add(ComplexNumber other) {
double realPart = this.real + other.real;
double imaginaryPart = this.imaginary + other.imaginary;
return new ComplexNumber(realPart, imaginaryPart);
}

// Method to multiply two complex numbers


ComplexNumber multiply(ComplexNumber other) {
double realPart = (this.real * other.real) - (this.imaginary * other.imaginary);
double imaginaryPart = (this.real * other.imaginary) + (this.imaginary * other.real);
return new ComplexNumber(realPart, imaginaryPart);
}

// Method to display a complex number


void display() {
System.out.println(real + " + " + imaginary + "i");
}
}

public class exmpl2 {


public static void main(String[] args) {
// Create two complex numbers
ComplexNumber c1 = new ComplexNumber(3, 4);
ComplexNumber c2 = new ComplexNumber(1, 7);

System.out.print("First complex number: ");


c1.display();
System.out.print("Second complex number: ");
c2.display();

// Add the two complex numbers


ComplexNumber sum = c1.add(c2);
System.out.print("Sum of complex numbers: ");
sum.display();

// Multiply the two complex numbers


ComplexNumber product = c1.multiply(c2);
System.out.print("Product of complex numbers: ");
product.display();
}
}

You might also like