Java Practical Assignment
Java Practical Assignment
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;
}
System.out.println(q);
Method Overriding
File Name – Parent.Java
package poly;
public class Parent{
public void Run(){
System.out.println("Parent Class Activated...");
}
}
import poly.*;
// 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.
// main method
public static void main(String args[])
{
try
{
// calling the method
validate(13);
}
catch (InvalidAgeException ex)
{
System.out.println("Caught the exception");
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();
}
import java.util.*;
// 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.
// Driver Function
public static void main(String args[])
{
int n = 5;
printPattern(n);
}
}