PART B
1. Write a java program to demonstrate the concept
of “this” keyword
import [Link].*;
import [Link].*;
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()
{
[Link]("i="+i) ;
[Link]("j="+j) ;
}
public static void main(String args[])
{
Thisdemo a1= new Thisdemo() ;
[Link]() ;
}
}
OUTPUT:
2. Write a java program to demonstrate wrapper class
and its Methods
import [Link].*;
public class Wrapperdemo {
public static void main(String args[]) {
int i = 100;
Integer i1 = new Integer(i);
Integer i2 = [Link]("200");
[Link](" The primitive value of i1=" + [Link]());
[Link](" The primitive value of i2=" + [Link]());
String str1 = "1234";
int num2 = [Link](str1);
[Link](" The value of num2=" + num2);
[Link](" The String value of i1=" + [Link]());
[Link](" The String value of i2=" + [Link]());
}
}
OUTPUT:
[Link] a java program to implement user defined
exceptions
class NegativeAgeException extends Exception {
private int number;
public NegativeAgeException(int number) {
[Link] = number;
}
public String toString() {
return "Age cannot be negative: " + number;
}
}
public class UserDefinedException {
public static void main(String[] args) {
try {
int age = getAge();
if (age < 0) {
throw new NegativeAgeException(age);
}
[Link]("Age is: " + age);
} catch (NegativeAgeException e) {
[Link](e);
}
}
public static int getAge() {
return -6;
}
}
OUTPUT:
4. Write a java program to demonstrate Static variable
and Static Methods
import [Link].*;
class Staticdemo {
static int val = 1024;
static int valmethod() {
return val / 2;
}
}
class Demo {
public static void main(String args[]) {
[Link](" Value is" + [Link]);
[Link] = 4;
[Link](" Value is" + [Link]);
[Link]("calling static method" + [Link]());
}
}
OUTPUT:
5. Write a java program to demonstrate Vectors
import [Link].*;
class Vectordemo
{
public static void main(String args[])
{
Vector v= new Vector() ;
[Link](" C") ;
[Link]("C++") ;
[Link](" Java") ;
[Link](" J2EE") ;
[Link](" Initially the vector content :"+[Link]()) ;
[Link](" The last element is "+ [Link]()) ;
[Link](" VB", 1) ;
[Link] ("C#", 0) ;
[Link](" After inserting the vector content"+[Link]()) ;
[Link](3) ;
[Link](" After remove element at 3, the vector content "+
[Link]()) ;
[Link](" C++", 1) ;
[Link]("VB") ;
[Link](" After removing VB, the vector content "+[Link]()) ;
}
}
OUTPUT:
6. Write a java program to demonstrate the
concatenation of two files.
import [Link].*;
import [Link].*;
class Concatfiles {
public static void main(String args[]) throws IOException
{
FileInputStream n1= new FileInputStream("[Link]");
FileInputStream n2= new FileInputStream("[Link]");
SequenceInputStream s = new SequenceInputStream(n1, n2);
int c;
while(( c= [Link]()) != -1)
{
char ch=((char) c) ;
[Link](ch) ;
}
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
7) Write a program to demonstrate theading using
runnable interface
import [Link];
class Newthread implements Runnable {
Thread t;
Newthread() {
t = new Thread(this, " Demo thread");
[Link](" Child thread");
[Link]();
}
public void run() {
try {
for (int i = 5; i > 0; i--) {
[Link](" Child thread " + i);
[Link](5000);
}
} catch (InterruptedException e) {
[Link](" Child interrupted ");
}
[Link](" Existing child thread");
}
}
class Threaddemo {
public static void main(String args[]) {
new Newthread();
try {
for (int i = 5; i > 0; i--) {
[Link](" Main thread " + i);
[Link](1000);
}
} catch (InterruptedException e) {
[Link](" Main Interrupted");
}
[Link](" Existing main thread");
}
}
OUTPUT: