Ex.
No:01 Class and Object
Aim:
Java Program using Class and Object.
Source Code:
import [Link].*;
class Car
{
String color;
String model;
void displayInfo()
{
[Link]("Car Model: " + model);
[Link]("Car Color: " + color);
}
}
public class Main
{
public static void main(String[] args)
{
Car myCar = new Car();
[Link] = "BMW";
[Link] = "White";
[Link]();
}
}
OUTPUT:
Car Model: BMW
Car Color: White
[Link] Constructors
Aim:
Java Program using Constructors.
Source Code:
import [Link].*;
class Student
{
String name;
int age;
Student(String n, int a)
{
name = n;
age = a;
}
void displayInfo()
{
[Link]("Student Name: " + name);
[Link]("Student Age: " + age);
}
}
public class Main
{
public static void main(String[] args)
{
Student s1 = new Student("Aravind", 19);
Student s2 = new Student("Hari", 20);
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
Student Name: Aravind
Student Age: 19
Student Name: Hari
Student Age: 20
[Link] Command-Line Arguments
Aim:
Java Program using Command-Line Arguments.
Source Code:
import [Link].*;
public class CommandArgs
{
public static void main(String[] args)
{
if ([Link] == 0)
{
[Link]("No command-line arguments found.");
}
else
{
[Link]("Command-line arguments are:");
for (int i = 0; i < [Link]; i++)
{
[Link]("Argument " + (i + 1) + ": " + args[i]);
}
}
}
}
OUTPUT:
javac [Link]
java Main Hello World 2025
Command-line arguments are:
Argument 1: Hello
Argument 2: World
Argument 3: 2025
No command-line arguments found.
[Link] Random Class
Aim:
Java Program using Random Class.
Source Code:
import [Link];
public class Main
{
public static void main(String[] args)
{
Random rand = new Random();
int randomInt = [Link](100);
double randomDouble = [Link]();
[Link]("Random Integer: " + randomInt);
[Link]("Random Double: " + randomDouble);
}
}
OUTPUT:
Random Integer: 77
Random Double: 0.40065390781648713
Random Integer: 53
Random Double: 0.39312187473546867
Random Integer: 82
Random Double: 0.8573613043766334
[Link] Vectors
Aim:
Java Program using Vectors.
Source Code:
import [Link];
public class SimpleVectorExample
{
public static void main(String[] args)
{
Vector<Integer> vector = new Vector<>();
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link]("Initial Vector: " + vector);
[Link]([Link](20));
[Link]("Updated Vector after removing 20: " + vector);
int element = [Link](1);
[Link]("Element at index 1: " + element);
[Link]("Size of the vector: " + [Link]());
}
}
OUTPUT:
Initial Vector: [10, 20, 30, 40]
Updated Vector after removing 20: [10, 30, 40]
Element at index 1: 30
Size of the vector: 3
[Link] String Tokenizer Class
Aim:
Java Program using String Tokenizer Class.
Source Code:
import [Link];
public class SimpleStringTokenizerExample
{
public static void main(String[] args)
{
String sentence = "Java is a powerful programming language";
StringTokenizer tokenizer = new StringTokenizer(sentence);
[Link]("Tokens in the sentence:");
while ([Link]())
{
[Link]([Link]());
}
}
}
OUTPUT:
Tokens in the sentence:
Java
is
a
powerful
programming
language
[Link] Interface
Aim:
Java Program using Interface.
Source Code:
import [Link].*;
interface Animal
{
void sound();
void sleep();
}
class Dog implements Animal
{
public void sound()
{
[Link]("Bow Bow");
}
public void sleep()
{
[Link]("The dog is sleeping.");
}
}
class Cat implements Animal
{
public void sound()
{
[Link]("Meow");
}
public void sleep() {
[Link]("The cat is sleeping.");
}
}
public class InterfaceExample
{
public static void main(String[] args)
{
Animal dog = new Dog();
Animal cat = new Cat();
[Link]("Dog actions:");
[Link]();
[Link]();
[Link]("\nCat actions:");
[Link]();
[Link]();
}
}
OUTPUT:
Dog actions:
Bow Bow
The dog is sleeping.
Cat actions:
Meow
The cat is sleeping.
[Link] Inheritance
Aim:
Java Program using all forms of Inheritance.
Source Code:
import [Link].*;
class Vehicle
{
void move()
{
[Link]("The vehicle is moving.");
}
}
class Car extends Vehicle
{
void drive()
{
[Link]("The car is driving.");
}
}
class ElectricCar extends Car
{
void charge()
{
[Link]("The electric car is charging.");
}
}
class Bike extends Vehicle
{
void pedal()
{
[Link]("The bike is being pedaled.");
}
}
interface Flyable
{
void fly();
}
interface Driveable
{
void drive();
}
class FlyingCar extends Vehicle implements Flyable, Driveable
{
public void fly()
{
[Link]("The flying car is flying.");
}
public void drive()
{
[Link]("The flying car is driving.");
}
}
public class InheritanceExample
{
public static void main(String[] args)
{
Vehicle vehicle = new Vehicle();
[Link]();
Car car = new Car();
[Link]();
[Link]();
ElectricCar electricCar = new ElectricCar();
[Link]();
[Link]();
[Link]();
Bike bike = new Bike();
[Link]();
[Link]();
FlyingCar flyingCar = new FlyingCar();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
The vehicle is moving.
The vehicle is moving.
The car is driving.
The vehicle is moving.
The car is driving.
The electric car is charging.
The vehicle is moving.
[Link] String class
Aim:
Java Program using String class.
Source Code:
import [Link].*;
public class StringExample
{
public static void main(String[] args)
{
String greeting = new String("Hello, World!");
String name = "Sri";
String message = greeting + " My name is " + name;
[Link](message);
int length = [Link]();
[Link]("Length of greeting: " + length);
String upperCaseGreeting = [Link]();
[Link]("Uppercase: " + upperCaseGreeting);
String lowerCaseGreeting = [Link]();
[Link]("Lowercase: " + lowerCaseGreeting);
boolean containsWorld = [Link]("World");
[Link]("Contains 'World'? " + containsWorld);
int index = [Link]("World");
[Link]("Index of 'World': " + index);
String substring = [Link](7, 12);
[Link]("Substring: " + substring);
String newGreeting = [Link]("World", "Java");
[Link]("Replaced Greeting: " + newGreeting);
}
}
OUTPUT:
Hello, World! My name is Sri
Length of greeting: 13
Uppercase: HELLO, WORLD!
Lowercase: hello, world!
Contains 'World'? true
Index of 'World': 7
Substring: World
Replaced Greeting: Hello, Java!
[Link] String Buffer class
Aim:
Java Program using String Buffer class.
Source Code:
import [Link].*;
public class StringBufferExample
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Hello");
[Link](" World!");
[Link]("After append: " + sb);
[Link](6, "Beautiful ");
[Link]("After insert: " + sb);
[Link]();
[Link]("After reverse: " + sb);
[Link]();
[Link](6, 16, "Amazing");
[Link]("After replace: " + sb);
[Link](6, 13);
[Link]("After delete: " + sb);
int length = [Link]();
[Link]("Length of StringBuffer: " + length);
char charAtIndex = [Link](0);
[Link]("Character at index 0: " + charAtIndex);
}
}
OUTPUT:
After append: Hello World!
After insert: Hello Beautiful World!
After reverse: !dlroW lufituaeB olleH
After replace: Hello AmazingWorld!
After delete: Hello World!
Length of StringBuffer: 12
Character at index 0: H
[Link] Exception Handling
Aim:
Java Program using Exception Handling.
Source Code:
import [Link].*;
public class ExceptionHandlingExample
{
public static void main(String[] args)
{
int[] numbers = {1, 2, 3};
try
{
[Link]("Accessing element at index 5: " + numbers[5]);
}
catch (ArrayIndexOutOfBoundsException e)
{
[Link]("Exception caught: " + e);
}
finally
{
[Link]("Finally block executed.");
}
try
{
int result = 10 / 0;
[Link]("Result: " + result);
}
catch (ArithmeticException e)
{
[Link]("Exception caught: " + e);
}
finally
{
[Link]("Finally block executed after division by zero.");
}
}
}
OUTPUT:
Exception caught: [Link]: Index 5 out of bounds for
length 3
Finally block executed.
Exception caught: [Link]: / by zero
Finally block executed after division by zero.