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

OOPS LAB-ASSIGNMENT

its a proper assignment on opps object oriented programming

Uploaded by

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

OOPS LAB-ASSIGNMENT

its a proper assignment on opps object oriented programming

Uploaded by

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

KALYANI GOVERNMENT ENGINEERING COLLEGE

Department of Computer Science & Engineering

Object Oriented Programming (PCC-CS593) Lab Assignment

Name – Bidisha Sarkar


ROLL – 10200222028
Year – 3rd Semester – 5th
Session – 2024-2025
Date – 11.08.2024

1
KALYANI GOVERNMENT ENGINEERING COLLEGE
Department Of Computer Science & Engineering

INDEX

Sl. No. Topic Page No. Signature


1 Introduction Programs 4-13
i. Write a java program to find out the 4
Area of a Circle.
ii. Write a java program to compute the 4
GCD of two numbers.
iii. Write a JAVA Program to make sum of 5
digit (e.g. 821 = 8+2+1).
iv. Write a JAVA Program to find whether 6
a number is palindrome or not.
v. Write a Java program for following 7
grading system.
• Note: Percentage>=90% : Grade A
• Percentage>=80% : Grade B
• Percentage>=70% : Grade C
• Percentage>=60% : Grade D
• Percentage>=40% : Grade E
• Percentage<40% : Grade F
vi. Write a Java program to display prime 8
numbers between a given interval.
vii. Write a Java program to check 9
whether a given number is Armstrong
Number or not.
viii. Write Java programs for the patterns 11
given bellow:
(a) (b)
1 1
234 212
56789 32123
4321234
2
2 Objects, Classes and Methods 13-25
i. Create a “circle” class & a “point” class. 13
The coordinates of the circle are given
and used within the “circle” class as
object of the “point” class. Display the
area of circle.
ii. Write a Program in JAVA to find the 14
factorial of a number with two methods
getdata() and calculatefact() by creating
objects.
iii. Write a program in java to find 15
Fibonacci series using constructors.
iv. Create a class Box having fields length, 17
breadth, height and methods getdata()
and displaydata(). Make two objects of
the class access fields by the methods.
v. Modify the above program using 19
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).
vi. Write a program to define a class 21
Fraction having data members
numerator and denominator. Initialize
three objects using different
constructors and display its fractional
value.
vii. Write a program in java to implement 22
method overloading for implementing
polymorphism.
viii. Write a program to demonstrate use 23
of 'this' keyword.
ix. Write a java code to illustrate the 24
property of static keyword.

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

2. Write a java program to compute the GCD of two numbers.


CODE
import java.util.Scanner;
public class GCD {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");

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

3. Write a JAVA Program to make sum of digit (e.g. 821 = 8+2+1).


CODE
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int sum = 0;
while (number != 0) {
sum += number % 10;
number /= 10;

5
}
System.out.println("The sum of the digits is: " + sum);
}
}
OUTPUT
Enter a number: 821
The sum of the digits is: 11

4. Write a JAVA Program to find whether a number is palindrome or not.


CODE
import java.util.Scanner;
public class PalindromeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int originalNumber = number;
int reversedNumber = 0;
while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}
if (originalNumber == reversedNumber) {
System.out.println(originalNumber + " is a palindrome.");
} else {

6
System.out.println(originalNumber + " is not a palindrome.");
}
}
}
OUTPUT
Enter a number: 121
121 is a palindrome.

5. Write a Java program for following grading system.


• Note: Percentage>=90% : Grade A
• Percentage>=80% : Grade B
• Percentage>=70% : Grade C
• Percentage>=60% : Grade D
• Percentage>=40% : Grade E
• Percentage<40% : Grade F
CODE
import java.util.Scanner;
public class GradingSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the percentage: ");
double percentage = scanner.nextDouble();
char grade;
if (percentage >= 90) {
grade = 'A';
} else if (percentage >= 80) {

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

6. Write a Java program to display prime numbers between a given interval.


CODE
import java.util.Scanner;
public class PrimeNumbersInInterval {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the lower bound: ");
int lower = scanner.nextInt();
System.out.print("Enter the upper bound: ");

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

7. Write a Java program to check whether a given number is Armstrong


Number or not.
CODE
import java.util.Scanner;
public class ArmstrongNumberCheck {
9
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int originalNumber = number;
int result = 0;
int digits = String.valueOf(number).length();
while (number != 0) {
int digit = number % 10;
result += Math.pow(digit, digits);
number /= 10;
}
if (result == originalNumber) {
System.out.println(originalNumber + " is an Armstrong number.");
} else {
System.out.println(originalNumber + " is not an Armstrong number.");
}
}
}
OUTPUT
Enter a number: 153
153 is an Armstrong number.

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

Objects, Classes and Methods:


1. Create a “circle” class & a “point” class. The coordinates of the circle are
given and used within the “circle” class as object of the “point” class. Display
the area of circle.
CODE
class Point {
double x, y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
}

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());
}
}

public class CirclePointDemo {


public static void main(String[] args) {
Point center = new Point(5, 3);
Circle circle = new Circle(center, 4);
circle.displayArea();
}
}
OUTPUT
The area of the circle is: 50.26548245743669

2. Write a Program in JAVA to find the factorial of a number with two


methods getdata() and calculatefact() by creating objects.
CODE
import java.util.Scanner;
class Factorial {
int number;
void getdata() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
number = scanner.nextInt();
14
}
int calculatefact() {
int fact = 1;
for (int i = 1; i <= number; i++) {
fact *= i;
}
return fact;
}
}

public class FactorialDemo {


public static void main(String[] args) {
Factorial obj = new Factorial();
obj.getdata();
int result = obj.calculatefact();
System.out.println("Factorial: " + result);
}
}
OUTPUT
Enter a number: 5
Factorial: 120

3. Write a program in java to find Fibonacci series using constructors.


CODE
class Fibonacci {
int n;

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();
}
}

public class FibonacciDemo {


public static void main(String[] args) {
Fibonacci fib = new Fibonacci(10);
}
}
OUTPUT
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

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);
}
}

public class BoxDemo {


public static void main(String[] args) {
Box box1 = new Box();
Box box2 = new Box();

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);
}
}

public class FractionDemo {


public static void main(String[] args) {
Fraction fraction1 = new Fraction();
Fraction fraction2 = new Fraction(3, 4);

21
Fraction fraction3 = new Fraction(5);
fraction1.displayFraction();
fraction2.displayFraction();
fraction3.displayFraction();
}
}
OUTPUT
Fraction: 0/1
Fraction: 3/4
Fraction: 5/1

7. Write a program in java to implement method overloading for


implementing polymorphism.
CODE
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}

public class MethodOverloadingDemo {


22
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Sum (int, int): " + calc.add(5, 10));
System.out.println("Sum (double, double): " + calc.add(2.5, 3.5));
System.out.println("Sum (int, int, int): " + calc.add(1, 2, 3));
}
}
OUTPUT
Sum (int, int): 15
Sum (double, double): 6.0
Sum (int, int, int): 6

8. Write a program to demonstrate use of 'this' keyword.


CODE
class ThisDemo {
int a, b;
ThisDemo(int a, int b) {
this.a = a;
this.b = b;
}
void display() {
System.out.println("a = " + this.a + ", b = " + this.b);
}
}

public class Main {

23
public static void main(String[] args) {
ThisDemo obj = new ThisDemo(10, 20);
obj.display();
}
}
OUTPUT
a = 10, b = 20

9. Write a java code to illustrate the property of static keyword.


CODE
class StaticDemo {
static int count = 0;
StaticDemo() {
count++;
}
static void displayCount() {
System.out.println("Count: " + count);
}
}

public class Main {


public static void main(String[] args) {
StaticDemo obj1 = new StaticDemo();
StaticDemo obj2 = new StaticDemo();
StaticDemo obj3 = new StaticDemo();
StaticDemo.displayCount();

24
}
}
OUTPUT
Count: 3

25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy