Anda di halaman 1dari 25

-------------------------------------PROGRAM 1

Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to implement simple Hello World program. Input
class Hello { public static void main(String[] args) { System.out.println("Hello World"); } }

Output

PROGRAM 2
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to implement sum of numbers using classes and objects. Input
class AddNo { int a,b; public AddNo(int x,int y) { a=x; b=y; } public void add() { System.out.println("Sum of "+a+" and "+b+" = "+(a+b)); } } class SumOfTwo { public static void main(String arg[]) { AddNo a=new AddNo(25,50); a.add(); } }

Output

PROGRAM 3
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to implement method overloading. Input
class OverLoad { public int add(int a,int b) { return a+b; } public String add(String a,String b) { return (a+" "+b); } } class Test { public static void main(String a[]) { OverLoad o=new OverLoad(); int sum=o.add(50,70); String concat=o.add("Hi","Gaurav"); System.out.println("sum of 50 and 70 is "+sum); System.out.println("concatenation of Hi and Gaurav is "+concat); } }

Output

PROGRAM 4
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to implement method overriding. Input
class Vehicle { int speed; public Vehicle(int s) { speed=s; } public int speed() { speed=speed+10; return speed; } } class Car extends Vehicle { public Car(int s) { super(s); } public int speed() { speed=speed+40; return speed; } } class Bus extends Vehicle { public Bus(int s) { super(s); } } class TestOverride { public static void main(String[] args) { Bus a=new Bus(80); System.out.println("speed of bus is"+a.speed()); Car b=new Car(80); System.out.println("speed of car is"+b.speed()); } }

Output

PROGRAM 5
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to implement inheritance. Input
class Vehicle { int speed; public Vehicle() { speed=0; } int start() { speed=10; return speed; } } class Bike extends Vehicle { String color; public Bike(String color) { this.color=color; } int up() { speed+=40; return speed; } int down() { speed-=30; return speed; } } class TestInheritance { public static void main(String[] args) { Bike b=new Bike("RED"); System.out.println("color of bike is "+b.color); System.out.println("bike is at rest with speed "+b.speed+"km/h"); System.out.println("bike starts with speed "+b.start()+"km/h"); System.out.println("speed of bike after speed up "+b.up()+"km/h"); System.out.println("speed of bike after speed down "+b.down()+"km/h"); } }

Output

PROGRAM 6
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to illustrate the concept of abstract classes. Input
abstract class Shape { abstract int calculateArea(int a); } class Square extends Shape { int side; public Square(int a) { side=a; } int calculateArea(int a) { return(a*a); } } class TestAbstract { public static void main(String[] args) { Square s=new Square(5); System.out.println("area of square is "+s.calculateArea(s.side)); } }

Output

PROGRAM 7
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to demonstrate the concept of interfaces. Input
interface Shape { abstract int area(int a); abstract int circum(int a); } class Square implements Shape { int side; public Square(int a) { side=a; } public int area(int a) { return(a*a); } public int circum(int a) { return(4*a); } } class TestInterface { public static void main(String[] args) { Square s=new Square(25); System.out.println("area of square is "+s.area(s.side)); System.out.println("circumference of square is "+s.circum(s.side)); } }

Output

PROGRAM 8
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to demonstrate the concept of packages. Input
package package1; class Student { int sid; public Student(int id) { sid=id; } void showId() { System.out.println("id of student is "+sid); } } class Teacher { int tid; public Teacher(int id) { tid=id; } void showId() { System.out.println("id of teacher is "+tid); } } class TestPackage { public static void main(String[] args) { new Student(5).showId(); new Teacher(3).showId(); } }

Output

PROGRAM 9
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to demonstrateexception handling. Input
class Except { int a=5,b=0; void divide() { try { int c=a/b; } catch(ArithmeticException e) { System.out.println("divide by zero: "); } } } class TestException { public static void main(String[] args) { Except a=new Except(); a.divide(); } }

Output

PROGRAM 10
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to use multiple catch statements. Input
class MultiCatch { public static void main(String[] args) { try { int a=a=args.length; System.out.println("a= "+a); int b=42/a; int[] c={1}; c[42]=99; } catch(ArithmeticException e) { System.out.println("divide by zero: "); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array Index out of bounds: "); } } }

Output

PROGRAM 11
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to demonstrate the concept of finally. Input
class Except2 { int a=5,b=0; void divide() { try { int c=a/b; } catch(ArithmeticException e) { System.out.println("divide by zero exception: "); } finally { System.out.println(a); } System.out.println("after catch statement"); } } class FinallyDemo { public static void main(String[] args) { Except2 a=new Except2(); a.divide(); } }

Output

PROGRAM 12
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program for creating your own exception and demonstrate to show throw and throws. Input
class MyException extends Exception { private int detail; MyException(int a) { detail=a; } public String toString() { return "MyException ["+detail+"]"; } } class ExceptionDemo { static void compute(int a) throws MyException { System.out.println("Called Compute("+a+")"); if(a>10) throw new MyException(a); System.out.println("Normal Exit"); } public static void main(String args[]) { try { compute(1); compute(20); } catch(MyException e) { System.out.println("Exception"); } } }

Output

PROGRAM 13
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to illustrate concept of threads by:a. Inherit Thread class Input
public class ExtendThread { public static void main(String[] args) { new NewThread1(); try { for (int i = 5; i > 0; i--) { System.out.println("Main Thread: "+i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main Thread Interrupted"); } System.out.println("Main Thread exiting"); } } public class NewThread1 extends Thread{ public NewThread1() { super("Demo Thread"); System.out.println("Child Thread: "+this); start(); } public void run(){ try { for(int i=5;i>0;i--){ System.out.println("Child Thread: "+i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child Interrupted"); } System.out.println("Exiting child Thread"); } }

b. Implement Runnable interface.


public class ThreadDemo { public static void main(String[] args) { new NewThread(); try{ for(int i=5;i>0;i--){ System.out.println("Main Thread= "+i); Thread.sleep(1000); } } catch(InterruptedException e){ System.out.println("MainThread interrupted"); } System.out.println("Main Thread exiting"); } } public class NewThread implements Runnable { Thread t; public NewThread() { t=new Thread(this, "Demo Thread"); System.out.println("Child Thread"); t.start(); } public void run() { try{ for(int i=5;i>0;i--){ System.out.println("Child Thread: "+i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child Interrupted"); } System.out.println("Exiting child Thread"); } }

Output a. Inherit Thread class

b. Implement Runnable interface.

PROGRAM 14
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program for showing different AWT controls like buttons, checkbox, listbox, textbox, choice, scrollbar, labels. Input
import java.awt.Button; import java.awt.Checkbox; import java.awt.Choice; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Label; import java.awt.List; import java.awt.Scrollbar; import java.awt.TextField; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class AwtExample { Button b; Label l; TextField t; Scrollbar s; Checkbox c; List li; Choice co; public AwtExample() { b=new Button("Button"); l=new Label("Label"); t=new TextField("TextField",20); s=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,50); c=new Checkbox(); li=new List(); co=new Choice(); Frame f = new Frame("AWTExample"); f.setLayout(new GridLayout(4, 3)); f.add(l); f.add(t); f.add(c); f.add(b); f.add(co); f.add(li); f.add(s); f.setSize(500, 500); f.setVisible(true); f.addWindowListener(new WindowAdapter() {

@Override public void windowClosing(WindowEvent arg0) { System.exit(0); } }); } public static void main(String[] args) { AwtExample a=new AwtExample(); } }

Output

PROGRAM 15
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write a program to show following types of layout manager a. FlowLayout b. BorderLayout c. GridLayout Input
import java.awt.BorderLayout; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Label; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class LayoutExample { public LayoutExample() { Frame f1 = new Frame("FlowLayout"); f1.setLayout(new FlowLayout()); Label l1 = new Label("FlowLayout"); Button b1 = new Button("1"); Button b2 = new Button("2"); f1.add(l1); f1.add(b1); f1.add(b2); f1.setSize(250, 100); f1.setVisible(true); Frame f2 = new Frame("BorderLayout"); f2.setLayout(new BorderLayout()); Label l2 = new Label("BorderLayout"); Button b3 = new Button("3"); Button b4 = new Button("4"); Button b5 = new Button("5"); Button b6 = new Button("6"); f1.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.exit(0); } }); f2.add(l2, BorderLayout.CENTER); f2.add(b3, BorderLayout.NORTH); f2.add(b4, BorderLayout.SOUTH); f2.add(b5, BorderLayout.EAST);

f2.add(b6, BorderLayout.WEST); f2.setSize(250, 100); f2.setVisible(true); f2.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.exit(0); } }); Frame f3 = new Frame("GridLayout"); Label l3 = new Label("GridLayout"); Button b7 = new Button("7"); Button b8 = new Button("8"); Button b9 = new Button("9"); f3.setLayout(new GridLayout(2, 2)); f3.add(l3); f3.add(b7); f3.add(b8); f3.add(b9); f3.setSize(250, 100); f3.setVisible(true); f3.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.exit(0); } }); } public static void main(String[] args) { LayoutExample l=new LayoutExample(); } }

Output a. FlowLayout

b. BorderLayout

c.GridLayout

PROGRAM 16
Name : Gaurav Kumar Roll No.:1109110035 Objective: Write an applet program to draw a. Rectangle b. Circle Input
import java.applet.Applet; import java.awt.Graphics; /* <applet code="CirRec" width=700 height=500> </applet> */ public class CirRec extends Applet{ public void paint(Graphics g) { g.drawOval(10, 10, 50, 50); g.drawRect(100, 20, 60, 50); } }

Output

Anda mungkin juga menyukai