Experiment No.
– 1
Date -
Aim :- Write a program in Java that takes a user's name as input and displays
each character of the name along with its corresponding ASCII code.
Software used :- Intellij IDEA.
Theory :- In ASCII Encoding , the Characters are mapped to numerical values
in the ASCII table. For example, 'A' is 65, 'a' is 97, '0' is 48, etc. The program
reads a string input from the user and processes each character individually.
We also use Type Casting in which Characters are cast to integers to obtain their
ASCII values. And looping constructs are used to iterate through each character
in the string efficiently.
Algorithm :-
1. Start
2. Declare variables
o Declare a string variable name
o Declare an integer variable i
o Declare a character variable ch
3. Create a Scanner object to take input from the user
4. Read input
o Display the message "Enter your name = "
o Read the string and store it in variable name
5. Initialize loop counter
o Set i = 0
6. Check loop condition
o If i < length of string name, go to Step 7
o Otherwise, go to Step 11
7. Extract character
o Assign ch = [Link](i)
8. Convert character to ASCII
o Typecast character ch into integer
9. Display output
o Print the character and its ASCII value
[Link] loop counter
o Set i = i + 1
o Go back to Step 6
[Link] the Scanner object
[Link]
Source Code :-
import [Link];
public class AsciiValue {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter your name = ");
String name = [Link]();
for(int
i=0;i<[Link]();i++){
char ch = [Link](i);
[Link](ch +" " + (int)ch );
[Link]();
}
}
Output :-
Experiment No. – 2
Date -
Aim :- Write a program in java to print even numbers between 23 and 57.
Software used :- Intellij IDEA.
Theory :-This program demonstrates the use of loops and conditional statements
in Java to find even numbers within a given range. A for loop is used to iterate
through numbers from 23 to 56. For each number, the modulus operator %
checks whether the number is divisible by 2. If the remainder is zero, the number
is even and is printed. This approach avoids unnecessary storage and directly
displays the required output.
Algorithm :-
1. Start
2. Declare variable
o Declare an integer variable i
3. Display message
o Print "Even numbers between 23 and 57 ="
4. Initialize loop counter
o Set i = 23
5. Check loop condition
o If i < 57, go to Step 6
o Otherwise, go to Step 10
6. Check even condition
o If i mod 2 == 0, go to Step 7
o Otherwise, go to Step 8
7. Display number
o Print the value of i
8. Increment loop counter
o Set i = i + 1
9. Go back to Step 5
[Link]
Source Code :-
public class EvenNumber {
public static void main(String[] args) {
[Link]("Even numbers between 23 and 57 =");
for(int i=23;i<57;i++){
if(i%2==0){
[Link](i+" ");
}
}
}
}
Output :-
Experiment No. – 3
Date -
Aim :- To write a Java program to swap two values using object reference by
implementing a user-defined swap() function.
Software used :- Intellij IDEA.
Theory :- This program demonstrates swapping of two values in Java using
object reference. The values are stored as data members of a class object. When
the object is passed to the swap() function, the reference to the same object is
passed. Any changes made to the object inside the function directly affect the
original object in the calling method. This shows how Java allows modification
of object data through reference passing.
Algorithm :-
1. Start
2. Define a class
o Create a class Number containing two integer variables a and b
3. Declare variables
o Declare an object obj of class Number
o Declare an integer variable temp
4. Create a Scanner object to read input
5. Read input values
o Display the message "Enter the two numbers = "
o Read first integer and store it in obj.a
o Read second integer and store it in obj.b
6. Call swapping method
o Call the method swap(obj) and pass the object as argument
7. Swapping logic (inside swap method)
o Assign temp = n.a
o Assign n.a = n.b
oAssign n.b = temp
8. Display result
o Print "Values after swapping = " followed by obj.a and obj.b
9. Close the Scanner object
[Link]
Source Code :-
import [Link]; class
Number {
int a;
int b;
}
public class Swapping {
public static void main(String[] args) { Scanner in =
new Scanner([Link]); Number obj = new
Number();
[Link]("Enter the two numbers = "); obj.a =
[Link]();
obj.b = [Link]();
swap(obj);
[Link]("Values after swapping = " + obj.a + " " + obj.b);
[Link]();
}
static void swap(Number n) { int
temp = n.a;
n.a = n.b;
n.b = temp;
}
}
Output :-
Experiment No. – 4
Date –
Aim :- Write a program that creates 2D array with different values.
- The first element should be an array containing 32
- The second array should be containing 300 and 500
- The third element should be an array containing the values 39.4 and 60
Now declare, allocate, and initialize the array.
Software used :- Intellij IDEA.
Theory :- A two-dimensional array in Java is an array of arrays.
In this program, a jagged 2D integer array is declared, allocated, and initialized
in a single statement.
Each row can have a different number of elements.
Nested for loops are used to access and display all elements of the array.
Algorithm :-
1. Start
2. Declare and initialize a 2D array
o Declare a double type two-dimensional array arr
o Initialize it with rows of different lengths
3. Display length of the array
o Print the number of rows using [Link]
4. Display array elements
o Print the message "Elements of the array:"
5. Initialize row counter
o Set i = 0
6. Check row condition
o If i < [Link], go to Step 7
o Otherwise, go to Step 11
7. Initialize column counter
o Set j = 0
8. Check column condition
o If j < length of arr[i], go to Step 9
o Otherwise, go to Step 10
9. Display element
o Print arr[i][j]
o Increment j = j + 1
o Go back to Step 8
[Link] to next row
o Print a new line
o Increment i = i + 1
o Go back to Step 6
[Link]
Source Code :-
public class TwoDArray{
public static void main(String[] args) {
// Declare, allocate and initialize the 2D array
double[][] arr = {
{32},
{300, 500},
{39.4, 60}
};
// Display length of the 2D array
[Link]("Length of the array: " + [Link]);
// Display elements of the array
[Link]("Elements of the array:");
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < arr[i].length; j++) {
[Link](arr[i][j] + " ");
}
[Link]();
}
}
}
Output :-
Experiment No. – 5
Date –
Aim :- Write a program in Java to implement insertion and deletion in stack and
queue.
Software used :- Intellij IDEA.
Theory :-
A stack is a linear data structure that follows the LIFO (Last In First Out) principle.
Insertion in a stack is called push and deletion is called pop.
A queue is a linear data structure that follows the FIFO (First In First Out)
principle.
Insertion in a queue is called enqueue and deletion is called dequeue.
In this program, both stack and queue are implemented using arrays.
During insertion and deletion, appropriate messages are printed to show which
element is inserted or deleted.
Algorithm :-
A. Stack Operations
1. Start
2. Initialize stack
o Create an integer array stack of size 5
o Set top = -1
3. Push operation
o If top == size of stack − 1, display "Stack Overflow"
o Else
▪ Increment top by 1
▪ Store the element at stack[top]
▪ Display "Inserted element into stack"
4. Pop operation
o If top == -1, display "Stack Underflow"
o Else
▪ Retrieve element from stack[top]
▪ Decrement top by 1
▪ Display "Deleted element from stack"
B. Queue Operations
1. Initialize queue
o Create an integer array queue of size 5
o Set front = -1 and rear = -1
2. Enqueue operation
o If rear == size of queue − 1, display "Queue Overflow"
o Else
▪ If front == -1, set front = 0
▪ Increment rear by 1
▪ Store the element at queue[rear]
▪ Display "Inserted element into queue"
3. Dequeue operation
o If front == -1 or front > rear, display "Queue Underflow"
o Else
▪ Retrieve element from queue[front]
▪ Increment front by 1
▪ Display "Deleted element from queue"
4. Stop
Source Code :-
public class StackAndQueueOpt{
// Stack
static int[] stack = new int[5];
static int top = -1;
// Queue
static int[] queue = new int[5];
static int front = -1, rear = -1;
public static void main(String[] args) {
// Stack operations
push(10);
push(20);
pop();
// Queue operations
enqueue(30);
enqueue(40);
dequeue();
}
static void push(int x) {
if (top == [Link] - 1) {
[Link]("Stack Overflow");
} else {
stack[++top] = x;
[Link]("Inserted element into stack: " + x);
}
}
static void pop() {
if (top == -1) {
[Link]("Stack Underflow");
} else {
int deleted = stack[top--];
[Link]("Deleted element from stack: " + deleted);
}
}
static void enqueue(int x) {
if (rear == [Link] - 1) {
[Link]("Queue Overflow");
} else {
if (front == -1) front = 0;
queue[++rear] = x;
[Link]("Inserted element into queue: " + x);
}
}
static void dequeue() {
if (front == -1 || front > rear) {
[Link]("Queue Underflow");
} else {
int deleted = queue[front++];
[Link]("Deleted element from queue: " + deleted);
}
}
}
Output :-
Experiment No. – 6
Date –
Aim :- Write a program in Java to produce tokens from a given long string.
Software used :- Intellij IDEA.
Theory :- Tokenization is the process of breaking a long string into smaller parts
called [Link] this program, the StringTokenizer class is used to split a string
into words based on a [Link] word separated by a space is treated as one
token and displayed individually.
Algorithm :-
1. Start
2. Declare variables
o Declare a string variable str
o Declare an object st of class StringTokenizer
3. Initialize the string
o Assign the sentence.
"Java is an object oriented programming language" to str
4. Create StringTokenizer object
o Initialize st by passing string str and delimiter " " (space)
5. Check for remaining tokens
o If [Link]() is true, go to Step 6
o Otherwise, go to Step 8
6. Extract token
o Retrieve the next token using [Link]()
7. Display token
o Print the extracted token
o Go back to Step 5
8. Stop
Source Code :-
import [Link];
public class StringTokenize {
public static void main(String[] args) {
String str = "Java is an object oriented programming language";
StringTokenizer st = new StringTokenizer(str, " ");
while ([Link]()) {
[Link]([Link]());
}
Output :-
Experiment No. – 7
Date –
Aim :- Write a program that describes a class Person. It should have instance
variables to record name, age, and salary. Create a Person object. Set and display
its instance variables.
Software used :- Intellij IDEA.
Theory :- In object-oriented programming, a class is a blueprint used to create
objects. It defines the properties and behavior that an object can have.
In this program, the class Person represents a real-world entity. The variables
name, age, and salary are instance variables, which means each object of the class
has its own separate copy of these variables.
An object of the class Person is created in the main method. Values are assigned
to the instance variables using the object reference. These values are then
displayed using output statements.
Algorithm :-
1. Start
2. Define a class
o Create a class named Person
3. Declare instance variables
o Declare a string variable name
o Declare an integer variable age
o Declare a double variable salary
4. Create an object
o Create an object of class Person
5. Assign values
o Assign a value to name
o Assign a value to age
o Assign a value to salary
6. Display values
o Print the value of name
o Print the value of age
o Print the value of salary
7. Stop
Source Code :-
class Person {
String name;
int age;
double salary;
}
public class PersonDemo {
public static void main(String[] args) {
// Create Person object
Person p = new Person();
// Set instance variables
[Link] = "Ram";
[Link] = 25;
[Link] = 45000;
// Display instance variables
[Link]("Name: " + [Link]);
[Link]("Age: " + [Link]);
[Link]("Salary: " + [Link]);
}
}
Output :-
Experiment No. – 8
Date –
Aim :- Write a program that creates a class Circle with instance variables for the
center and the radius. Create a constructor of class Circle to initialize and display
the variables.
Software used :- Intellij IDEA.
Theory :- A class in Java is a blueprint used to create objects. It contains variables
and methods that describe the properties and behavior of an object.
In this program, the class Circle represents a real-world circle. The variables
centerX, centerY, and radius are instance variables, meaning each object of the
class has its own copy of these values.
A constructor is a special method that is automatically called when an object is
created. It has the same name as the class and is used to initialize instance
variables. In this program, the constructor of the Circle class initializes the center
coordinates and radius and also displays their values.
Algorithm :-
1. Start
2. Define a class
o Create a class named Circle
3. Declare instance variables
o Declare integer variables centerX and centerY to represent the center
o Declare a double variable radius
4. Create a constructor
o Accept values for center and radius as parameters
o Assign the received values to the instance variables
o Display the values of center and radius inside the constructor
5. Define main method
o Create an object of class Circle
o Pass values to the constructor while creating the object
6. Stop
Source Code :-
class Circle {
int centerX;
int centerY;
double radius;
// Constructor to initialize and display values
Circle(int x, int y, double r) {
centerX = x;
centerY = y;
radius = r;
[Link]("Center of Circle: (" + centerX + ", " + centerY + ")");
[Link]("Radius of Circle: " + radius);
}
}
public class CircleClass {
public static void main(String[] args) {
// Create Circle object
Circle c = new Circle(10, 20, 7.5);
}
}
Output :-
Experiment No. – 9
Date –
Aim :- Write a program that uses length property for displaying any number of
commandline arguments.
Software used :- Intellij IDEA.
Theory :- Command line arguments are values that are passed to a Java program
at the time of execution. These arguments are received in the main() method
through the parameter String[] args.
In Java, args is an array of strings. The length property of this array is used to
determine the number of command line arguments passed to the program.
In this program, the length property is first checked to determine whether any
arguments are passed. If no arguments are present, an appropriate message is
displayed. If arguments are present, the total number of arguments is displayed
and each argument is accessed using a loop and printed on the screen.
Algorithm :-
1. Start
2. Define a class
o Create a class named CommandlineArg
3. Define the main method
o Use main(String[] args) to receive command line arguments
4. Provide command line arguments at execution time
o Execute the program by passing arguments (Java is a object-
oriented language).
5. Check number of arguments
o If [Link] == 0, display
"No command line arguments passed."
6. Display argument count
o Print the number of command line arguments using [Link]
7. Display arguments
o Use a loop to access and display each argument from the args array
8. Stop
Source Code :-
public class CommandlineArg {
public static void main(String[] args) {
if ([Link] == 0) {
[Link]("No command line arguments passed.");
} else {
[Link]("Number of command line arguments: " + [Link]);
[Link]("Command line arguments are:");
for (int i = 0; i < [Link]; i++) {
[Link](args[i]);
}
}
}
}
Output :-