Java Part B
Java Part B
import java.util.*;
import java.io.*;
class Thisdemo
{
int i, j;
Thisdemo()
{
this(100) ;
}
Thisdemo(int i, int j)
{
this.i=i;
this.j=j;
}
Thisdemo(int a)
{
this(a, 200) ;
}
void display()
{
System.out.println("i="+i) ;
System.out.println("j="+j) ;
}
public static void main(String args[])
{
Thisdemo a1= new Thisdemo() ;
a1.display() ;
}
}
OUTPUT:
2. Write a java program to demonstrate wrapper class
and its Methods
import java.io.*;
OUTPUT:
3.Write a java program to implement user defined
exceptions
if (age < 0) {
throw new NegativeAgeException(age);
}
OUTPUT:
4. Write a java program to demonstrate Static variable
and Static Methods
import java.io.*;
class Staticdemo {
static int val = 1024;
class Demo {
public static void main(String args[]) {
System.out.println(" Value is" + Staticdemo.val);
Staticdemo.val = 4;
System.out.println(" Value is" + Staticdemo.val);
System.out.println("calling static method" + Staticdemo.valmethod());
}
}
OUTPUT:
5. Write a java program to demonstrate Vectors
import java.util.*;
class Vectordemo
{
public static void main(String args[])
{
Vector v= new Vector() ;
v.add(" C") ;
v.add("C++") ;
v.add(" Java") ;
v.add(" J2EE") ;
System.out.println(" Initially the vector content :"+v.toString()) ;
System.out.println(" The last element is "+ v.lastElement()) ;
v.insertElementAt(" VB", 1) ;
v.insertElementAt ("C#", 0) ;
System.out.println(" After inserting the vector content"+v.toString()) ;
v.removeElementAt(3) ;
System.out.println(" After remove element at 3, the vector content "+
v.toString()) ;
v.setElementAt(" C++", 1) ;
v.remove("VB") ;
System.out.println(" After removing VB, the vector content "+v.toString()) ;
}
}
OUTPUT:
6. Write a java program to demonstrate the
concatenation of two files.
import java.io.*;
import java.util.*;
class Concatfiles {
public static void main(String args[]) throws IOException
{
FileInputStream n1= new FileInputStream("File1.txt");
FileInputStream n2= new FileInputStream("File2.txt");
SequenceInputStream s = new SequenceInputStream(n1, n2);
int c;
while(( c= s.read()) != -1)
{
char ch=((char) c) ;
System.out.println(ch) ;
}
s.close();
n1.close();
n2.close();
}
}
OUTPUT:
7) Write a program to demonstrate theading using
runnable interface
import java.lang.Thread;
Newthread() {
t = new Thread(this, " Demo thread");
System.out.println(" Child thread");
t.start();
}
class Threaddemo {
public static void main(String args[]) {
new Newthread();
try {
for (int i = 5; i > 0; i--) {
System.out.println(" Main thread " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(" Main Interrupted");
}
System.out.println(" Existing main thread");
}
}
OUTPUT: