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
[Link]("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++) {
[Link](arr[i] + ", ");
}
}
public static void main(String[] args) {
Stack stack = new Stack(5);
[Link](1);
[Link](2);
[Link](3);
[Link]("Stack: ");
[Link]();
// remove element from stack
[Link]();
[Link]("\nAfter popping out");
[Link]();
}
}
2. Create a Java program to implement the concept of Queue.
import [Link];
import [Link];
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++)
[Link](i);
// Display contents of the queue.
[Link]("Elements of queue "
+ q);
// To remove the head of queue.
int removedele = [Link]();
[Link]("removed element-"
+ removedele);
[Link](q);
// To view the head of queue
int head = [Link]();
[Link]("head of queue-"
+ head);
// Rest all methods of collection
// interface like size and contains
// can be used with this
// implementation.
int size = [Link]();
[Link]("Size of queue-"
+ size);
}
}
3. Create a package in java to show dynamic polymorphism.
Method Overriding
File Name – [Link]
package poly;
public class Parent{
public void Run(){
[Link]("Parent Class Activated...");
}
}
File Name – [Link]
import poly.*;
class Sub_Child extends Parent{
public void Run(){
[Link]("Child Class Activated...");
}
}
class Child{
public static void main(String args[])
{
Sub_Child abc = new Sub_Child();
[Link]();
}
}
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
[Link](
"Thread " + [Link]().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
[Link]("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();
[Link]();
}
}
}
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 {
[Link]("welcome to vote");
}
}
// main method
public static void main(String args[])
{
try
{
// calling the method
validate(13);
}
catch (InvalidAgeException ex)
{
[Link]("Caught the exception");
// printing the message from InvalidAgeException object
[Link]("Exception occured: " + ex);
}
[Link]("rest of the code...");
}
}
6. Convert the content of a given file into the uppercase content
of the same file.
import [Link].*;
public class Mainfile{
public static void main(String[] args){
File fileToBeModified = new File("D:/java/[Link]");
String oldContent = "";
BufferedReader reader = null;
FileWriter writer = null;
try{
reader = new BufferedReader(newFileReader(fileToBeModified));
String line = [Link](); //Reading the content
of input text file
while (line != null) {
oldContent = oldContent + line +
[Link]();
line = [Link]();
}
//printing the original content
[Link]("Original Content of the file: " +
oldContent);
//Replacing lowerCase text to upperCase text
String newContent = [Link]();
//Rewriting the input text file with newContent
writer = new FileWriter(fileToBeModified);
[Link](newContent); //Printing the content of
modified file
//printing the content of the modified file
[Link]("New content of the file: " +
newContent);
}
catch (IOException e){
[Link]();
}
finally{
try{
//Closing the resources
[Link]();
[Link]();
}
catch (IOException e) {
[Link]();
}
}
}
}
7. Write java program to print Number-increasing Pyramid pattern
using function.
import [Link].*;
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.
[Link](j + " ");
}
// print new line for each row
[Link]();
}
}
// 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.
[Link](j + " ");
}
// print new line for each row
[Link]();
}
}
// Driver Function
public static void main(String args[])
{
int n = 5;
printPattern(n);
}
}