/* Write a Java program to Illustrate a concept of class box with constructor.
*/ class Box { private double length; double breadth; Box() { [Link]("From default Constructor"); length=0; breadth=0; } Box(double l,double b) { [Link]("From Parameterised"); length=l; breadth=b; } Box(Box b) { [Link]("using Copy Constructor"); [Link]=[Link]; [Link]=[Link]; } void set_dimensions(double c,double d) { length=c; breadth=d; } double area() { return length*breadth; } double perimeter() { return (2*(length+breadth));
Page No. 1
} } class BoxDemo { public static void main(String args[]) { Box mybox1=new Box(); mybox1.set_dimensions(10.25,20.35); [Link]("Area of a MyBox1 is:"+[Link]()); [Link]("Perimeter of a MyBox1 is:"+[Link]()); Box mybox2=new Box(10.25,15.75); [Link]("Area of a MyBox2 is:"+[Link]()); [Link]("Perimeter of a MyBox2 is:"+[Link]()); Box mybox3=new Box(mybox2); [Link]("Area of a MyBox3 is:"+[Link]()); [Link]("Perimeter of a MyBox3 is:"+[Link]()); } }
Page No. 2
OUTPUT: From default Constructor Area of a MyBox1 is:208.5875 Perimeter of a MyBox1 is:61.2 From Parameterised Area of a MyBox2 is:161.4375 Perimeter of a MyBox2 is:52.0 using Copy Constructor Area of a MyBox3 is:161.4375 Perimeter of a MyBox3 is:52.0
Page No. 3
// Write a Java Program to demonstrate Method Overloading. class Value { int arr[]={1,7,90,3,67}; void max_min(int v) { [Link]("The maximum & minimum Values are same:"+v); } void max_min(int v1,int v2) { if(v1>v2) { [Link]("The Max is"+v1); [Link]("The Min is"+v2); } else { [Link]("The Max is:"+v2); [Link]("The Min is:"+v1); } } void max_min(int v1,int v2,int v3) { if((v1>v2)&&(v1>v3)) { [Link]("The Maximun is:"+v1); } else if(v2>v3) [Link]("The Maximun is :"+v2); else [Link]("The Maximum is :"+v3); if((v1<v2)&&(v1<v3)) [Link]("The Minimum is:"+v1); else if(v2<v3) [Link]("The Minimum is:"+v2); else [Link]("The Minimum is:"+v3); }
Page No. 4
void max_min(int arr[]) { for(int i=0;i<=4;i++) { for(int j=i+1;j<5;j++) { if(arr[i]>arr[j]) { int temp; temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } [Link]("The Minimum is:"+arr[0]); [Link]("The Maximum is:"+arr[4]); } } class Maxmin { public static void main(String args[]) { Value v=new Value(); v.max_min(10); v.max_min(20,10); v.max_min(20,10,30); [Link]("The Max & Min of a given array of Elements is:" ); v.max_min([Link]); } }
Page No. 5
OUTPUT: The The The The The The The The
maximum & minimum Values are same:10 Max is20 Min is10 Maximum is :30 Minimum is:10 Max & Min of a given array of Elements is: Minimum is:1 Maximum is:90
Page No. 6
// Write a Java Program to demonstrate Inheritence. class Person { String name; int age; Person(String si,int age) { name=si; [Link]=age; } } //Person Class class Student extends Person { int rollnumber; Student(String s1,int age,int rollnumber) { super(s1,age); [Link]=rollnumber; } } // Student class class Ugstudent extends Student { int marks; Ugstudent(String s2,int age,int rollnumber,int marks) { super(s2,age,rollnumber); [Link]=marks; } void display() { [Link]("\n Ug Student Details"); [Link]("\n Name:"+name+"\n Age:"+age+"\n Roll Number:"+rollnumber+"\n Marks:"+marks); } } //Ug Class class Pgstudent extends Student { int markspg;
Page No. 7
Pgstudent(String s3,int age,int rollnumber,int markspg) { super(s3,age,rollnumber); [Link]=markspg; } void display() { [Link]("\n Pg Student Details"); [Link]("\n Name"+name+"\n Age:"+age+"\n Rollno:"+rollnumber+"\n Marks PG:"+markspg); } }// Pg Class class Multy { public static void main(String args[]) { Ugstudent ug=new Ugstudent("Hari",23,32,789); [Link](); Pgstudent pg=new Pgstudent("Ram",25,47,456); [Link](); } } //Multi Class
Page No. 8
OUTPUT: Ug Student Details Name:Hari Age:23 Roll Number:3 Marks:789 Pg Student Details NameRam Age:25 Rollno:47 Marks PG:456
Page No. 9
//Write a Java Program to demonstrate Dynamic Polymorphism. class Animal { public void eat() { [Link]("Every Animals eats to live"); } } // Animal Class class Elephant extends Animal { public void eat() { [Link]("Elephant eats Leaves"); } } // Elephant Class class Lion extends Animal { public void eat() { [Link]("Lion eats Flesh"); } }// Lion Class class DynamicDispatch { public static void main(String args[]) { Animal a1=new Animal(); [Link](); Elephant e1=new Elephant(); a1=e1; [Link](); Lion l1=new Lion(); a1=l1; [Link](); } } //DynamicDispatch Class
Page No. 10
OUTPUT: Every Animals eats to live Elephant eats Leaves Lion eats Flesh
Page No. 11
//Program to implement the following Hierarchy and find area abstract class Shape { double a,b; final double PI=3.14156; Shape(double a) { this.a=a; } Shape(double a,double b) { this.a=a; this.b=b; } abstract double area(); } class Square extends Shape { Square(double a) { super(a); } double area() { return a*a; } } class Triangle extends Shape { Triangle(double a,double b) { super(a,b); } double area() { return(a*b)/2; } } class Circle extends Shape
Page No. 12
{ Circle(double a) { super(a); } double area() { return PI*a*a; } } class HierarchyDemo { public static void main(String args[]) { Square s=new Square(10.5); [Link]("the area of square is:"+[Link]()); Triangle t=new Triangle(20.25,30.15); [Link]("the area of triangle is:"+[Link]()); Circle c=new Circle(15.75); [Link]("the area of circle is:"+[Link]()); } }
Page No. 13
OUTPUT: The area of square is:110.25 The area of triangle is:305.26875 The Area of circle is:779.3032275
Page No. 14
/*Write a Java program to implement an Animal Abstract class.*/ abstract class Animal { public void eat() { [Link]("Om nom nom, food is delicious!"); } public abstract void speak(); } class Cat extends Animal { public void speak() { [Link]("Meow!"); } } class Dog extends Animal { public void speak() { [Link]("Bark! Bark!"); } } class AbstractDemo { public static void main(String args[]) { Animal a= new Cat(); [Link](); Animal a1 = new Dog(); [Link](); } }
Page No. 15
OUTPUT: Meow! Bark! Bark!
Page No. 16
//Program on multithreading by using runnable interface class Critical { public void m1() { [Link]("entered m1"); try { [Link](2000); } catch(InterruptedException ie) { [Link](); } [Link]("exit m1"); } public void m2() { [Link]("entered m2"); try { [Link](2000); } catch(InterruptedException ie) { [Link](); } [Link]("exit m2"); } } class Runnable1 implements Runnable { Critical c; Runnable1(Critical c1) { c=c1; } public void run() {
Page No. 17
c.m1(); } } class Runnable2 implements Runnable { Critical c; Runnable2(Critical c1) { c=c1; } public void run() { c.m2(); } } class TestCritical { public static void main(String args[]) { Critical c1=new Critical(); Runnable1 r1=new Runnable1(c1); Runnable2 r2=new Runnable2(c1); Thread t1=new Thread(r1); Thread t2=new Thread(r2); [Link](); [Link](); } }
Page No. 18
OUTPUT:entered m1 entered m2 exit m1 exit m2
Page No. 19
//Write a Java program on multithreading by using the thread class. class NewThread extends Thread { NewThread() { super("Demo Thread"); [Link]("Child thread: " + this); start(); } public void run() { try { for(int i = 5; i > 0; i--) { [Link]("Child Thread: " + i); [Link](500); } } catch (InterruptedException e) { [Link]("Child interrupted."); } [Link]("Exiting child thread."); } } class ExtendThread { public static void main(String args[]) { new NewThread(); try { for(int i = 5; i > 0; i--) { [Link]("Main Thread: " + i); [Link](1000);
Page No. 20
} } catch (InterruptedException e) { [Link]("Main thread interrupted."); } [Link]("Main thread exiting."); } }
Page No. 21
OUTPUT: Child thread: Thread[Demo Thread,5,main] Main Thread: 5 Child Thread: 5 Child Thread: 4 Child Thread: 3 Main Thread: 4 Child Thread: 2 Main Thread: 3 Child Thread: 1 Exiting child thread. Main Thread: 2 Main Thread: 1 Main thread exiting.
Page No. 22
/*Write a Java program to demonstrate the concept of synchronization by using Bank Account class.*/ public class { Deposit static int bal=1000; public static void main(String[] args) { Account ac=new Account(); DepositThread first,second; first=new DepositThread(ac,1000,"#1"); second=new DepositThread(ac,1000,"#2"); [Link](); [Link](); try{ [Link](); [Link](); } catch(InterruptedException e) { } [Link]("final balance"+bal); } } class Account { synchronized void deposit(int amount,String name) { int bal; [Link](name+"trying to deposite"+amount); [Link](name+"getting balance"); bal=getBalance(); [Link](name+"got balance"+bal);
Page No. 23
bal+=amount; [Link](name+"setting balance"); setBalance(bal); [Link](name+"new balance"+[Link]); } int getBalance() { return [Link];} void setBalance(int bal) { [Link]=bal; } } class DepositThread extends Thread { Account ac; String msg; int depamt; DepositThread(Account ac,int amt,String msg) { [Link]=ac; [Link]=amt; [Link]=msg; } public void run() { [Link](depamt,msg); } }
Page No. 24
OUTPUT: #1trying to deposite1000 #1getting balance #1got balance1000 #1setting balance #1new balance2000 #2trying to deposite1000 #2getting balance #2got balance2000 #2setting balance #2new balance3000 final balance3000
Page No. 25
// Program to implement producers-consumer problem class communicate { public static void main(String[] args)throws Exception { //producer produces some data which consumer consumes producer obj1=new producer(); //pass producer object to consumer so that it is then available to consumer Consumer obj2=new Consumer(obj1); //create 2 threads and attach to producer and consumer Thread t1=new Thread(obj1); Thread t2=new Thread(obj2); //run the threads [Link](); [Link](); } } class producer extends Thread { //to add data,we use string buffer object StringBuffer sb; producer() { sb=new StringBuffer(); } public void run() { synchronized(sb) { //go on appending data (numbers)to string buffer for(int i=1;i<=10;i++) { try{
Page No. 26
[Link](i+":"); [Link](100); [Link]("appending"); }catch(Exception e){} } //data production is over,so notify to consumer thread [Link](); } } } class Consumer extends Thread { //create producer reference to refer to producer object from consuner class producer prod; Consumer(producer prod) { [Link]=prod; } public void run() { synchronized([Link]) { //wait till a notification is recieved from the producer //[Link](); //there is no wastage of time of even a single millisecond try{ [Link](); }catch(Exception e){} //when data production is over ,display data of stringbuffer
Page No. 27
[Link]([Link]); } } }
Page No. 28
OUTPUT: c:\>java Communicate appending appending appending appending appending appending appending appending appending appending 1:2:3:4:5:6:7:8:9:10:
Page No. 29
// Program to Demonstrate String Tokenizer import [Link].*; public class StringTokenizerDemo { public static void main(String args[]) { String str1="hello world; how,do you/do, all are/ok; thank/you"; StringTokenizer st=new StringTokenizer(str1,",;/"); [Link]("Number of tokens:"+[Link]()); while([Link]()) { [Link]([Link]()); } } }
Page No. 30
OUTPUT: Number of tokens: 8 hello world how do you do all are ok thank you
Page No. 31
/* Java class for matrix operations such as read,write,add,sub and multiply */ import [Link].*; public class MyMatrix { int array1[ ][ ]=new int[2][3]; int array2[ ][ ]={{10,20,30},{40,50,60}}; int array3[ ][ ]=new int [2][3]; public void readMatrix() throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader([Link])); for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { [Link]("enter an integer number"); String str=[Link](); int num=[Link](str); array1[i][j]=num; } } [Link](); } public void writeMatrix() { for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { [Link](array1[i][j]+"\t"); } [Link](); } } public void addMatrix() { for(int i=0;i<2;i++)
Page No. 32
{ for(int j=0;j<3;j++) { array3[i][j]=array1[i][j]+array2[i][j]; [Link](array3[i][j]+"\t"); } [Link](); } } public void subMatrix() { for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { array3[i][j]=array1[i][j]array2[i][j]; [Link](array3[i][j]+"\t"); } [Link](); } } public void multiplyMatrix() { for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { array3[i][j]=array1[i][j]*array2[i][j]; [Link](array3[i][j]+"\t"); } [Link](); } }
Page No. 33
public static void main(String args[]) throws Exception { MyMatrix mm=new MyMatrix(); [Link](); [Link]("result of write matrix"); [Link](); [Link]("result of addition matrix"); [Link](); [Link]("result of subtraction matrix"); [Link](); [Link]("result of multiplication matrix"); [Link](); } }
Page No. 34
OUTPUT: enter an integer number 1 enter an integer number 2 enter an integer number 3 enter an integer number 4 enter an integer number 5 enter an integer number 6 result of write matrix 1 2 3 4 5 6 result of addition matrix 11 22 33 44 55 66 result of subtraction matrix -9 -18 -27 -36 -45 -54 result of multiplication matrix 10 40 90 160 250 360
Page No. 35
// java program to illustrate linkedlist using list interface import [Link].*; public class LinkedListDemo { public static void main(String args[]) { LinkedList LL=new LinkedList(); Integer i1=new Integer(5); [Link]("5"); [Link](new Double(3.5)); [Link]("hello"); [Link](new Boolean(true)); [Link](0,new Float(2.3f)); [Link](new Character('A')); [Link](new Short("5")); [Link](i1); [Link](new Byte("3")); [Link](2,"hari"); [Link](1); [Link](); [Link](); [Link]("index of il elements:"+[Link](i1)); [Link]("Last of il elements:"+ [Link](i1)); [Link]("object of index 1:"+[Link](1)); [Link]("first elements:"+[Link]()); [Link]("last elements:"+[Link]()); [Link]("size of LinkedList:"+[Link]()); [Link]("list is empty:"+[Link]()); [Link]("printing the elements through an iterator"); ListIterator L1=[Link](); while([Link]()) { [Link]([Link]()); }
Page No. 36
} }
Page No. 37
OUTPUT: index of il elements:5 Last of il elements:5 objects of index 1:3.5 first elements:hari last elements:5 size of LinkedList:6 list is empty:false printing the elements through an iterator hari 3.5 hello true A 5
Page No. 38
/*Java program for implementation of stack operations like push and pop*/ import [Link].*; public class StackDemo { public static void main(String[] args) { Stack st=new Stack(); [Link]([Link]()); [Link](new Integer(5)); [Link](new Boolean(true)); [Link](new Float(59.7f)); [Link](new Double(100.5)); Iterator it=[Link](); while([Link]()) { Object obj=[Link](); [Link](obj); } [Link]("poping the element is: "+[Link]()); [Link]("stack is empty before while loop: "+[Link]()); while(![Link]()) { [Link]([Link]()); } [Link]("stack is empty after while loop: "+[Link]()); } }
Page No. 39
OUTPUT: true poping the element is: 100.5 stack is empty before while loop: false 59.7 true 5 stack is empty after while loop: true
Page No. 40
//Java program to implement TreeSet class import [Link].*; public class TreeSetInfo { public static void main(String args[]) { TreeSet ts=new TreeSet(); [Link]("Hemanth"); [Link]("Shekar"); [Link]("Manoj"); [Link]("Number of elements present:"+[Link]()); [Link](ts); } }
Page No. 41
OUTPUT:Number of elements present:3 [Hemanth,Shekar,Manoj]
Page No. 42
/* Write a java program to demonstrate methods of Tree Set class*/ import [Link].*; class TreeSetDemo { public static void main(String args[]) { TreeSet ts= new TreeSet(); [Link]("Anu"); [Link]("Benny"); [Link]("Som"); [Link]("Sonu"); [Link]("Kevin"); [Link]("Christy"); [Link]("Elements of treeset:"+ts); [Link]("Size of treeset:"+[Link]()); [Link]("Kevin"); [Link]("Elements of treeset:"+ts); [Link]("First element:"+[Link]()); [Link]("Last element:"+[Link]()); [Link]("Solly is an element of treeset: "+[Link]("Solly")); [Link](); [Link]("Elements of treeset:"+ts); } }
Page No. 43
OUTPUT: Elements of treeset:[Anu, Benny, Christy, Kevin, Som, Sonu] Size of treeset:6 Elements of treeset:[Anu, Benny, Christy, Som, Sonu] First element:Anu Last element:Sonu Solly is an element of treeset: false Elements of treeset:[]
Page No. 44
//Write a java program to implement Hash Set Class import [Link].*; public class HashSetInfo { public static void main(String args[]) { HashSet hs=new HashSet(); Integer i1=new Integer(10); [Link](i1); Double d1=new Double(10.5); [Link](d1); [Link](new Integer(20)); [Link]("Sandeep"); [Link](new String("author")); [Link]("number of elements:"+[Link]()); [Link]("i1 exists:"+[Link](i1)); [Link]("elements before removed"); [Link](hs); [Link](hs); [Link](hs); } }
Page No. 45
Output: numberofelements:5 i1 exists:true elements before removed [Sandeep,10.5,20,10,author] [Sandeep,10.5,20,10,author]
Page No. 46
/*Create student class with particulars like name, rno, marks etc and print them in ascending order using iterator (list iterator).*/ import [Link].*; class Student { String name; int rno; int marks; Student(String name,int rno,int marks) { [Link]= name; [Link]= rno; [Link]= marks; } public String toString() { return "Name "+name+" RollNo "+rno+" Marks "+marks; } } class IteratorDemo { public static void main(String args[]) { LinkedHashSet hs= new LinkedHashSet(); [Link](new Student("Christy",333,445)); [Link](new Student("Johaan",111,480)); [Link](new Student("Justin",222,456)); [Link](new Student("Sonu",444,467)); Iterator i= [Link](); while([Link]()) { [Link]([Link]()); } } }
Page No. 47
OUTPUT: Name Name Name Name
Christy RollNo 333 Marks 445 Johaan RollNo 111 Marks 480 Justin RollNo 222 Marks 456 Sonu RollNo 444 Marks 467
Page No. 48
//Write Java program by using Tree Map Class. import [Link]; import [Link]; import [Link]; import [Link]; public class MarksMap { public static void main(String args[]) { String subjects[]={"English","Maths","Science","Social","Drawin g"}; double marks[]={40,50,60,70,80}; TreeMap tm = new TreeMap(); for(int i=0;i<[Link];i++) { [Link](subjects[i],new Double(marks[i])); } Set keys=[Link](); Iterator it=[Link](); while ([Link]()) { Object obj=[Link](); [Link](obj+""+[Link](obj)); } } }
Page No. 49
OUTPUT: Drawing80.0 English40.0 Maths50.0 Science60.0 Social70.0
Page No. 50
//Write a Java program to illustrate the methods of vector import [Link].*; public class VectorTest { public static void main(String[] args) { Vector vect = new Vector(); [Link](new Integer(5)); [Link](new Float(5.7F)); [Link](new String("Hello")); [Link]("Sure"); Double d=new Double(15.76); [Link](d); String str="World"; [Link](str,1); [Link]("First element:"+[Link]()); [Link]("Last element:"+[Link]()); [Link]("4th element:"+[Link](3)); [Link]("Index of Hello:"+[Link]("Hello")); [Link]("Element Sure exists:"+[Link]("Sure")); [Link]("Size of vector:"+[Link]()); [Link]("Capacity of vector before trimming:"+[Link]()); [Link](); [Link]("Capacity of vector after trimming:"+[Link]()); [Link](vect); Enumeration e=[Link](); while ([Link]()) { [Link]([Link]()); }
Page No. 51
String s=(String)[Link](1); [Link]("Str is "+s); } }
Page No. 52
OUTPUT: First element:5 Last element:15.76 4th element:Hello Index of Hello:3 Element Sure exists:true Size of vector:6 Capacity of vector before trimming:10 Capacity of vector after trimming:6 [5, World, 5.7, Hello, Sure, 15.76] 5 World 5.7 Hello Sure 15.76 Str is World
Page No. 53
// Program to Illustrate the Comparator Interface import [Link].*; class Animal implements Comparator<String> { public int compare(String str1,String str2) { return [Link](str1); } }; public class ComparatorDemo { public static void main(String args[]) { TreeSet<String>ts1=new TreeSet<String>(); TreeSet<String>ts2=new TreeSet<String>(new Animal()); [Link]("camel"); [Link]("zebra"); [Link]("rabbit"); [Link]("allegator"); [Link]("tortise"); [Link]("zebra"); [Link]("camel"); [Link]("rabbit"); [Link]("allegator"); [Link]("tortise"); [Link]("Default value"); [Link]("\t"+ts1); [Link]("value with comparator:"); [Link]("\t"+ts2); } }
Page No. 54
OUTPUT: Default value [allegator, camel, rabbit, tortise, zebra] value with comparator: [zebra, tortise, rabbit, camel, allegator]
Page No. 55
/* Write a Java program to print table using Buffered Reader and Buffered writer */ import [Link].*; class Table { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader([Link])); [Link]("Enter a number for multiplication table"); int n = [Link]([Link]()); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter([Link])); for(int i=1;i<=10;i++) { int t= i*n; String s=((Integer)i).toString()+"*"+((Integer)n).toString()+"= "+((Integer)t).toString()+"\n"; [Link](s,0,[Link]()); } [Link](); [Link](); [Link](); } }
Page No. 56
OUTPUT: Enter a number for multiplication table 7 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63 7*10=70
Page No. 57
// Program to read and write files import [Link].*; class FileDemo { public static void main(String args[]) throws IOException { BufferedReader br1=new BufferedReader(new InputStreamReader([Link])); [Link]("Enter the file name"); String fname=[Link](); FileOutputStream fo=new FileOutputStream(fname); BufferedReader br2=new BufferedReader(new InputStreamReader([Link])); String s; [Link]("Enter the lint of Text here"); [Link](); s=[Link](); for(int i=0;i<[Link]();i++) [Link]([Link](i)); [Link](); FileInputStream fi=new FileInputStream(fname); int read; [Link]("\n Content in the"+fname); while((read=[Link]())!=-1) [Link]((char)read); [Link](); } }
Page No. 58
OUTPUT: Enter the file name NewText Enter the lint of Text here Hi Content in the NewText Hi
Page No. 59
//Write a Java program to illustrate serialization. import [Link].*; public class SerializationDemo { public static void main(String args[]) { try { MyClass ob1=new MyClass("hello",-7,2.7e10); [Link]("object1"+ob1); FileOutputStream fos=new FileOutputStream("serial"); ObjectOutputStream oos=new ObjectOutputStream(fos); [Link](ob1); [Link](); [Link](); } catch(Exception e) { [Link]("Exception during serialization"+e); [Link](0); } try { MyClass ob2; FileInputStream fis=new FileInputStream("serial"); ObjectInputStream ois=new ObjectInputStream(fis); ob2=(MyClass)[Link](); [Link](); [Link]("object2"+ob2); } catch(Exception e) {
Page No. 60
[Link]("Exception during deserialization"+e); [Link](0); } } } class MyClass implements Serializable { String s; int i; double d; public MyClass(String s,int i,double d) {this.s=s; this.i=i; this.d=d; } public String toString() { return "s="+s+";i="+";d="+d; } }
Page No. 61
OUTPUT: object1s=hello;i=;d=2.7E10 object2s=hello;i=;d=2.7E10
Page No. 62
/* Write a Java program which count the number of customers in the bank(use static variable)*/ class Customer { static int count; String name; int acno,bal; Customer(String name,int acno,int bal) { [Link]=name; [Link]=acno; [Link]=bal; } void display() { count++; [Link](name+" "+acno+" "+bal); } public static void main(String args[]) { Customer c1= new Customer("Kevin",111,20000); Customer c2= new Customer("Johan",222,15000); Customer c3= new Customer("Jusin",333,18000); Customer c4= new Customer("Relin",444,25000); [Link]("Customer details\n"); [Link]("Name "+"Account No "+"Balance "); [Link](); [Link](); [Link]();
Page No. 63
[Link](); [Link]("Total customer:"+[Link]); } }
Page No. 64
OUTPUT: Customer details Name Account No Kevin 111 Johan 222 Jusin 333 Relin 444 Total customer:4 Balance 20000 15000 18000 25000
Page No. 65
// Write a Java applet to implement a simple calculator. package javaapplication2; import [Link].*; import [Link].*; public class calculator 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("CA"); 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);
Page No. 66
strnum = ""; num = 0; } } public void init() { setLayout(null); for(int i = 0;i <= 9; i ++) { button[i] = new Button([Link](i)); button[i].setBackground([Link]); 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);
Page No. 67
[Link](0,264,67,53); add(mult); 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);
Page No. 68
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;
Page No. 69
} if ([Link]() == clear) { [Link]("0"); strnumtemp = ""; numtemp = 0; strnum = ""; num = 0; counter = 0; } } }
Page No. 70
OUTPUT:
Page No. 71
//Write a JavaProgram to demonstrate banner applet import [Link].*; import [Link].*; public class SampleBanner extends Applet implements Runnable { String str = "This is a simple Banner developed by [Link]. "; Thread t ; boolean b; public void init() { setLayout(null); setBackground([Link]); setForeground([Link]); } public void start() { t = new Thread(this); b = false; [Link](); } public void run () { char ch; for( ; ; ) { try { repaint(); [Link](250); ch = [Link](0); str = [Link](1, [Link]()); str = str + ch; } catch(InterruptedException e)
Page No. 72
{ } } } public void paint(Graphics g) { [Link](1,1,300,150); [Link]([Link]); [Link](1,1,300,150); [Link]([Link]); [Link](str, 1, 150); } }
Page No. 73
OUTPUT:
Page No. 74
/* Write a Java program validate user name and password text fields.*/ import [Link].*; import [Link].*; public class PassDemo extends Frame implements ActionListener { TextField usr,passwd; Button b; String ur="sophia",pw="jonu"; Label l,user,pass; public PassDemo() { user= new Label("User Name "); usr= new TextField(15); pass= new Label("Password "); passwd= new TextField(15); [Link]('*'); add(user); add(pass); l= new Label(" "); b= new Button("Enter"); setLayout(new FlowLayout()); add(user); add(usr); add(pass); add(passwd); add(b); add(l); [Link](this); setVisible(true); setBackground([Link]); setSize(250,250); } public void actionPerformed(ActionEvent ae) { String u,p;
Page No. 75
u=[Link](); p=[Link](); if ([Link](ur) & [Link](pw)) [Link]("You are a valid user"); else [Link]("You are not a valid user"); } public static void main(String args[]) { new PassDemo(); } }
Page No. 76
OUTPUT:
Page No. 77
/* Write a Java program to demonstrate an application involving GUI with controls menus and event handling.*/ import [Link].*; import [Link].*; import [Link].*; class MenuFrame extends JFrame implements ActionListener { JMenuBar mb; JMenu fileMenu,editMenu; JLabel response; FileDialog fd; static MenuFrame mf; JButton b; public MenuFrame() { fileMenu= new JMenu("File"); editMenu= new JMenu("Edit"); response= new JLabel("Menu Tester...."); [Link](200,100,250,50); setSize(600,300); setLocation(450,100); Container contentPane= getContentPane(); [Link](new FlowLayout()); [Link](response); [Link]([Link]); [Link]([Link]); b= new JButton("OK"); [Link](b); [Link](this); JMenuItem item; item= new JMenuItem("New"); [Link](this); [Link](item); item= new JMenuItem("Open");
Page No. 78
[Link](this); [Link](item); item= new JMenuItem("Save"); [Link](this); [Link](item); item= new JMenuItem("Exit"); [Link](this); [Link](item); item= new JMenuItem("Cut"); [Link](this); [Link](item); item= new JMenuItem("Copy"); [Link](this); [Link](item); item= new JMenuItem("Paste"); [Link](this); [Link](item); mb = new JMenuBar(); setJMenuBar(mb); [Link](fileMenu); [Link](editMenu); addMouseListener(new MyAdapter()); setDefaultCloseOperation(EXIT_ON_CLOSE); setTitle("Menu & Mouse events"); } public static void main(String args[]) { mf= new MenuFrame(); [Link](true); } class MyAdapter extends MouseAdapter { public void mouseClicked(MouseEvent me) { int x=[Link]();
Page No. 79
int y= [Link](); [Link]("You have clicked at ("+x+","+y+")"); } } public void actionPerformed(ActionEvent ae) { String menuName; menuName=[Link](); if([Link]("Exit")) [Link](0); else if([Link]("Save")) { fd = new FileDialog(mf,"File Dialog",[Link]); [Link](true); } else if([Link]("Open")) { fd = new FileDialog(mf,"File Dialog",[Link]); [Link](true); String r1=[Link]();[Link](r1); } else if([Link]("OK")) [Link]("You have clicked OK button"); else [Link]("You have selected "+menuName); } }
Page No. 80
OUTPUT:
Page No. 81