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 argument is passed
if (args.length == 0) {
System.out.println("Please provide a number as a command-line
argument.");
return;
// Convert the argument to an integer
int num = Integer.parseInt(args[0]);
// Handle edge cases
if (num <= 1) {
System.out.println(num + " is not a prime number.");
return;
}
boolean isPrime = true;
// Check for factors
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
// Output result
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " 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) {
// Creating a jagged array (array with variable row sizes)
int[][] jaggedArray = new int[3][];
// Initialize each row with a different size
jaggedArray[0] = new int[] {1, 2};
jaggedArray[1] = new int[] {3, 4, 5};
jaggedArray[2] = new int[] {6};
// Print all 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++) {
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 : class Person {
// Private attributes
private String name;
private int age;
// Constructor to initialize name and age
public Person(String name, int age) {
this.name = name;
this.age = age;
// Method to print person details
public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
public class PersonTest {
public static void main(String[] args) {
// Creating two Person objects
Person person1 = new Person("Alice", 25);
Person person2 = new Person("Bob", 30);
// Displaying their details
person1.displayInfo();
person2.displayInfo();
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 : class Person {
// Private instance variables
private String name;
private int age;
private String country;
// Getter for name
public String getName() {
return name;
// Setter for name
public void setName(String name) {
this.name = name;
// Getter for age
public int getAge() {
return age;
// Setter for age
public void setAge(int age) {
this.age = age;
// Getter for country
public String getCountry() {
return country;
// Setter for country
public void setCountry(String country) {
this.country = country;
public class PersonDemo {
public static void main(String[] args) {
// Create a Person object
Person p = new Person();
// Set values using setters
p.setName("vaibhaw srivastav");
p.setAge(20);
p.setCountry("India");
// Get and print values using getters
System.out.println("Name: " + p.getName());
System.out.println("Age: " + p.getAge());
System.out.println("Country: " + p.getCountry());
OUTPUT :
EXPERIMENT No. 4
Aim :-(a) Write a program in java to implement the following types of inheritence
Single Inheritence Multilevel Inheritence
Software Required :- windows, java , JDK , eclipse , etc.
CODE : class Animal {
void eat() {
System.out.println("Animal eats food.");
// Single Inheritance: Dog inherits from Animal
class Dog extends Animal {
void bark() {
System.out.println("Dog barks.");
// Multilevel Inheritance: Puppy inherits from Dog (which inherits from Animal)
class Puppy extends Dog {
void weep() {
System.out.println("Puppy weeps.");
}
}
// Main class to test
public class InheritanceDemo {
public static void main(String[] args) {
// Single Inheritance Example
Dog d = new Dog();
System.out.println("Single Inheritance:");
d.eat(); // inherited from Animal
d.bark(); // own method
// Multilevel Inheritance Example
Puppy p = new Puppy();
System.out.println("\nMultilevel Inheritance:");
p.eat(); // from Animal
p.bark(); // from Dog
p.weep(); // from Puppy
}
OUTPUT :
EXPERIMENT No. 5
Aim :- Write a Program to demonstrate interfaces and abstract classes.
Software Required :- windows, java , JDK , eclipse , etc.
CODE : interface Drawable {
void draw(); // abstract method
// Abstract class
abstract class Shape {
String color;
// Constructor
Shape(String color) {
this.color = color;
// Abstract method
abstract double area();
// Concrete method
void displayColor() {
System.out.println("Color: " + color);
}
// Concrete class: extends abstract class and implements interface
class Circle extends Shape implements Drawable {
double radius;
// Constructor
Circle(String color, double radius) {
super(color); // Call to abstract class constructor
this.radius = radius;
// Implement abstract method from Shape
@Override
double area() {
return Math.PI * radius * radius;
// Implement method from interface
@Override
public void draw() {
System.out.println("Drawing a Circle");
// Main class to test
public class InterfaceAbstractDemo {
public static void main(String[] args) {
Circle c = new Circle("Red", 5.0);
c.draw(); // From interface
c.displayColor(); // From abstract class
System.out.println("Area: " + c.area()); // Abstract method implemented in
Circle
OUTPUT :
EXPERIMENT No. 6
Aim :- Write a Program to implement method overloading and overriding.
Software Required :- windows, java , JDK , eclipse , etc.
CODE : class Calculator {
// Method Overloading: add with different parameter types
int add(int a, int b) {
return a + b;
double add(double a, double b) {
return a + b;
// Method to be overridden
void display() {
System.out.println("Calculator: performing calculations.");
// Subclass
class ScientificCalculator extends Calculator {
// Overriding the display method
@Override
void display() {
System.out.println("Scientific Calculator: advanced calculations.");
// Main class
public class OverloadOverrideDemo {
public static void main(String[] args) {
// Method Overloading
Calculator calc = new Calculator();
System.out.println("Integer Add: " + calc.add(5, 10));
System.out.println("Double Add: " + calc.add(3.5, 7.2));
// Method Overriding
Calculator sciCalc = new ScientificCalculator();
sciCalc.display(); // Calls overridden method from ScientificCalculator
OUTPUT :
EXPERIMENT No. 7
Aim :- Write a Program to demonstrate the following string handling functions: (a)
String Length (b) Concatenation (c) Character extraction (d) String Comparison (e)
Searching and Modifying String (f) String Buffer and String Builder class
implementation
Software Required :- windows, java , JDK , eclipse , etc.
CODE : public class StringHandlingDemo {
public static void main(String[] args) {
// (a) String Length
String str1 = "Hello World";
System.out.println("(a) Length of '" + str1 + "': " + str1.length());
// (b) Concatenation
String str2 = "Java";
String result = str1 + " from " + str2;
System.out.println("(b) Concatenation: " + result);
// (c) Character Extraction
char ch = str1.charAt(6);
System.out.println("(c) Character at index 6: " + ch);
// (d) String Comparison
String s1 = "OpenAI";
String s2 = "openai";
System.out.println("(d) s1 equals s2? " + s1.equals(s2));
System.out.println("(d) s1 equalsIgnoreCase s2? " + s1.equalsIgnoreCase(s2));
// (e) Searching and Modifying String
String sentence = "Java programming is fun";
System.out.println("(e) Contains 'fun'? " + sentence.contains("fun"));
System.out.println("(e) Replace 'fun' with 'awesome': " +
sentence.replace("fun", "awesome"));
System.out.println("(e) Index of 'is': " + sentence.indexOf("is"));
System.out.println("(e) Substring (5 to 16): " + sentence.substring(5, 16));
// (f) StringBuffer and StringBuilder
// StringBuffer - thread-safe
StringBuffer sb1 = new StringBuffer("Hello");
sb1.append(" World");
sb1.insert(6, "Java ");
sb1.replace(0, 5, "Hi");
System.out.println("(f) StringBuffer result: " + sb1);
// StringBuilder - faster, not thread-safe
StringBuilder sb2 = new StringBuilder("Welcome");
sb2.append(" to Java");
sb2.reverse();
System.out.println("(f) StringBuilder result (reversed): " + sb2);
OUTPUT :
EXPERIMENT No. 8
Aim :- (a) Write a Java program to implement user defined exception handling for
negative amount entered
Software Required :- windows, java , JDK , eclipse , etc.
CODE : import java.util.Scanner;
class NegativeAmountException extends Exception {
public NegativeAmountException(String message) {
super(message);
class Bank {
public void deposit(double amount) throws NegativeAmountException {
if (amount < 0) {
throw new NegativeAmountException("Amount cannot be negative!");
} else {
System.out.println("Deposited: Rs. " + amount);
public class NegativeAmountDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Bank bank = new Bank();
try {
System.out.print("Enter amount to deposit (in Rs.): ");
double amount = scanner.nextDouble();
bank.deposit(amount);
} catch (NegativeAmountException e) {
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("Invalid input!");
} finally {
scanner.close();
OUTPUT :