0% found this document useful (0 votes)
15 views27 pages

JAVA Practicle (Nizamuddin Sameer)

This document is a practical file for a BCA course at Guru Govind Singh Indraprastha University, focusing on Object-Oriented Programming using Java. It includes a list of programming tasks, such as creating Java programs for printing messages, checking leap years, demonstrating classes and interfaces, and handling exceptions. Each task is accompanied by sample code and expected outputs.

Uploaded by

mdjamaluddin9123
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)
15 views27 pages

JAVA Practicle (Nizamuddin Sameer)

This document is a practical file for a BCA course at Guru Govind Singh Indraprastha University, focusing on Object-Oriented Programming using Java. It includes a list of programming tasks, such as creating Java programs for printing messages, checking leap years, demonstrating classes and interfaces, and handling exceptions. Each task is accompanied by sample code and expected outputs.

Uploaded by

mdjamaluddin9123
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
You are on page 1/ 27

GURU GOVIND SINGH INDRAPRASTHA UNIVERSITY

INSTITUTE OF INNOVATION IN TECHNOLOGY & MANAGEMENT

OOPS USING JAVA


BCA 104P
PRACTICAL FILE

Submitted To: Submitted By:

Prof (Dr) Geetali Banerji Name: Nizamuddin Sameer


Professor Class: BCA (E1)
Enrolment No: 05124402024
INDEX
Sn Detailed Statement Date Teacher Remarks
o.

1. Write a Java program to print 'Hello' on screen and your


name in separate line.

2. Write a Program to check whether a year is Leap Year


or not.

3 Write a program to display the given pattern using


nested Loop

**

***

****

4. Write a program in java to Compute the sum of the


digits of an integer using user-defined method.

5. Write a program to find maximum and minimum


number from an array.

6. Write a program declaring a class Rectangle with data


member’s length and Breadth and member functions
Input, Output and CalcArea.

7. Write a program to demonstrate the use of static


variable, static method and static block.

8. Write a program in java to implement constructor using


parameter

9. Write a program to demonstrate use of method


overloading to calculate area of square, rectangle and
triangle.

10 Write a program to use super() to invoke base class


constructor.

11. Write a program to demonstrate the concept of abstract


class with constructor and ``final`` method

12. Write a Java program to create an interface Shape with


the getArea() method. Create three classes Rectangle,
Circle, and Triangle that implement the Shape interface.
Implement the getArea() method for each of the three
classes.

13. Write a Program to create user define package and then


import the package

14. Write a Java program to get a list of all file/directory


names in the given directory.

15. Write a Java program to read input from the Java


console.

16. Write a Java program to create a method that reads a


file and throws an exception if the file is not found.

17. Write a Java program to throws user define exception

18. Write a Java program that reads a list of integers from


the user and throws an exception if any numbers are
duplicates.

19. Write a Java program to create a basic Java thread that


prints "Hello, World!" when executed.

20. Write a Java program to create multi threading that


prints message when executed
Q1-Write a Java program to print 'Hello' on screen and your
name in separate line.
import java.util.*;

public class hello {

public static void main(String[] args){

System.out.println("HELLO KARAN");

Output:-

HELLO KARAN

Ques-2 Write a Program to check whether a year is Leap Year


or not.
Output:-
2024 is a leap year.

Ques-3 Write a program to display the given pattern using nested Loop
*
**
***
* * * *.
Output

Q-4 Write a program in java to Compute the sum of the digits of


an integer using user defined method.
Q 5 Write a program to find maximum and minimum number
from an array.
\

Q-6 Write a program declaring a class Rectangle with data


member’s length and Breadth and member functions Input,
Output and CalcArea.
Output:-
Length-5
Breadth-3
Area=15

Q7 Write a program to demonstrate the use of static variable, static


method and static block.

Output:-
Q8 Write a program in java to implement constructor using parameter.

Output:-

Q-9 Write a program to demonstrate use of method overloading to


calculate area of square, rectangle and triangle.
Output

Ques 10 Write a program to use super() to invoke base class


constructor.
Output

Q11 Write a
program to demonstrate the concept of abstract class with constructor
and ``final`` method.
Output

Q12 Write a Java program to create an interface Shape with the


getArea() method. Create three classes Rectangle, Circle, and Triangle
that implement the Shape interface. Implement the getArea() method
for each of the three classes.
ANS-
class Shape {

double getArea();

class Rectangle implements Shape {

double length, width;

Rectangle(double length, double width) {

this.length = length;

this.width = width;

public double getArea() {

return length * width;

class Circle implements Shape {

double radius;

Circle(double radius) {

this.radius = radius;

public double getArea() {

return Math.PI * radius * radius;

}
}

class Triangle implements Shape {

double base, height;

Triangle double base, double height) {

this.base = base;

this.height = height;

public double getArea() {

return 0.5 * base * height;

public class Main {

public static void main(String[] args) {

Shape rect = new Rectangle(5, 3);

Shape circle = new Circle(4);

Shape triangle = new Triangle(6, 2);

System.out.println("Rectangle Area: " + rect.getArea());

System.out.println("Circle Area: " + circle.getArea());

System.out.println("Triangle Area: " + triangle.getArea());

}
}

Output:-
Rectangle Area: 15.0

Circle Area: 50.26548245743669

Triangle Area: 6.0

Q13 Write a Program to create user define package and then


import the package.
ANS:
Create a package named mypackage
// File: mypackage/MyClass.java
package mypackage;

public class MyClass {


public void displayMessage() {
System.out.println("Hello from MyClass in mypackage!");
}

Create a main program that imports the package

// File: Main.java

import mypackage.MyClass;
public class Main {

public static void main(String[] args) {

MyClass obj = new MyClass();

obj.displayMessage();

Output:-
Hello from MyClass in mypackage!

Q14 Write a Java program to get a list of all file/directory


names in the given directory.
Output:-
Q-15 Write a Java program to read input from the Java console

Output:-
Q-16 Write a Java program to create a method that reads a file and
throws an exception if the file is not found.

Output:-
Q17 Write a Java program to throws user define exception

Output:-
Q18 Write a Java program that reads a list of integers from the
user and throws an exception if any numbers are duplicates.
ANS-
import java.util.*;

class DuplicateNumberException extends Exception {


public DuplicateNumberException(String message) {
super(message);
}
}

public class DuplicateChecker {

public static void checkForDuplicates(List<Integer> numbers)


throws DuplicateNumberException {
Set<Integer> uniqueNumbers = new HashSet<>();

for (int num : numbers) {


if (!uniqueNumbers.add(num)) {
throw new DuplicateNumberException("Duplicate
number found: " + num);
}
}

System.out.println("All numbers are unique.");


}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
List<Integer> numberList = new ArrayList<>();

System.out.print("Enter how many numbers: ");


int count = sc.nextInt();
System.out.println("Enter " + count + " numbers:");
for (int i = 0; i < count; i++) {
numberList.add(sc.nextInt());
}
try {
checkForDuplicates(numberList);
} catch (DuplicateNumberException e) {
System.out.println("Error: " + e.getMessage());
}
sc.close();
}
}

Output:-
Q19 Write a Java program to create a basic Java thread that
prints "Hello, World!" when executed

Output

Q20 Write a Java program to create multi threading that prints


message when executed.
Out
put

You might also like