OOPS LAB-ASSIGNMENT
OOPS LAB-ASSIGNMENT
1
KALYANI GOVERNMENT ENGINEERING COLLEGE
Department Of Computer Science & Engineering
INDEX
3
Introduction Programs:
1. Write a java program to find out the Area of a Circle.
CODE
import java.util.Scanner;
public class AreaOfCircle {
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();
double area = Math.PI * radius * radius;
System.out.println("The area of the circle is: " + area);
}
}
OUTPUT
Enter the radius of the circle: 5
The area of the circle is: 78.53981633974483
4
int num2 = scanner.nextInt();
while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
System.out.println("The GCD is: " + num1);
}
}
OUTPUT
Enter the first number: 54
Enter the second number: 24
The GCD is: 6
5
}
System.out.println("The sum of the digits is: " + sum);
}
}
OUTPUT
Enter a number: 821
The sum of the digits is: 11
6
System.out.println(originalNumber + " is not a palindrome.");
}
}
}
OUTPUT
Enter a number: 121
121 is a palindrome.
7
grade = 'B';
} else if (percentage >= 70) {
grade = 'C';
} else if (percentage >= 60) {
grade = 'D';
} else if (percentage >= 40) {
grade = 'E';
} else {
grade = 'F';
}
System.out.println("The grade is: " + grade);
}
}
OUTPUT
Enter the percentage: 85
The grade is: B
8
int upper = scanner.nextInt();
System.out.println("Prime numbers between "+lower+" and "+upper+":");
for (int i = lower; i <= upper; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
}
OUTPUT
Enter the lower bound: 10
Enter the upper bound: 30
Prime numbers between 10 and 30:
11 13 17 19 23 29
10
8. Write Java programs for the patterns given bellow:
(a)
1
234
56789
(b)
1
212
32123
4321234
CODE (a)
public class Pattern1 {
public static void main(String[] args) {
int number = 1;
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i * 2 - 1; j++) {
System.out.print(number + " ");
number++;
}
System.out.println();
}
}
}
11
OUTPUT
1
234
56789
CODE (b)
public class Pattern2 {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
for (int j = i; j > 1; j--) {
System.out.print(j + " ");
}
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
12
OUTPUT
1
212
32123
4321234
class Circle {
Point center;
double radius;
Circle(Point center, double radius) {
this.center = center;
this.radius = radius;
}
double area() {
13
return Math.PI * radius * radius;
}
void displayArea() {
System.out.println("The area of the circle is: " + area());
}
}
15
Fibonacci(int n) {
this.n = n;
generateSeries();
}
void generateSeries() {
int a = 0, b = 1;
System.out.print("Fibonacci Series: " + a + " " + b);
for (int i = 2; i < n; i++) {
int next = a + b;
System.out.print(" " + next);
a = b;
b = next;
}
System.out.println();
}
}
16
4. Create a class Box having fields length, breadth, height and methods
getdata() and displaydata(). Make two objects of the class access fields by the
methods.
CODE
import java.util.Scanner;
class Box {
double length, breadth, height;
void getdata() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter length: ");
length = scanner.nextDouble();
System.out.print("Enter breadth: ");
breadth = scanner.nextDouble();
System.out.print("Enter height: ");
height = scanner.nextDouble();
}
void displaydata() {
System.out.println("Length: " + length);
System.out.println("Breadth: " + breadth);
System.out.println("Height: " + height);
}
}
17
box1.getdata();
box2.getdata();
System.out.println("Box 1 dimensions:");
box1.displaydata();
System.out.println("Box 2 dimensions:");
box2.displaydata();
}
}
OUTPUT
Enter length: 10
Enter breadth: 5
Enter height: 8
Enter length: 6
Enter breadth: 3
Enter height: 4
Box 1 dimensions:
Length: 10.0
Breadth: 5.0
Height: 8.0
Box 2 dimensions:
Length: 6.0
Breadth: 3.0
Height: 4.0
18
5. Modify the above program using constructors and overloaded
constructors. Create overloaded constructors to assign default values say (-
1.00) to each data members, to assign some initial values, to make a cube
(only one parameter).
CODE
class Box {
double length, breadth, height;
Box() {
length = -1.00;
breadth = -1.00;
height = -1.00;
}
Box(double l, double b, double h) {
length = l;
breadth = b;
height = h;
}
Box(double side) {
length = breadth = height = side;
}
void displaydata() {
System.out.println("Length: " + length);
System.out.println("Breadth: " + breadth);
System.out.println("Height: " + height);
}
}
19
public class BoxDemo {
public static void main(String[] args) {
Box defaultBox = new Box();
Box cuboidBox = new Box(10, 5, 8);
Box cubeBox = new Box(5);
System.out.println("Default Box dimensions:");
defaultBox.displaydata();
System.out.println("Cuboid Box dimensions:");
cuboidBox.displaydata();
System.out.println("Cube Box dimensions:");
cubeBox.displaydata();
}
}
OUTPUT
Default Box dimensions:
Length: -1.0
Breadth: -1.0
Height: -1.0
Cuboid Box dimensions:
Length: 10.0
Breadth: 5.0
Height: 8.0
Cube Box dimensions:
Length: 5.0
Breadth: 5.0
Height: 5.0
20
6. Write a program to define a class Fraction having data members numerator
and denominator. Initialize three objects using different constructors and
display its fractional value.
CODE
class Fraction {
int numerator, denominator;
Fraction() {
numerator = 0;
denominator = 1;
}
Fraction(int num, int den) {
numerator = num;
denominator = den;
}
Fraction(int num) {
numerator = num;
denominator = 1;
}
void displayFraction() {
System.out.println("Fraction: " + numerator + "/" + denominator);
}
}
21
Fraction fraction3 = new Fraction(5);
fraction1.displayFraction();
fraction2.displayFraction();
fraction3.displayFraction();
}
}
OUTPUT
Fraction: 0/1
Fraction: 3/4
Fraction: 5/1
23
public static void main(String[] args) {
ThisDemo obj = new ThisDemo(10, 20);
obj.display();
}
}
OUTPUT
a = 10, b = 20
24
}
}
OUTPUT
Count: 3
25