Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Java Program

nil

Uploaded by

Mrs.Minu Meera M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Program

nil

Uploaded by

Mrs.Minu Meera M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

// PROGRAM - 1 GENERATING RANDOM NUMBERS

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);

System.out.println(" THE TRANSLATE COORDINATE : ( " +P.getX() + ","


+P.getY() +
")" +"(" +P1.getX() +"," +P1.getY() +")" );
}
}

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)

enter the xl coordinate:


1
enter the yl coordinate:
2
enter the row displacement:
3
enter the coloumn displacement:
4
RECTANGLE
THE TRANSLATE COORDINATE : ( 6.0,8.0)(2.0,4.0)

enter the xl coordinate:


3
enter the yl coordinate:
4
enter the row displacement:
1
enter the coloumn displacement:
2
RECTANGLE
THE TRANSLATE COORDINATE : ( 2.0,4.0)(6.0,8.0)
// PROGRAM - 3 STRING MANIPULATION
import java.io.*;
public class StringManipulation
{
public static void main(String args[])throws IOException
{
int vowels=0,consonants=0,digits=0,spe_char=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String ");
String str=br.readLine();
str=str.toLowerCase();
for(int i=0; i<str.length(); i++)
{
char ch=str.charAt(i);
if(ch!=' ')
{
if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u') // check vowels
vowels++;
else if((ch >= 'a' && ch <= 'z' )) //check consonants
consonants++;
else if(ch>='0' && ch<='9') //check digits
digits++;
else
spe_char++;
}
}
System.out.println("\nTotal Vowels: "+vowels);
System.out.println("Total Consonants: "+consonants);
System.out.println("Total Digits: "+digits);
System.out.println("Total Special characters: "+spe_char);
}
}

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:

To enter name and email id into file

enter new name : (stop to stop)

Junaitha

enter email id :

junai34@gmail.com

enter new name : (stop to stop)

MinuMeera

enter email id :

rminumeera@yahoo.com

enter new name : (stop to stop)

stop

to find email address from the file for given name

enter name (quit to end)

MinuMeera

emailaddress for MinuMeerais :rminumeera@yahoo.com

enter name (quit to end)

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

JOB LIST CONTAINS KAVITHA

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:

Can't divide a number by 0


Null Pointer Exception Occured
Array Index is Out Of Bounds
//PROGRAM – 7 EXCEPTION HANDLING (B) User-defined Exception
// A Class that represents use-defined exception
class MyException extends Exception {
public MyException(String s)
{
super(s);
}
}

// A Class that uses above MyException


public class Main {
public static void main(String args[])
{
try {
// Throw an object of user defined exception
throw new MyException("Java");
}
catch (MyException e) {
System.out.println("Caught Exception:");

// Print the message from MyException object


System.out.println(e.getMessage());
}
}
}

OUTPUT:

Caught Exception:
Java

// PROGRAM-8A THREAD WITHOUT SYNCHRONIZATION


class Table
{
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 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

PROGRAM 10 – INCORPORATING GRAPHICS


import java.awt.*;
import java.applet.*;
/*<Applet code = "graph" width = 500 height = 500></Applet>*/
public class graph extends Applet
{
public void init()
{
setBackground(Color.white);
}
public void paint(Graphics g)
{
g.drawString("Line",15,10);
g.drawLine(0,0,60,60);
g.drawString("Oval",25,80);
g.drawOval(60,60,60,80);
g.drawString("Filled Round Rectangle",15,155);
g.fillRoundRect(20,160,40,80,25,25);
g.drawString("Filled Arc",130,240);
g.fillArc(130,250,60,80,0,175);
int x[]={250,300,250,300,250};
int y[]={250,250,300,300,250};
int num = 5;
g.drawString("Polygon",240,240);
g.drawPolygon(x,y,num);
}
}
OUTPUT :

PROGRAM 11 – WORKING WITH COLORS AND FONTS


import java.awt.*;
import java.applet.*;
/*<Applet code = "color" width = 500 height=500></Applet>*/
public class color extends Applet
{
public void init()
{
}
public void paint(Graphics g)
{
setBackground(Color.yellow);
g.setColor(Color.blue);
Font F1 = new Font("Serif",Font.BOLD,30);
g.setFont(F1);
g.drawString("Java Programming",100,100);
g.setColor(Color.red);
Font F2 = new Font("Times New Roman",Font.ITALIC,30);
g.setFont(F2);
g.drawString("Java Programming",100,200);
}
}
OUTPUT :

You might also like