Program-1
Aim: Write a program to print "Hello world" and take and pass an argument to
write Hello and name of the user.
Source code :
class HelloUser {
public static void main(String[] args) {
[Link]("Hello World");
[Link]("Hello " + args[0]);
}
}
Output :
PS C:\Users\krish\Desktop\redis> javac [Link]
PS C:\Users\krish\Desktop\redis> java HelloUser Dharaya
Hello World
Hello Dharaya
Program-2
Aim : Write a program to find the greatest of 3 numbers, take arguments as
input.
Source Code :
class GreatestOfThree {
public static void main(String[] args) {
int a = [Link](args[0]);
int b = [Link](args[1]);
int c = [Link](args[2]);
int greatest;
if (a >= b && a >= c) {
greatest = a;
} else if (b >= a && b >= c) {
greatest = b;
} else {
greatest = c;
}
[Link]("Greatest number is: " + greatest);
}
}
Output :
PS C:\Users\krish\Desktop\redis> javac [Link]
PS C:\Users\krish\Desktop\redis> java GreatestOfThree 12 2 23
Greatest number is: 23
Program-3
Aim : Write a program to find the factorial of the number taken as argument.
Source Code :
class Factorial {
public static void main(String[] args) {
int n = [Link](args[0]);
long fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
[Link]("Factorial of " + n + " is: " + fact);
}
}
Output :
PS C:\Users\krish\Desktop\redis> javac [Link]
PS C:\Users\krish\Desktop\redis> java Factorial 8
Factorial of 8 is: 40320
Program-4
Aim : Write a program to find the LCM of 2 numbers taken as arguments.
Source Code :
class LCM {
public static void main(String[] args) {
int a = [Link](args[0]);
int b = [Link](args[1]);
int x = a, y = b;
while (y != 0) {
int temp = y;
y = x % y;
x = temp;
}
int gcd = x;
int lcm = (a * b) / gcd;
[Link]("LCM of " + a + " and " + b + " is: " + lcm);
}
}
Output :
PS C:\Users\krish\Desktop\redis> javac [Link]
PS C:\Users\krish\Desktop\redis> java LCM 5 12
LCM of 5 and 12 is: 60
Program-5
Aim : Write a program to implement stack without any pre define class
Source Code :
class Stack {
private int[] arr;
private int top;
private int capacity;
Stack(int size) {
capacity = size;
arr = new int[capacity];
top = -1;
}
void push(int value) {
if (top == capacity - 1) {
[Link]("Stack Overflow! Cannot push " + value);
return;
}
arr[++top] = value;
[Link](value + " pushed into stack");
}
int pop() {
if (top == -1) {
[Link]("Stack Underflow! Stack is empty");
return -1;
}
return arr[top--];
}
int peek() {
if (top == -1) {
[Link]("Stack is empty");
return -1;
}
return arr[top];
}
}
class StackDemo {
public static void main(String[] args) {
Stack stack = new Stack(5);
[Link](10);
[Link](20);
[Link](30);
[Link]("Popped: " + [Link]());
[Link]("Top element: " + [Link]());
}
}
Output :
PS C:\Users\krish\Desktop\redis> javac [Link]
PS C:\Users\krish\Desktop\redis> java StackDemo
10 pushed into stack
20 pushed into stack
30 pushed into stack
Popped: 30
Top element: 20
Program-6
Aim : Write a program to implement queue without using any pre define class
Source Code :
class Queue {
private int[] arr;
private int front;
private int rear;
private int capacity;
Queue(int size) {
capacity = size;
arr = new int[capacity];
front = 0;
rear = -1;
}
void enqueue(int value) {
if (rear == capacity - 1) {
[Link]("Queue Overflow! Cannot insert " + value);
return;
}
arr[++rear] = value;
[Link](value + " inserted into queue");
}
int dequeue() {
if (rear < front) {
[Link]("Queue Underflow! Queue is empty");
return -1;
}
return arr[front++];
}
int peek() {
if (rear < front) {
[Link]("Queue is empty");
return -1;
}
return arr[front];
}
}
class QueueDemo {
public static void main(String[] args) {
Queue queue = new Queue(5);
[Link](10);
[Link](20);
[Link](30);
[Link]("Removed: " + [Link]());
[Link]("Front element: " + [Link]());
}
}
Output :
PS C:\Users\krish\Desktop\redis> javac [Link]
PS C:\Users\krish\Desktop\redis> java QueueDemo
10 inserted into queue
20 inserted into queue
30 inserted into queue
Removed: 10
Front element: 20
Program-7
Aim : Write a program to check given string is palindrome using stack class
Source Code :
import [Link];
class PalindromeCheck {
public static void main(String[] args) {
String str = args[0];
Stack<Character> stack = new Stack<>();
for (int i = 0; i < [Link](); i++) {
[Link]([Link](i));
}
boolean isPalindrome = true;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) != [Link]()) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
[Link](str + " is a Palindrome.");
} else {
[Link](str + " is NOT a Palindrome.");
}
}
}
Output :
PS C:\Users\krish\Desktop\redis> javac [Link]
PS C:\Users\krish\Desktop\redis> java PalindromeCheck 1221
1221 is a Palindrome.
Program-8
Aim : Write a program to reverse first k elements of the queue using pre define
classes
Source Code :
import [Link];
import [Link];
import [Link];
class ReverseFirstKQueue {
public static void reverseFirstK(Queue<Integer> queue, int k) {
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < k; i++) {
[Link]([Link]());
}
while (![Link]()) {
[Link]([Link]());
}
int size = [Link]();
for (int i = 0; i < size - k; i++) {
[Link]([Link]());
}
}
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
int k = 3;
[Link]("Original Queue: " + queue);
reverseFirstK(queue, k);
[Link]("Queue after reversing first " + k + " elements: " + queue);
}
}
Output :
PS C:\Users\krish\Desktop\redis> javac [Link]
PS C:\Users\krish\Desktop\redis> java ReverseFirstKQueue
Original Queue: [10, 20, 30, 40, 50]
Queue after reversing first 3 elements: [30, 20, 10, 40, 50]
Program-9
Aim : Write a program to produce tokens from a given string
Source Code :
import [Link];
class TokenizerDemo {
public static void main(String[] args) {
String str = "Java is fun to learn";
StringTokenizer tokenizer = new StringTokenizer(str);
[Link]("Tokens from the string:");
while ([Link]()) {
[Link]([Link]());
}
}
}
Output :
Program-10
Aim : Write a program to check whether 2 strings are anagram or not
Source Code :
import [Link];
class AnagramCheck {
public static void main(String[] args) {
String str1 = args[0];
String str2 = args[1];
// Remove spaces and convert to lowercase
str1 = [Link]("\\s", "").toLowerCase();
str2 = [Link]("\\s", "").toLowerCase();
// Convert strings to char arrays
char[] arr1 = [Link]();
char[] arr2 = [Link]();
// Sort both arrays
[Link](arr1);
[Link](arr2);
// Compare sorted arrays
if ([Link](arr1, arr2)) {
[Link]("Strings \"" + args[0] + "\" and \"" + args[1] + "\" are Anagrams.");
} else {
[Link]("Strings \"" + args[0] + "\" and \"" + args[1] + "\" are NOT Anagrams.");
}
}
}
Output :
Program-11
Aim : Write a program to find percentage of upper case, lower case, digits & special characters
in a string
Source Code :
class CharacterPercentage {
public static void main(String[] args) {
String str = args[0];
int upper = 0, lower = 0, digits = 0, special = 0;
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ([Link](ch)) {
upper++;
} else if ([Link](ch)) {
lower++;
} else if ([Link](ch)) {
digits++;
} else {
special++;
}
}
int total = [Link]();
double upperPercent = (upper * 100.0) / total;
double lowerPercent = (lower * 100.0) / total;
double digitPercent = (digits * 100.0) / total;
double specialPercent = (special * 100.0) / total;
[Link]("String: " + str);
[Link]("Uppercase letters: " + upperPercent + "%");
[Link]("Lowercase letters: " + lowerPercent + "%");
[Link]("Digits: " + digitPercent + "%");
[Link]("Special characters: " + specialPercent + "%");
}
}
Output :
Program-12
Aim : Write a program to reverse toggle each word in the string and reverse string also
Source Code :
class ReverseToggle {
public static void main(String[] args) {
String str = args[0];
String[] words = [Link](" ");
StringBuilder toggledReversedWords = new StringBuilder();
// Reverse each word and toggle case
for (String word : words) {
StringBuilder reversedWord = new StringBuilder(word).reverse();
StringBuilder toggledWord = new StringBuilder();
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ([Link](ch)) {
[Link]([Link](ch));
} else if ([Link](ch)) {
[Link]([Link](ch));
} else {
[Link](ch);
}
}
[Link](toggledWord).append(" ");
}
// Reverse the entire string after toggling
String finalResult = new StringBuilder([Link]().trim()).reverse().toString();
[Link]("Original String: " + str);
[Link]("After reverse toggle each word: " + [Link]().trim());
[Link]("After reversing entire string: " + finalResult);
}
}
Output:
Program-13
Aim: Create a Java program with three classes — Person, Employee, and Manager — to
demonstrate all uses of the super keyword in multi-level inheritance
Source Code:
class Person {
String role = "Person";
Person(String name) {
[Link]("Person constructor: " + name);
}
void displayRole() {
[Link]("Role from Person: " + role);
}
}
class Employee extends Person {
String role = "Employee";
Employee(String name, String department) {
super(name);
[Link]("Employee constructor: " + department);
}
void displayRole() {
[Link]("Role from Employee: " + role);
[Link]();
}
}
class Manager extends Employee {
String role = "Manager";
Manager(String name, String department, int teamSize) {
super(name, department);
[Link]("Manager constructor: Team size = " + teamSize);
}
void displayRole() {
[Link]("Role from Manager: " + role);
[Link]();
}
void showAllRoles() {
[Link]("Manager role: " + role);
[Link]("Employee role: " + [Link]);
[Link]("Person role: " + ((Person)this).role);
}
}
public class SuperKeywordDemo {
public static void main(String[] args) {
Manager m = new Manager("Alice", "IT", 5);
[Link]();
[Link]();
}
}
Output:
Program-14
Aim: Write a Java program to show multithreaded producer and consumer application.
Source Code:
import [Link];
public class ProducerConsumerDemo {
public static void main(String[] args) throws InterruptedException {
final PC pc = new PC();
Thread producer = new Thread(() -> {
try {
[Link]();
} catch (InterruptedException e) {
[Link]();
}
});
Thread consumer = new Thread(() -> {
try {
[Link]();
} catch (InterruptedException e) {
[Link]();
}
});
[Link]();
[Link]();
[Link]();
[Link]();
}
public static class PC {
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
public void produce() throws InterruptedException {
int value = 0;
while (true) {
synchronized (this) {
while ([Link]() == capacity)
wait();
[Link]("Producer produced-" + value);
[Link](value++);
notify();
[Link](1000);
}
}
}
public void consume() throws InterruptedException {
while (true) {
synchronized (this) {
while ([Link]() == 0)
wait();
int val = [Link]();
[Link]("Consumer consumed-" + val);
notify();
[Link](1000);
}
}
}
}
}
Output:
Program-15
Aim: Create a Customized Exception and also make use of all the 5 exception keywords.
Source code:
class InvalidAgeException extends Exception {
public InvalidAgeException(String str) {
super(str);
}
}
public class ExceptionDemo {
static void validate(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age is not valid to vote");
} else {
[Link]("Welcome to vote");
}
}
public static void main(String[] args) {
try {
validate(15); // will throw exception
} catch (InvalidAgeException ex) {
[Link]("Caught the exception");
[Link]("Exception occurred: " + [Link]());
} finally {
[Link]("Inside finally block");
}
}
}
Output: