0% found this document useful (0 votes)
17 views

Java Part B

The document contains 7 code snippets demonstrating different Java concepts: 1. Use of the "this" keyword to call constructors 2. Wrapper classes and methods for primitive types 3. Implementing user-defined exceptions 4. Static variables and methods 5. Using the Vector class 6. Concatenating files by reading input streams sequentially 7. Creating and running threads using the Runnable interface

Uploaded by

Shahad nazar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java Part B

The document contains 7 code snippets demonstrating different Java concepts: 1. Use of the "this" keyword to call constructors 2. Wrapper classes and methods for primitive types 3. Implementing user-defined exceptions 4. Static variables and methods 5. Using the Vector class 6. Concatenating files by reading input streams sequentially 7. Creating and running threads using the Runnable interface

Uploaded by

Shahad nazar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PART B

1. Write a java program to demonstrate the concept


of “this” keyword

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.*;

public class Wrapperdemo {


public static void main(String args[]) {
int i = 100;
Integer i1 = new Integer(i);
Integer i2 = Integer.valueOf("200");
System.out.println(" The primitive value of i1=" + i1.intValue());
System.out.println(" The primitive value of i2=" + i2.intValue());
String str1 = "1234";
int num2 = Integer.parseInt(str1);
System.out.println(" The value of num2=" + num2);
System.out.println(" The String value of i1=" + i1.toString());
System.out.println(" The String value of i2=" + i2.toString());
}
}

OUTPUT:
3.Write a java program to implement user defined
exceptions

class NegativeAgeException extends Exception {


private int number;

public NegativeAgeException(int number) {


this.number = 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);
}

System.out.println("Age is: " + age);


} catch (NegativeAgeException e) {
System.out.println(e);
}
}

public static int getAge() {


return -6;
}
}

OUTPUT:
4. Write a java program to demonstrate Static variable
and Static Methods

import java.io.*;

class Staticdemo {
static int val = 1024;

static int valmethod() {


return val / 2;
}
}

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;

class Newthread implements Runnable {


Thread t;

Newthread() {
t = new Thread(this, " Demo thread");
System.out.println(" Child thread");
t.start();
}

public void run() {


try {
for (int i = 5; i > 0; i--) {
System.out.println(" Child thread " + i);
Thread.sleep(5000);
}
} catch (InterruptedException e) {
System.out.println(" Child interrupted ");
}
System.out.println(" Existing child thread");
}
}

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:

You might also like