PART-A
1. Implement a stack using a stack class which has methods push, pop.
import java.util.Stack;
class StackMain
{
public static void main(String[] args)
{
Stack<String> courses= new Stack <String>();
courses.push("BBA");
courses.push("BCA");
courses.push("BA");
System.out.println("stack: " + courses);
courses.pop();
System.out.println("stack after pop: " + courses);
}
}
2.Shapes using function overloading.
3.Geometrical figures using abstract class.
4.Sort the vector and display.
import java.util.*;
import java.util.Vector;
public class VectorSort
{
public static void main(String args[])
{
Vector<Integer>in=new Vector<Integer>();
in.add(30);
in.add(70);
in.add(50);
in.add(10);
in.add(40);
in.add(20);
in.add(60);
in.add(100);
System.out.println("Original vector:"+ in);
Collections.sort(in);
System.out.println("ln sorted vector:"+ in);
}
}
5.Multiple catch claues to handle exceptions
public class MultipleCatchBlock1
{
public static void main(String[] args)
{
try
{
int a[]=new int[5];
a[5]=25/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
System.out.println("rest of the code");
}
}
6.Applet which recieves font name, font size as parameters.
7.Reads a file and displays the files with a line no before each line.
import java.util.*;
import java.io.*;
class Rfile
{
public static void main(String args[])throws IOException
{
int j=1;
char ch;
Scanner scr=new Scanner(System.in);
System.out.println("\nEnter File name:");
String str=scr.next();
FileInputStream f=new FileInputStream(str);
System.out.println("\n contents of the files are");
int n=f.available();
System.out.print(j+": ");
for(int i=0;i<n;i++)
{
ch=(char)f.read();
System.out.println(ch);
if(ch=='\n')
{
System.out.print(++j+": ");
}
}
}
}
8.Displays the number of characters, lines and words in a text file.
import java.util.*;
import java.io.*;
class Cfile
{
public static void main(String args[])throws IOException
{
int n1=1,nw=0;
char ch;
Scanner scr=new Scanner(System.in);
System.out.println("\nEnter file name:");
String str=scr.nextLine();
FileInputStream f=new FileInputStream(str);
int n=f.available();
for(int i=0;i<n;i++)
{
ch=(char)f.read();
if(ch=='\n')
n1++;
else if(ch==' ')
nw++;
}
System.out.println("\nNumber of lines:"+ n1);
System.out.println("\nNumber of words:"+(n1+nw));
System.out.println("\nNumber of character:"+n);
}
}
PART-B
1.Find Factorial.
public class Factorial{public static void main(String args[]){int[] arr=new int[10];int fact;if(args.length==0){System.out.println("No command line arguments");return;}for(int i=0;i<args.length;i++){arr[i]=Integer.parseInt(args[i]);}for(int i=0;i<args.length;i++){fact=1;while(arr[i]>0){fact=fact*arr[i];arr[i]--;}System.out.println("Factorial of"+ args[i]+"is:"+fact);}}}
2.Prime no. b/w two limits.
public class Factorial
{
public static void main(String args[])
{
int[] arr=new int[10];
int fact;
if(args.length==0)
{
System.out.println("No command line arguments");
return;
}
for(int i=0;i<args.length;i++)
{
arr[i]=Integer.parseInt(args[i]);
}
for(int i=0;i<args.length;i++)
{
fact=1;
while(arr[i]>0)
{
fact=fact*arr[i];
arr[i]--;
}
System.out.println("Factorial of"+ args[i]+"is:"+fact);
}
}
}