1.
Classes and Object
import [Link].*;
class Model
{
int age;
String name,gen;
void get()
{
DataInputStream ba;
ba=new DataInputStream([Link]);
try
{
[Link]("Enter the name");
name=[Link]();
[Link]("Enter the gender");
gen=[Link]();
[Link]("Enter the age");
age=[Link]([Link]());
}
catch(IOException e)
{
[Link]("error");
}
}
void out()
{
[Link]("The Data is Inserted as ...");
[Link]("Name = "+name);
[Link]("Gender ="+gen);
[Link]("Age ="+age);
}
}
class Model1
{
public static void main(String args[])
{
Model obj=new Model();
[Link]();
[Link]();
}
}
Output:
U:\>java Model1
Enter the name: karthick
Enter the gender: male
Enter the age: 28
The data is inserted as……..
Name: karthick
Gender: male
Age: 28.
[Link]
Single Inheritance :
import [Link].*;
class Odd
{
void oddvalue(int x)
{
[Link]("The entered number "+x+" an Odd number .");
}
}
class Even extends Odd
{
void evenvalue(int x)
{
[Link]("The entered number "+x+" an Even number .");
}
}
class Value
{
public static void main(String args[])
{
DataInputStream din;
din=new DataInputStream([Link]);
try
{
int no;
[Link]("Enter a number : ");
no=[Link]([Link]());
Even en=new Even();
if ((no%2)==0)
{
[Link](no);
}
else
{
[Link](no);
}
}
catch(IOException e)
{
[Link]("Error !!!");
}
}
}
Output:
U:\>java Value
Enter a number:6
The number as an even number
Multilevel Inheritance
import [Link].*;
class Odd
{
void oddvalue(int x)
{
[Link]("The entered number "+x+" an Odd number .");
}
}
class Even extends Odd
{
void evenvalue(int x)
{
[Link]("The entered number "+x+" an Even number .");
}
}
class Less extends Even
{
void lessvalue(int x)
{
[Link]("The entered number "+x+" is lesser than Zero .");
}
}
class Multivalue
{
public static void main(String args[])
{
DataInputStream din;
din=new DataInputStream([Link]);
try
{
int no;
[Link]("Enter a number : ");
no=[Link]([Link]());
Even en=new Even();
Less ls=new Less();
if (no<0)
{
[Link](no);
}
else
{
if ((no%2)==0)
{
[Link](no);
}
else
{
[Link](no);
}
}
}
catch(IOException e)
{
[Link]("Error !!!");
}
}
}
Output:
i)U:\>java Multivalue
Enter the number:3
The entered number 3 as an odd number
ii) U:\>java Multivalue
Enter the number:-9
The entered number-9 is less than zero.
Hierarchical Inheritance
import [Link].*;
class Value
{
void content(int x)
{
if ((x%2)==0)
{
Even en=new Even();
[Link](x);
}
else
{
Odd od=new Odd();
[Link](x);
}
}
}
class Odd extends Value
{
void oddvalue(int x)
{
[Link]("The entered number "+x+" an Odd number .");
}
}
class Even extends Value
{
void evenvalue(int x)
{
[Link]("The entered number "+x+" an Even number .");
}
}
class Hiervalue
{
public static void main(String args[])
{
DataInputStream din;
din=new DataInputStream([Link]);
try
{
int no;
[Link]("Enter a number : ");
no=[Link]([Link]());
Odd od=new Odd();
[Link](no);
}
catch(IOException e)
{
[Link]("Error !!!");
}
}
}
Output:
i)U:\>java Hiervalue
Enter a number:8
The number 8 as an even number
ii) U:\>java Hiervalue
Enter a number:5
The number 5 as an odd number
3)Multithreading
Synchronization
class Share extends Thread
{
static String msg[]={"This", "is", "a", "synchronized", "variable"};
Share(String threadname){
super(threadname);
}
public void run()
{
display(getName());
}
public void display(String threadN){
synchronized([Link]){
for(int i=0;i<=4;i++)
[Link](threadN+msg[i]);
Try
{
[Link](1000);
}
catch(Exception e)
{
}
[Link](" Object Exiting ");
}
}
}
public class SynStatement1 {
public static void main(String[] args) {
Share t1=new Share("Thread One: ");
[Link]();
Share t2=new Share("Thread Two: ");
[Link]();
}
}
Output :
U:\>javac [Link]
U:\> java SynStatement1
Thread One :This
Thread One :is
Thread One :a
Thread One :synchronized
Thread One :variable
Object Exiting
Thread Two :This
Thread Two :is
Thread Two :a
Thread Two :synchronized
Thread Two :variable
Object Exiting
Thread priorities
public class Main
{
public void setPrioritiesOnThreads()
{
Thread thread1 = new Thread(new TestThread(1));
Thread thread2 = new Thread(new TestThread(2));
//Setting priorities on the Thread objects
[Link](Thread.MAX_PRIORITY);
[Link](Thread.MIN_PRIORITY);
[Link]();
[Link]();
try {
//Wait for the threads to finish
[Link]();
[Link]();
} catch (InterruptedException ex) {
[Link]();
}
[Link]("Done.");
}
public void noPrioritiesOnThreads()
{
Thread thread3 = new Thread(new TestThread(3));
Thread thread4 = new Thread(new TestThread(4));
[Link]();
[Link]();
try
{
[Link]();
[Link]();
}
catch (InterruptedException ex)
{
[Link]();
}
[Link]("Done.");
}
public static void main(String[] args)
{
[Link]("Thread without Priority");
new Main().noPrioritiesOnThreads();
[Link]("Priority on Thread");
new Main().setPrioritiesOnThreads();
}
class TestThread implements Runnable
{
int id;
public TestThread(int id) {
[Link] = id;
}
public void run()
{
for (int i = 1; i <= 5; i++)
{
[Link]("Thread" + id + ": " + i);
}
}
}
}
Output
G:\>java Main
Thread without Priority
Thread3: 1
Thread4: 1
Thread3: 2
Thread4: 2
Thread3: 3
Thread4: 3
Thread3: 4
Thread4: 4
Thread3: 5
Thread4: 5
Done.
Priority on Thread
Thread1: 1
Thread1: 2
Thread1: 3
Thread1: 4
Thread1: 5
Thread2: 1
Thread2: 2
Thread2: 3
Thread2: 4
Thread2: 5
Done.
Note : The Output may vary Depending on the CPU speed .
4)Packages&interfaces
Pack1 coding:
package pack1;
public interface Bnw
{
public void bkwt();
}
Pack2 coding:
package pack2;
public interface Color
{
public void colorcp();
}
Main Program :
import [Link].*;
import [Link].*;
import pack1.*;
import pack2.*;
class Photocopy implements Bnw,Color
{
public void bkwt()
{
[Link]("Total amount for black and white copy: ");
}
public void colorcp()
{
[Link]("Total amount for color photocopy");
}
}
class Output
{
public static void main(String args[])
{
DataInputStream din=new DataInputStream([Link]);
Photocopy cp=new Photocopy();
double bnw,cn,bw,c;
try
{
[Link]("Enter number of normal pages :");
bnw=[Link]([Link]());
bw=bnw*6.50;
[Link]("Enter no of color pages:");
cn=[Link]([Link]());
c=cn*8;
[Link]();
[Link](bw);
[Link]();
[Link](c);
}
catch(IOException e)
{
[Link]("Error");
}
}
}
Output :
U:\> cd pack1
U:\pack1>javac [Link]
U:\pack1>cd..
U:\>cd pack2
U:\pack2> javac [Link]
U:\pack2>cd..
U:\>javac [Link]
U:\>java Output
Enter the number of Normal pages : 3
Enter the number of Color pages :1
Total amount for black and white copy : 19.5
Total amount for Color copy : 8.0
[Link]
import [Link].*;
class RupeetoDollar
{
double dollar;
void convert (double rupe,double rate)
{
dollar=rupe*rate;
[Link]("Rupee :"+rupe);
[Link]("Dollar :"+dollar);
}
void convert (double rupee)
{
dollar=rupee*54.60;
[Link]("Rupee :"+rupee);
[Link]("Dollar :"+dollar);
}
}
class Convert
{
public static void main(String args[])
{
double rup,rate;
RupeetoDollar obj=new RupeetoDollar();
DataInputStream din;
din=new DataInputStream([Link]);
try
{
[Link]("Enter Rupee value to be converted:");
rup=[Link]([Link]());
[Link]("Default Dollar Rate :54.60");
[Link](rup);
[Link]("Enter Today's Dollar rate:");
rate=[Link]([Link]());
[Link](rup,rate);
}
catch(IOException e)
{
[Link]("Error !!!");
}
}
}
Output:
U:\ java Convert
Enter the rupee value to be converted:
30
Default dollar rate:54.60
Rupee:30
Dollar:1638
Enter today’s dollar rate:45
Rupee:30
Dollar rate:1350.0
6. String handling
import [Link].*;
class Stringhandling
{
public static void main(String args[])
{
int ch;
String c1,c2;
[Link](" 1. String Constructors ");
[Link](" 2. String Length ");
[Link](" 3. String Concatenation ");
[Link](" 4. Character Extraction ");
[Link](" String Comparison ");
[Link](" 5. Equal or Not ");
[Link](" 6. starts with and ends with ");
[Link](" 7. compareTo ");
[Link](" Modifying a String ");
[Link](" 8. replace ");
[Link](" 9. trim ");
[Link](" 10. Change Case");
[Link]("Enter the choice : ");
DataInputStream din;
din=new DataInputStream([Link]);
try
{
ch=[Link]([Link]());
switch (ch)
{
case 1:
char cd []={'J','A','V','A','_','U','S','I','N','G','_','O','O','P','S'};
String s1 = new String(cd);
String s2 = new String(cd,5,5);
byte ascii[]={74,65,73,67};
String s3=new String(ascii);
[Link]("String cd = "+s1);
[Link]("String of start index :4 and characters 5 : "+s2);
[Link]("The Character representation of ascii 74,65,73,67 is:
"+s3);
break;
case 2:
[Link]("Enter String : ");
c1=[Link]();
[Link]("String Length of "+c1+" is : "+[Link]());
break;
case 3:
[Link]("Enter String 1 : ");
c1=[Link]();
[Link]("Enter String 2 : ");
c2=[Link]();
[Link]("The Concatenated String is "+c1+" and "+c2);
[Link]("The Concatenated String is "+[Link](c2));
break;
case 4:
c1="Sample program for Character Extraction in Java ";
[Link]("String : "+c1);
char buf[]=new char[10];
[Link](7,15,buf,1);
[Link]("The Extracted String is : ");
[Link](buf);
break;
case 5:
[Link]("Enter String 1 : ");
c1=[Link]();
[Link]("Enter String 2 : ");
c2=[Link]();
[Link](c1+" is equal to "+c2+" -> "+[Link](c2));
[Link](c1+" is equal(Ignoring case) to "+c2+" ->
"+[Link](c2));
break;
case 6:
[Link]("Enter String : ");
c1=[Link]();
[Link](c1+" starts with 'Tool' -> "+[Link]("Tool"));
[Link](c1+" ends with 'bar' -> "+[Link]("bar"));
break;
case 7:
[Link]("Enter String 1 : ");
c1=[Link]();
[Link]("Enter String 2 : ");
c2=[Link]();
int no=[Link](c2);
if(no==0)
{
[Link](c1+" is equal to "+c2+" -> true");
}
else
{
[Link](c1+" is equal to "+c2+" -> false");
}
break;
case 8:
c1="meneging";
[Link]("Encrpted String is : "+c1);
[Link]("Key : Replace 'e' by 'a'");
c2=[Link]('e','a');
[Link]("Decrypted String is : "+c2);
break;
case 9:
c1=" Jaic ";
[Link]("String :"+c1);
[Link]("Trimmed String :"+[Link]());
break;
case 10:
[Link]("Enter Your name : ");
c1=[Link]();
String upper=[Link]();
String lower=[Link]();
[Link]("UpperCase : "+upper);
[Link]("LowerCase : "+lower);
break;
default:
[Link](" Invalid choice ...");
}
}
catch(IOException e)
{
[Link](" Error !!!");
}
}
}
Output:
U:\ java String handling
[Link]
2. String Length
[Link] Concatenation
4. Character Extraction
String Comparison
5. Equal or Not
6. starts with and ends with
7. compareTo
Modifying a String
8. replace
9. trim
10. Change Case
Enter the choice : 1
String cd = JAVA_USING_OOPS
String of start index :4 and characters 5 : USING
The Character representation of ascii 74,65,73,67 is: JAIC
Enter the choice : 2
Enter String : book
String Length of book is : 4
[Link] handling
UserDefinedException :
import [Link];
import [Link];
import [Link];
class MyException extends Exception
{
MyException(){}
public String toString(){
return "Exception : Number should be greater than Zero";
}
}
public class UserException
{
public static void main(String[] args)
{
try {
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter number");
int n = [Link]([Link]());
check(n);
}
catch (MyException e) {
[Link](e);
}catch (IOException e) {
[Link](e);
}
}
static int factorial(int no)
{
if(no==1)
{
return 1;
}
else
{
return(no*factorial(no-1));
}
}
static void check(int no)throws MyException{
if(no>0){
int fact=0;
fact=fact+factorial(no);
[Link]("Factorial of "+no+" is :"+fact);
}else {
throw new MyException();
}
}
}
Output:
i)U:\ java UserException
Enter the number:6
Factorial of 6 is:720
ii)U:\ java UserException
Enter the number:-6
Exception: number should be greater than zero.
[Link]-DATA
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
/* <applet code="BioDataForm" width=900 height=650></applet> */
public class BioDataForm extends Applet implements ActionListener
{
Label l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11;
TextArea ta;
TextField t1,t2,t3,t4,t5,t6;
Checkbox c1,c2,c3,c4,c5,c6,c7,c8;
CheckboxGroup cbg1,cbg2;
Button b1,b2;
public void init()
{
try
{
setLayout(null);
setBackground([Link]);
l1=new Label("Bio-Data");
l2=new Label("Name");
l3=new Label("Age");
l4=new Label("Sex");
l5=new Label("Date Of Birth");
l6=new Label("Marital Status");
l7=new Label("Nationality");
l8=new Label("Phone Number");
l9=new Label("Qualification");
l10=new Label("Updated Data");
l11=new Label("( dd/mm/yyyy ) ");
b1=new Button("OK");
b2=new Button("Cancel");
t1=new TextField(30);
t2=new TextField(3);
t3=new TextField(10);
t4=new TextField(10);
t5=new TextField(10);
t6=new TextField(10);
cbg1=new CheckboxGroup();
cbg2=new CheckboxGroup();
c1=new Checkbox("Male",cbg1,false);
c2=new Checkbox("Female",cbg1,false);
c3=new Checkbox("Bachelor",cbg2,false);
c4=new Checkbox("Spinster",cbg2,false);
c5=new Checkbox("Married",cbg2,false);
c6=new Checkbox("Divorced",cbg2,false);
c7=new Checkbox("Widower",cbg2,false);
c8=new Checkbox("Widow",cbg2,false);
ta=new TextArea();
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
add(l8);
add(l9);
add(l10);
add(l11);
add(c1);
add(c2);
add(c3);
add(c4);
add(c5);
add(c6);
add(c7);
add(c8);
add(t1);
add(t2);
add(t3);
add(t4);
add(t5);
add(t6);
add(b1);
add(b2);
add(ta);
[Link](190,80,110,20);
[Link](190,110,110,20);
[Link](190,170,110,20);
[Link](190,230,110,20);
[Link](190,260,110,20);
[Link](190,290,110,20);
[Link](420,40,110,20);
[Link](60,80,110,20);
[Link](60,110,110,20);
[Link](60,140,110,20);
[Link](60,170,110,20);
[Link](60,200,110,20);
[Link](60,230,110,20);
[Link](60,260,110,20);
[Link](60,290,110,20);
[Link](120,420,110,20);
[Link](320,170,110,20);
[Link](190,140,110,20);
[Link](310,140,110,20);
[Link](190,200,110,20);
[Link](310,200,110,20);
[Link](430,200,110,20);
[Link](550,200,110,20);
[Link](670,200,110,20);
[Link](790,200,110,20);
[Link](90,350,110,20);
[Link](240,350,110,20);
[Link](90,500,110,20);
[Link](this);
[Link](this);
[Link](65,470,540,140);
}
catch(Exception e)
{
[Link]("Error");
}
}
public void actionPerformed(ActionEvent ae)
{
if([Link]()==b1)
{
[Link]("\n Name : "+[Link]());
[Link](" Age : "+[Link]());
}
if([Link]())
{
[Link](" Sex : "+[Link]());
}
if([Link]())
{
[Link](" Sex : "+[Link]());
}
if([Link]()==b1)
{
[Link](" Date Of Birth : "+[Link]());
}
if([Link]())
{
[Link](" Marital Status : "+[Link]());
}
if([Link]())
{
[Link](" Marital Status : "+[Link]());
}
if([Link]())
{
[Link](" Marital Status : "+[Link]());
}
if([Link]())
{
[Link](" Marital Status : "+[Link]());
}
if([Link]())
{
[Link](" Marital Status : "+[Link]());
}
if([Link]())
{
[Link](" Marital Status : "+[Link]());
}
if([Link]()==b1)
{
[Link](" Nationality : "+[Link]());
[Link](" Phone Number : "+[Link]());
[Link](" Qualification : "+[Link]());
}
if([Link]()==b2)
{
[Link](false);
setBackground([Link]);
}
}
}
OUTPUT :
[Link] Balls
/*<applet code="Moving_ball.class" width=300 height=300></applet>*/
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
public class Moving_ball extends Applet
{
Thread t;
int i;
int x=34,y=14;
public void init()
{
t=new Thread();
}
public void paint(Graphics g)
{
for(i=1;i<=5;i++)
{
[Link](x,y,40,40);
[Link]([Link]);
x=x+30;
y=y+30;
repaint();
try
{
[Link](1000);
}
catch(Exception e)
{}
}
}
public static void main(String args[])
{
Moving_ball mb=new Moving_ball();
}
}
Output :
U:\>javac Moving_ball.java
U:\>appletviewer Moving_ball.java
[Link]
//<APPLET code=[Link] height=380 name=calculator width=201
VIEWASTEXT><PARAM //NAME="foreground" VALUE="FFFFFF"><PARAM
NAME="background" VALUE="008080"><PARAM NAME="label" //VALUE="This string
was passed from the HTML host."></APPLET>
import [Link].*;
import [Link].*;
public class Calcul extends [Link] implements ActionListener {
TextField txtTotal = new TextField("");
Button button[] = new Button[10];
Button divide = new Button("/");
Button mult = new Button("*");
Button plus = new Button ("+");
Button minus = new Button("-");
Button isequalto = new Button("=");
Button clear = new Button("C");
double num ,numtemp ;
int counter;
String strnum = "",strnumtemp = "" ;
String op = "";
public void operation() {
counter ++;
if (counter == 1) {
numtemp = num;
strnum = "";
num = 0;
}else{
if (op == "+") numtemp += num;
else if (op == "-") numtemp -= num;
else if (op == "*") numtemp = numtemp * num;
else if (op == "/") numtemp = numtemp / num;
strnumtemp = [Link](numtemp);
[Link](strnumtemp);
strnum = "";
num = 0;
}
}
public void init() {
setLayout(null);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
for(int i = 0;i <= 9; i ++)
{
button[i] = new Button([Link](i));
button[i].setForeground([Link]);
}
button[1].setBounds(0,53,67,53);
button[2].setBounds(67,53,67,53);
button[3].setBounds(134,53,67,53);
button[4].setBounds(0,106,67,53);
button[5].setBounds(67,106,67,53);
button[6].setBounds(134,106,67,53);
button[7].setBounds(0,159,67,53);
button[8].setBounds(67,159,67,53);
button[9].setBounds(134,159,67,53);
for (int i = 1;i <= 9; i ++)
{
add(button[i]);
}
[Link](0,0,200,53);
add(txtTotal);
[Link](0,212,67,53);
add(plus);
button[0].setBounds(67,212,67,53);
add(button[0]);
[Link](134,212,67,53);
add(minus);
[Link](134,264,67,53);
add(divide);
[Link](67,264,67,53);
add(isequalto);
[Link](0,264,67,53);
add(mult);
[Link](67,316,67,53);
add(clear);
}
public void start() {
for(int i = 0;i <= 9; i ++) {
button[i].addActionListener(this);
}
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
public void stop() {
for(int i = 0;i <= 9; i ++) {
button[i].addActionListener(null);
}
[Link](null);
[Link](null);
[Link](null);
[Link](null);
[Link](null);
[Link](null);
}
public void actionPerformed(ActionEvent e)
{
for(int i = 0;i <= 9; i++)
{
if ([Link]() == button[i])
{
play(getCodeBase(),i + ".au");
strnum += [Link](i);
[Link](strnum);
num = [Link](strnum).doubleValue();
}
}
if ([Link]() == plus)
{
operation();
op = "+";
}
if ([Link]() == minus)
{
operation();
op = "-";
}
if ([Link]() == divide) {
operation();
op = "/";
}
if ([Link]() == mult) {
operation();
op = "*";
}
if ([Link]() == isequalto) {
if (op == "+") numtemp += num;
else if (op == "-") numtemp -= num;
else if (op == "*") numtemp = numtemp * num;
else if (op == "/") numtemp = numtemp / num;
strnumtemp = [Link](numtemp);
[Link](strnumtemp);
strnumtemp = "";
numtemp = 0;
strnum = "";
num = 0;
counter = 0;
}
if ([Link]() == clear) {
[Link]("0");
strnumtemp = "";
numtemp = 0;
strnum = "";
num = 0;
counter = 0;
}
}
}
Output: