1. Create a Java program to implement the concept of Stack.
class Stack {
private int arr[]; // store elements of stack
private int top; // represent top of stack
private int capacity; // total capacity of the stack
// Creating a stack
Stack(int size) {
arr = new int[size]; // initialize the array
capacity = size; // initialize the stack variables
top = -1;
}
// push elements to the top of stack
public void push(int x)
{
// insert element on top of stack
System.out.println("Inserting " + x);
arr[++top] = x;
}
// pop elements from top of stack
public int pop() {
// pop element from top of stack
return arr[top--];
}
// display elements of stack
public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + ", ");
}
}
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(1);
stack.push(2);
stack.push(3);
System.out.print("Stack: ");
stack.printStack();
// remove element from stack
stack.pop();
System.out.println("\nAfter popping out");
stack.printStack();
}
}
2. Create a Java program to implement the concept of Queue.
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args)
{
Queue<Integer> q
= new LinkedList<>();
// Adds elements {0, 1, 2, 3, 4} to
// the queue
for (int i = 0; i < 5; i++)
q.add(i);
// Display contents of the queue.
System.out.println("Elements of queue "
+ q);
// To remove the head of queue.
int removedele = q.remove();
System.out.println("removed element-"
+ removedele);
System.out.println(q);
// To view the head of queue
int head = q.peek();
System.out.println("head of queue-"
+ head);
// Rest all methods of collection
// interface like size and contains
// can be used with this
// implementation.
int size = q.size();
System.out.println("Size of queue-"
+ size);
}
}
3. Create a package in java to show dynamic polymorphism.
Method Overriding
File Name – Parent.Java
package poly;
public class Parent{
public void Run(){
System.out.println("Parent Class Activated...");
}
}
File Name – Child.Java
import poly.*;
class Sub_Child extends Parent{
public void Run(){
System.out.println("Child Class Activated...");
}
}
class Child{
public static void main(String args[])
{
Sub_Child abc = new Sub_Child();
abc.Run();
}
}
4. write a java program to demonstrate multithreading
// Java code for thread creation by extending
// the Thread class
class Multithreading extends Thread {
public void run()
{
try {
// Displaying the thread that is running
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
// Main Class
public class Multithread {
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
Multithreading object = new Multithreading();
object.start();
}
}
}
5. Create a customized exception and use all 5 exception
keywords.
/ class representing custom exception
class InvalidAgeException extends Exception
{
public InvalidAgeException (String str)
{
// calling the constructor of parent Exception
super(str);
}
}
// class that uses custom exception InvalidAgeException
public class TestCustomException1
{
// method to check the age
static void validate (int age) throws InvalidAgeException{
if(age < 18){
// throw an object of user defined exception
throw new InvalidAgeException("age is not valid to vote");
}
else {
System.out.println("welcome to vote");
}
}
// main method
public static void main(String args[])
{
try
{
// calling the method
validate(13);
}
catch (InvalidAgeException ex)
{
System.out.println("Caught the exception");
// printing the message from InvalidAgeException object
System.out.println("Exception occured: " + ex);
}
System.out.println("rest of the code...");
}
}
6. Convert the content of a given file into the uppercase content
of the same file.
import java.io.*;
public class Mainfile{
public static void main(String[] args){
File fileToBeModified = new File("D:/java/message.txt");
String oldContent = "";
BufferedReader reader = null;
FileWriter writer = null;
try{
reader = new BufferedReader(newFileReader(fileToBeModified));
String line = reader.readLine(); //Reading the content
of input text file
while (line != null) {
oldContent = oldContent + line +
System.lineSeparator();
line = reader.readLine();
}
//printing the original content
System.out.println("Original Content of the file: " +
oldContent);
//Replacing lowerCase text to upperCase text
String newContent = oldContent.toUpperCase();
//Rewriting the input text file with newContent
writer = new FileWriter(fileToBeModified);
writer.write(newContent); //Printing the content of
modified file
//printing the content of the modified file
System.out.println("New content of the file: " +
newContent);
}
catch (IOException e){
e.printStackTrace();
}
finally{
try{
//Closing the resources
reader.close();
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
7. Write java program to print Number-increasing Pyramid pattern
using function.
import java.util.*;
public class Pyramid {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle number of rows
for (i = 1; i <= n; i++) {
// inner loop to handle number of columns
for (j = 1; j <= i; j++) {
// printing column values upto the row
// value.
System.out.print(j + " ");
}
// print new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 5;
printPattern(n);
}
}
8. Write java program to print Number-increasing reverse Pyramid
pattern using function.
public class GeeksForGeeks {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle number of rows
for (i = n; i >= 1; i--) {
// inner loop to handle number of columns
for (j = 1; j <= i; j++) {
// printing column values upto the row
// value.
System.out.print(j + " ");
}
// print new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 5;
printPattern(n);
}
}