Studocu is not sponsored or endorsed by any college or university
Downloaded by Shilpi (navnitpanday786@gmail.com)
Department of Computer Science & Engineering
OOP using With JAVA
LAB FILE
(BCS- 452)
B.Tech. CSE(AI/ML) 2nd Year (IV th Sem.)
Academic Session 2024-25
Student Name: PARAMJEET LAMBA
Roll no.: 2302221530074
Faculty Name: Mr. Praveen
INDEX
Ex.
Experiment Date Grade Sign
No.
Create simple java programs
using command line arguments
(a) WAP that takes input from user
1
through command line argument and
then prints whether a number is
prime or not
Use Java compiler and eclipse
platform to write and execute java
program
2 (a) Write a program in java which
creates the variable size array
(Jagged Array) and print all the
values using loop statement.
Understand OOP concepts and
basics of Java programming
(a) Write a Java program to create a
class called “Person” with private and
age attribute. Create two instances of
the “Person”class, set their attributes
using the constructor, and print their
3 name and age.
(b) Write a Java program to a create
a class Person with private instance
variables name, age and country.
Provide public getter and setter
methods to access and modify these
variables.
Create Java Program using
inheritance and polymorphism
(a) Write a program in java to
4 implement the following types of
inheritence
Single Inheritence
Multilevel Inheritence
Write a Program to demonstrate
5
interfaces and abstract classes
Write a Program to implement
6 method overloading and
overriding.
Write a Program to demonstrate
the following string handling
functions:
(a) String Length
(b) Concatenation
7 (c) Character extraction
(d) String Comparison
(e) Searching and Modifying
String
(f) String Buffer and String
Builder class implementation
Implementation error-handling
techniques using exception
8
handling and multithreading.
(a) Write a Java program to
implement user defined exception
handling for negative amount
entered.
(b) Write a program in java which
creates two threads, “Even” thread
and “Odd” thread and print the even
no using Even Thread after every
two seconds and odd no using Odd
Threads after every five second.
Create java program with the
use of java package
(a) Create a package named
“Mathematics” and add a class
9 “Matrix” with methods to add and
subtract matrices (2x2). Write a
Java program importing the
Mathematics package and use the
classes defined in it.
Construct java program using
Java I/O package
(a) Write a program in java to take
input to take input from user by
using all the following methods:
10
Command Line Arguments
DataInput Stream Class
BufferReader Class
Scanner Class
Console Class
Create industry oriented
11 application using Spring
Framework.
Test RESTful web services using
12
Spring Boot.
Test Frontend web application
13
using Spring Boot.
CRUD Operations using REST
14
API using ArrayList or Map.
CRUD Operations using H2
15 Database.
EXPERIMENT No. 1
Aim :- (a) WAP that takes input from user through command
line argument and then prints whether a number is
prime or not
Software Required :- windows, java , JDK , eclipse , etc.
CODE :-
public class PrimeCheck {
public static void main(String[] args) {
// Check if user has provided a command line argument
if (args.length == 0) {
System.out.println("Please provide a number as a
command line argument.");
return;
}
// Parse the argument to an integer
int number = Integer.parseInt(args[0]);
// Check if the number is prime
boolean isPrime = true;
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}
// Print the result
if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime
number.");
}
}
}
OUTPUT :-
EXPERIMENT No. 2
Aim :- (a) Write a program in java which creates the variable
size array (Jagged Array) and print all the values using
loop statement.
Software Required :- windows, java , JDK , eclipse , etc.
CODE :-
public class JaggedArrayExample {
public static void main(String[] args) {
// Create a jagged array with 3 rows
int[][] jaggedArray = new int[3][];
// Initialize each row with a different size
jaggedArray[0] = new int[] {10, 21};
jaggedArray[1] = new int[] {35, 49, 50};
jaggedArray[2] = new int[] {66, 72, 81, 99};
// Print the values using nested loop
System.out.println("Jagged Array Elements:");
for (int i = 0; i < jaggedArray.length; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
}
System.out.println(); // New line after each row
}
}
}
OUTPUT :-
EXPERIMENT No. 3
Aim :- (a) Write a Java program to create a class called “Person”
with private and age attribute. Create two instances of the
“Person”class, set their attributes using the constructor,
and print their name and age.
Software Required :- windows, java , JDK , eclipse , etc.
CODE :-
public class Person {
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getters
public String getName() {
return name;
}
public int getAge() {
return age;
}
public static void main(String[] args) {
// Create two instances of Person class
Person person1 = new Person("Samvar", 21);
Person person2 = new Person("Surya", 55);
// Print their name and age
System.out.println("Person 1: Name = " +
person1.getName() + ", Age = " + person1.getAge());
System.out.println("Person 2: Name = " +
person2.getName() + ", Age = " + person2.getAge());
}
}
OUTPUT :-
EXPERIMENT No. 3
Aim :- (b) Write a Java program to a create a class Person with
private instance variables name, age and country. Provide
public getter and setter methods to access and modify
these variables.
Software Required :- windows, java , JDK , eclipse , etc.
CODE :-
public class Person {
// Private instance variables
private String name;
private int age;
private String country;
// Constructor
public Person() {
// Default constructor
}
// Parameterized constructor
public Person(String name, int age, String country) {
this.name = name;
this.age = age;
this.country = country;
}
// Getter methods
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getCountry() {
return country;
}
// Setter methods
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setCountry(String country) {
this.country = country;
}
// Optional: toString method for easy printing
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ",
country=" + country + "]";
}
// Example main method to demonstrate usage
public static void main(String[] args) {
// Create a new Person object
Person person = new Person();
// Set values using setter methods
person.setName("Utkarsh");
person.setAge(30);
person.setCountry("USA");
// Get values using getter methods
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
System.out.println("Country: " + person.getCountry());
// Using toString method
System.out.println(person);
// Using parameterized constructor
Person person2 = new Person("Paramjeet", 20,
"India");
System.out.println(person2);
}
}
OUTPUT :-