Java Program
Java Program
import java.io.*;
import java.util.*;
import java.lang.*;
class random1
{
public static void main(String[] args)
{
int a[] = new int[20];
int s,i,j;
Random r=new Random();
for(i=0;i<10;i++)
{
a[i]=r.nextInt(10);
}
System.out.println("before reading");
for(i=0;i<10;i++)
{
System.out.println(""+a[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(a[i]<a[j])
{
s=a[i];
a[i]=a[j];
a[j]=s;
}
}
}
System.out.println("after reading");
for(i=0;i<10;i++)
{
System.out.println(""+a[i]);
}
}
}
OUTPUT :
before reading
3
1
6
9
7
5
7
9
6
8
after reading
1
3
5
6
6
7
7
8
9
9
// PROGRAM - 2 IMPLEMENTING POINT CLASS
import java.io.*;
import java.util.*;
import java.awt.*;
import java.lang.*;
class pointclass
{
public static void main(String[] args)throws IOException
{
int x,y,xl,yl;
String s;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the xl coordinate: ");
s=br.readLine();
xl=Integer.parseInt(s);
System.out.println("enter the yl coordinate: ");
s=br.readLine();
yl=Integer.parseInt(s);
System.out.println("enter the row displacement: ");
s=br.readLine();
x=Integer.parseInt(s);
System.out.println("enter the coloumn displacement: ");
s=br.readLine();
y=Integer.parseInt(s);
Point P=new Point(x,y);
Point P1=new Point(xl,yl);
if(P.getX()==P1.getX() && P.getY()==P1.getY())
{
System.out.println("INVALID");
}
else if(P.getX()==P1.getX())
{
System.out.println("VERTICAL LINE");
}
else if(P.getY()==P1.getY())
{
System.out.println("HORIZONTAL LINE");
}
else if(P.getX()==P1.getY())
{
System.out.println("SQUARE");
}
else
{
System.out.println("RECTANGLE");
}
P.translate(x,y);
P1.translate(xl,yl);
OUTPUT:
enter the xl coordinate:
1
enter the yl coordinate:
1
enter the row displacement:
1
enter the coloumn displacement:
1
INVALID
THE TRANSLATE COORDINATE : ( 2.0,2.0)(2.0,2.0)
OUTPUT :
STRING MANIPULATION
Enter the String
My age is 19
Total Vowels : 3
Total Consonants :4
Total Digits : 2
Total Special character :3
//PROGRAM – 4 DATABASE CREATION FOR EMAIL ADDRESS
import java.io.*;
import java.util.*;
class database {
public static void main(String[] args) throws IOException
{
Properties ht = new Properties();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name,email;
FileInputStream fin = null;
boolean changed = false;
try
{
fin = new FileInputStream("emailid.dat");
}
catch(FileNotFoundException e)
{
System.out.println("database creation");
}
try
{
if(fin!=null)
{
ht.load(fin);
fin.close();
}
}
catch(IOException e)
{
System.out.println("error reading file!");
}
System.out.println("To enter name and email id into file");
do
{
System.out.println("enter new name :" + " (stop to stop) ");
name=br.readLine();
if(name.equals("stop"))
continue;
System.out.println("enter email id : ");
email = br.readLine();
ht.put(name,email);
changed = true;
}
while(!name.equals("stop"));
if(changed)
{
ht.store(new FileWriter("emailid.dat"),"email address book");
}
System.out.println("to find email address from the file for given name");
do
{
System.out.println("enter name " + "(quit to end)");
name = br.readLine();
if(name.equals("quit"))
continue;
email = (String)ht.get(name);
System.out.println("emailaddress for " + name + "is :" + email);
}
while(!name.equals("quit"));
}
}
OUTPUT:
Junaitha
enter email id :
junai34@gmail.com
MinuMeera
enter email id :
rminumeera@yahoo.com
stop
MinuMeera
quit
//PROGRAM – 5 USAGE OF VECTOR CLASS
import java.util.* ;
class Main
{
public static void main(String[] args)
{
Vector V = new Vector(4,2);
System.out.println(" INITIAL SIZE : " +V.size());
System.out.println(" INITIAL CAPACITY : " +V.capacity());
V.addElement(" PRIYA ");
V.addElement(" KAVITHA ");
V.addElement(" SINDHU ");
V.addElement(" BOOMA ");
System.out.println(" SENIOR MOST EMPLOYEE :" +V.firstElement());
System.out.println(" JUNIOR MOST EMPLOYEE : " + V.lastElement());
if(V.contains(" RAGHAVI "))
{
System.out.println(" \n JOB LIST CONTAINS KAVITHA ");
}
Enumeration enum1 = V.elements();
System.out.println(" \n ELEMENTS IN VECTOR : ");
while(enum1.hasMoreElements())
{
System.out.println(enum1.nextElement() +" ");
}
System.out.println();
}
}
OUTPUT :
INITIAL SIZE : 0
INITIAL CAPACITY : 4
SENIOR MOST EMPLOYEE : PRIYA
JUNIOR MOST EMPLOYEE : BOOMA
ELEMENTS IN VECTOR :
PRIYA
KAVITHA
SINDHU
BOOMA
//PROGRAM – 6 CREATE AND IMPORT A PACKAGE
//save as packageDemo.java
package mypackage;
public class packageDemo
{
public void display()
{
System.out.print("Package called");
}
}
//save as Demo.java
import mypackage.*;
class Demo
{
public static void main(String args[])
{
packageDemo obj= new packageDemo();
obj.display();
}
}
OUTPUT:
Package called
//PROGRAM – 7 EXCEPTION HANDLING (A) Built-in Exception
class ExceptionHandling
{
public static void main(String args[])throws Exception
{
try
{
int num1 = 30, num2 = 0;
int result = num1/num2;
System.out.println ("Result = " + result);
}
catch(ArithmeticException e)
{
System.out.println ("Can't divide a number by 0");
}
try
{
String a=null;
System.out.println(a.charAt(3));
}
catch(NullPointerException e)
{
System.out.println("Null Pointer Exception Occured");
}
try
{
int arr[]=new int[10];
System.out.println(arr[15]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index is Out Of Bounds");
}
}
}
OUTPUT:
OUTPUT:
Caught Exception:
Java
OUTPUT:
The Thread name is table 5: 5x1= 5
The Thread name is table 100: 100x1= 100
The Thread name is table 5: 5x2= 10
The Thread name is table 100: 100x2= 200
The Thread name is table 5: 5x3= 15
The Thread name is table 100: 100x3= 300
The Thread name is table 5: 5x4= 20
The Thread name is table 100: 100x4= 400
The Thread name is table 5: 5x5= 25
The Thread name is table 100: 100x5= 500
// PROGRAM-8B THREAD WITH SYNCHRONIZATION
class Table
{
synchronized void printTable(int n)
{
for(int i=1;i<=5;i++)
{
System.out.println("The Thread name is " + Thread.currentThread().getName()+":
"+ n + "x" + i + "= " + n*i );
try
{
Thread.sleep(400);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class MyThread1 extends Thread
{
Table t;
MyThread1(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(5);
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(100);
}
}
public class TestSynchronization2
{
public static void main(String args[])
{
Table obj = new Table();
MyThread1 t1=new MyThread1(obj);
t1.setName("table 5");
MyThread2 t2=new MyThread2(obj);
t2.setName("table 100");
t1.start();
t2.start();
}
}
OUTPUT:
The Thread name is table 5: 5x1= 5
The Thread name is table 5: 5x2= 10
The Thread name is table 5: 5x3= 15
The Thread name is table 5: 5x4= 20
The Thread name is table 5: 5x5= 25
The Thread name is table 100: 100x1= 100
The Thread name is table 100: 100x2= 200
The Thread name is table 100: 100x3= 300
The Thread name is table 100: 100x4= 400
The Thread name is table 100: 100x5= 500