You are on page 1of 34

INDEX

S.No .
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25.

PROGRAMS
WAP To Print Hello WAP To Print Numeric Series WAP To Print 3x3 Matrix WAP To Implement Switch Case WAP To Implement Scanners WAP To Implement Arguments WAP To Implement Constructor WAP To Implement Parameter Constructor WAP To Implement Overload WAP To Implement Call Value WAP To Implement Call Refer WAP To Implement Abstract WAP To Implement Inheritance WAP To Implement interface call back WAP To Implement interface extend WAP To Implement interface stack WAP To Implement access inheritance WAP To Implement stack interface WAP To Implement package WAP To Implement my thread WAP To Implement multi catch WAP To Implement throw exception WAP To Implement simple applet WAP To Implement frame WAP To Implement panel

SIGNATURE

PROGRAM:-1 Write a program to print HELLO. class demo1 { public static void main(String... s) { System.out.print("Hello java"); } }

Output: Hello java

PROGRAM-2 Write a program to print:1 23 456

class series { public static void main(int Arr[]) { int a=1; int b,c; for(b=1;b<=b;b++) { for(c=1;c<=b;c++) { System.out.println(a); a++; } System.out.println(); } } }

Output : 1 2 4 3 5 6

PROGRAM-3 Write a program to print a matrix of order 3*3. class ArrayDemo { public static void main(String[] args) { int array[][]= {{4,5,6},{6,8,9},{3,2,1}}; System.out.println("Matrix : "); for(int i = 0;i<array.length ; i++) { for(int j = 0;j<=array.length; j++) { System.out.print(" "+ array[i][j]); } System.out.println(); } } }

Output: 4 5 6 6 8 9 3 2 1

PROGRAM-4 Write a program to implement SWITCH CASE. public class SwitchDemo { public static void main(String[] args) { int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August";

break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString=+monthstring); } }

Output: August

PROGRAM-5 Write a program to implement SCANNER. import java.util.*; class Scan { Public static void main(String args[]) { System.out.println(enter the name and sirname:); Scnner sc = new Scanner(System.in); String a = sc.next line(); System.out.println(a); } }

Output : Enter the name and sirname : michael jackson

PROGRAM-6 Write a program to implement ARGUMENT. Class Arg { Public static void main(String args[]) { System.out.println(enter following arguments); System.out.println(first argument : +args[0]); System.out.println(second argument :+args[1]); Char a=args[0].charAt[0]; Char b=args[1].charAt[0]; int l=args[0].length(); int m=args[1].length(); System.out.println(a); System.out.println(b); System.out.println(l); System.out.println(m); } }

Output : Java arg xyz abc Enter the following arguments First argument : xyz Second argument : abc X A 3 3

10

Program-7 Write a program to implement CONSTRUCTOR. class Rectangle{ int l, b; float p, q; public Rectangle(int x, int y){ l = x; b = y;} public int first(){ return(l * b);} public Rectangle(int x){ l = x; b = x; } public int second(){ return(l * b); } public Rectangle(float x){ p = x; q = x; } public float third(){ return(p * q); } public Rectangle(float x, float y){ p = x;q = y; } public float fourth(){ return(p * q); } }

11

OUTPUT:The area of a rectangle in first constructor is : 8 The area of a rectangle in first constructor is : 25 The area of a rectangle in first constructor is : 4.0 The area of a rectangle in first constructor is : 6.0

12

PROGRAM-8 Write a program to implement PARAMETERIZED CONSTRUCTOR. Class Box{ double width; double height; double depth; Box(double w, double h, double d){ System.out.println(constructing box); width = w; height =h; dephth=d;} double volume(){ return width*height*depth;}} Class BoxDemo { double vol; public static void main(String args[]) { Box mybox1=new Box(10,20,15); Vol =mybox1.volume(); System.out.println(volume is=+vol); }} Output : Constructing Box Volume is =3000.0

13

PROGRAM-9 Write a program to implement OVERLOADING. public class MethodOverloading { public void add(int a, int b) { System.out.println(a + b); public void add(String x,String y) { System.out.println(x + y); public void add( char c, char d) { System.out.println(c + d); } public static void main(String [] args) { Calculation c = new Calculation(); c.add(7,8); c.add("he", "llo"); c.add('n','k'); } } } }

OUTPUT:a+b=15 x+y=hello c+d=nk

14

PROGRAM-10 Write a program to implement CALL BY VALUE. Class Test { Void meth(int I, int j) { i*=2; j/=2; } } Class call { Public static void main(String args[]) { Test ob =new test(); int a = 15,b=20; System.out.println(a and b before call : +a+ +b); Ob.meth(a,b); System.out.println(a abd b after call : +a+ +b); } }

Output : a and b before call : 15 20 a and b after call : 15 20

15

PROGRAM-11 Write a program to implement CALL BY REFERENCE. import java.io.*; import java.awt.*; public class callByReference { public static void main(String[] args) { System.out.println("This is call by reference example"); Rectangle rec = new Rectangle(20,30,200,50); System.out.println("Before move up massage"); System.out.println("After move up massage display"); moveUp(rec); System.out.println("r1 is now\n" + rec); } static void moveUp(Rectangle rect) { rect.translate(0,-20); } }

16

PROGRAM-12 Write a program to implement ABSTRACT CLASS. abstract class Shape { abstract public double area(); } public class AbstractClassExample extends Shape { static int l, b; public AbstractClassExample() { l = 5; b = 2; } public double area() { return l*b; } public void display() { System.out.println("Length: " + l + ",Breadth: " + b); System.out.println(area()); } public static void main(String args[]) { AbstractClassExample p = new AbstractClassExample(); p.display(); }}

17

PROGRAM-13 Write a program to implement INHERITANCE. class A{ int a; float b; void Show(){ System.out.println("b in super class: " + b); } } class B extends A{ int a; float b; B( int p, float q){ a = p; super.b = q; } void Show(){ super.Show(); System.out.println("b in super class: " + super.b); System.out.println("a in sub class: " + a); } public static void main(String[] args){ B subobj = new B(1, 5); subobj.Show(); } }

Output : b in super class: 5.0 b in super class: 5.0 a in sub class: 1

18

Program : 14 WAP to implement interface call back. public class Main { public interface Visitor { int DoJob(int a, int b); } public static void main(String[] args) { Visitor adder = new Visitor() { public int DoJob(int a, int b) { return a + b; } }; Visitor multiplier = new Visitor(){ public int DoJob(int a, int b) { return a*b; } }; System.out.println(adder.DoJob(10, 20)); System.out.println(multiplier.DoJob(10, 20)); } }

19

Program 15 WAP to implement interface extends. interface Monster { void menace(); } interface DangerousMonster extends Monster { void destroy(); } interface Lethal { void kill(); } class DragonZilla implements DangerousMonster { public void menace() { } public void destroy() { } } interface Vampire extends DangerousMonster, Lethal { void drinkBlood(); } class VeryBadVampire implements Vampire { public void menace() { } public void destroy() { }

20

public void kill() { } public void drinkBlood() { }} public class HorrorShow { static void u(Monster b) { b.menace(); } static void v(DangerousMonster d) { d.menace(); d.destroy(); } static void w(Lethal l) { l.kill(); } public static void main(String[] args) { DangerousMonster barney = new DragonZilla(); u(barney); v(barney); Vampire vlad = new VeryBadVampire(); u(vlad); v(vlad); w(vlad); } }

21

Program : 16 WAP to implement interface stack. interface stack{ void push(int x); int pop();} class StcksizeFix implements stack { private int tos; private int stck[]; StcksizeFix(int size) { stck=new int[size]; tos=-1; } public void push(int item) { if(tos==stck.length-1) System.out.println(The stack has over flow); else stck[++tos]=item; } public int pop() { if(tos==-1) { System.out.println(There is nothing to PoP); return 0; } else return stck[tos--]; } } class TestStack

22

{ public static void main(String args[]) { StcksizeFix Stack1= new StcksizeFix(5); StcksizeFix Stack2= new StcksizeFix(5); System.out.println(Start Push Objects in Stack); for(int i=0;i<5;i++) Stack1.push(2*i); for(int i=0;i<6;i++) Stack2.push(3*i); for(int i=0;i<6;i++) { System.out.println(The +(5-i)+ element in stack 1 is +Stack1.pop()+ +(5-i)+ element in stack 2 is +Stack2.pop()); } } } The

Output: Start Push Objects in Stack The stack has over flow The 5 element in stack 1 is 8 The 4 element in stack 1 is 6 The 3 element in stack 1 is 4 The 2 element in stack 1 is 2 The 1 element in stack 1 is 0 There is nothing to PoP There is nothing to PoP

The 5 element in stack 2 is 12 The 4 element in stack 2 is 9 The 3 element in stack 2 is 6 The 2 element in stack 2 is 3 The 1 element in stack 2 is 0

23

Program : 17 WAP to implement access inheritance. interface Area { float compute(float x, float y); } class Rectangle implements Area { public float compute(float x, float y) { return(x * y); } } class Triangle implements Area { public float compute(float x,float y) { return(x * y/2); } } class InterfaceArea { public static void main(String args[]) { Rectangle rect = new Rectangle(); Triangle tri = new Triangle(); Area area; area = rect; System.out.println("Area Of Rectangle = "+ area.compute(1,2)); area = tri; System.out.println("Area Of Triangle = "+ area.compute(10,2)); } } OUTPUT Area Of Rectangle = 2.0 Area Of Triangle = 10.0

24

Program : 18 WAP to implement stack interface. Class Fixed Stack implements Instack { Private int stck[]; Private int tos; Fixed stack(int size) { Stck = new int [size]; Tos=-1; } Public void push(int item) { If (tos = =stck.length-1) { System.out.println(stack is full); } else { Stck[++tos]=item; } Public int pop() { If( tos<0) { System.out.println(stack underflow); return(0); } else { return stck(tos--); } } } Class if test 25

{ public static void main(String args[]) fixed stack MyStack1=new fixed stack(5); fixed stack MyStack2=new fixed stack(8); for(int i=0,i<5,i++) MyStack1.push(i); for(int i=0,i<8,i++) MyStack2.push(i); Sytem.out.println(stack on MyStack1:); For(i=0;i<5;i++) { System.out.println(MySatck1.pop()); } System.out.println(stack in MyStack2:); for(i=0;i<8;i++) { System.out.println(MyStack2.pop()); } } }

26

Program: 19 WAP to implement package. package pack; public class First { static double amt,sim,comp,princ = 1000,rate = 0.09; static int n=2; public First() { System.out.println("\n...Welcome to First Class\n"); } public static void DisplayData() { System.out.println("Principle Amount\t:"+princ+"\nRate Of Interest\t:"+rate*100+"%"); System.out.println("Term\t\t\t:"+n); } public static void SimpleInterest() { System.out.println("\nDisplaying Simple Interest..."); try { Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second) } catch(InterruptedException e) { e.printStackTrace(); } sim = (princ*n*rate); System.out.println("\tInterest\t: "+sim); } public static void CompInterest() { System.out.println("\nDisplaying Compound Interest..."); try { Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second) } catch(InterruptedException e) {

27

e.printStackTrace(); } double ans = princ*(1+rate/n); comp = Math.pow(ans,(double)n*4.0); System.out.println("\tInterest\t: "+comp); } /* public static void main(String []args) { First f= new First(); f.DisplayData(); f.SimpleInterest(); f.CompInterest(); }*/ } /* The socond file imports the first file and calculates the interst*/ /*Second.java*/ import pack.First; class Second { public static void main(String[] args) { System.out.println("Running Class Second.."); First first = new First(); first.DisplayData(); first.SimpleInterest(); first.CompInterest(); } } Output Running Class Second.. ...Welcome to First Class Principle Amount :1000.0 Rate Of Interest :9.0% Term :2 Displaying Simple Interest... Interest : 180.0 Displaying Compound Interest... Interest : 1.4221006128366082E24

28

Program : 20 WAP to implement My thread. public class MyThread extends Thread { public void run () { int count = 0; while (true) { System.out.println ("Thread 1 alive"); // Print every 0.10sec for 2 seconds try { Thread.sleep (100); } catch (InterruptedException e) {} count++; if (count >= 20) break; } System.out.println ("Thread stopping"); } // run } // class MyThread

29

Program : 21 WAP to implement multi catch. public class Multi_Catch { public static void main (String args[]) { int array[]={20,10,30}; int num1=15,num2=0; int res=0; try { res = num1/num2; System.out.println("The result is" +res); for(int ct =2;ct >=0; ct--) { System.out.println("The value of array are" +array[ct]); } } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Error?. Array is out of Bounds"); } catch (ArithmeticException e) { System.out.println ("Can't be divided by Zero"); } } } output: Can't be divided by Zero

30

Program : 22 WAP to implement throw exeption. class Throw_clause { public static void main (String args [ ]) { try { throw new NullPointerException ( ); } catch (NullPointerException e) { System.out.println ("Invalid reference use"); } } }

output : Invalid refrence use

31

Program : 23 WAP to implement simple applet import java.awt.*; import java.applet.*; public class Checkerboard extends Applet { public void paint(Graphics g) { int row; // Row number, from 0 to 7 int col; // Column number, from 0 to 7 int x,y; // Top-left corner of square for ( row = 0; row < 8; row++ ) { for ( col = 0; col < 8; col++) { x = col * 20; y = row * 20; if ( (row % 2) == (col % 2) ) g.setColor(Color.red); else g.setColor(Color.black); g.fillRect(x, y, 20, 20); } } } } Output :

32

Program:24 WAP to implement frame. import java.awt.*; class MyFrame { Frame f; MyFrame(String s) { Frame f=new Frame(s); f.setSize(500,500); f.setVisible(true); } public static void main(String args[]) { new MyFrame("ff"); } } Output :

33

Program : 25 WAP to implement panel. Import java.swing.*; Public class panel { Public static void main(String args[]) { Jpanel p = new jpanel(panel); JTextField t = new jField(panel field); Jlevel l = new Jlevel(panel level); Jframe f = new Jframe(Swing frame); p.add(l); p.add(t); f.get content panel().add(p); f.set visible(true); f.set size(300,300); } }

34

You might also like