You are on page 1of 6

/*Overriding */

class A
{
public void display()
{
System.out.println("\n\n Method From A Class ");
}
}
class B extends A
{
public void display()
{
System.out.println("\nMethod From B Class ");
}
}
class Pr1
{
public static void main(String [] args)
{
A ref;

A a=new A();
ref=a;
ref.display();

B b=new B();
ref=b;
ref.display();

}
}
------------------------------------------------------------------------------
/*Multiple Inheritance */
class A
{
void dis1()
{
System.out.println("Method Of Class A ");
}
}
class B extends A
{
void dis2()
{
System.out.println("Method Of Class B ");
}
}
class C extends B
{
public void dis3()
{
dis1();
dis2();
System.out.println("Method Of Class C ");
}
}
class Pr2
{
public static void main(String [] args)
{
C c=new C();
c.dis3();
}
}
------------------------------------------------------------------------------
import java.util.*;
class Authenticationfailure extends Exception
{
Authenticationfailure(String msg)
{
super(msg);
}
}
class Pr4
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
try
{
System.out.println("Enter The PassWord ");
String str = in.next();
String s2="infoplanet";
if(str.equals(s2))
System.out.println("PAssWord Valid....");
else
throw new Authenticationfailure("PassWord InValid......");
}
catch(Authenticationfailure e)
{
System.out.println(e);
}
catch(InputMismatchException e)
{
System.out.println("Invalid ");
}
}
}

------------------------------------------------------------------------------
Applet Demostration
/* < APPLET CODE = " ColorApplet " WIDTH=400 HEIGHT =400 >
</APPLET>
*/
import java.awt.*;
import java.applet.*;
public class ColorApplet extends Applet
{
public void paint(Graphics g)
{ g.setColor(new Color(255,0,0));
g.drawString("Color Demonstration", 50,30);

g.drawLine(50,50,300,50);
g.fillRect(50,60,100,100);
g.setColor(Color.YELLOW);
g.fillOval(50,180,100,100);
}
}
------------------------------------------------------------------------------
Following program demonstrate exception handling:

class Test
{
public static void main(String[] args)
{
int a=10,b=5,c=5;
try
{
int x = a/(b-c);
System.out.println("x="+x);
}
catch(ArithmeticException e)
{
System.out.println("Caught an exception");
System.out.println("Divide by zero");
}
int y = a/(b+c);
System.out.println("y="+y);
}
}
------------------------------------------------------------------------------
interface Area
{
void compute();
}
class Shape
{
double dim1, dim2;
Shape(double a, double b)
{
dim1=a;
dim2=b;
}
}

class Rectangle extends Shape implements Area


{
Rectangle(double a, double b)
{
super(a,b);
}
public void compute()
{
System.out.println("Area of Rectangle="+(dim1*dim2));
}
}

class Triangle extends Shape implements Area


{
Triangle(double a, double b)
{
super(a,b);
}
public void compute()
{
System.out.println("Area of Triangle="+(dim1*dim2/2));
}
}
class InterfaceTest
{
public static void main(String [] args)
{
Area ref;
ref = new Rectangle(10,10);
ref.compute();
ref = new Triangle(10,9);
ref.compute();
}
}
------------------------------------------------------------------------------

import java.util.*;
class Test {
public static void main(String[] args)
{
Vector<Integer> v = new Vector<Integer>();
v.add(10);
v.add(30);
v.add(50);
v.add(20);
v.add(40);
v.add(80);
v.add(60);
System.out.println(v);
v.remove(3);

System.out.println("After removing element from 3rd index ");

System.out.println(v); //print Vector

System.out.println("Size of Vector = "+v.size());


}
}
------------------------------------------------------------------------------
Control Loops IN applet

import java.awt.*;

import java.applet.*;

public class ControlLoopApplet extends Applet

public void paint(Graphics g)

for(int i=1;i<=4;i++)

{
if(i%2==0)
{

g.fillOval(90,i*50+10,50,50);

g.setColor(Color.black);

else

g.drawOval(90,i*50+10,50,50);

g.setColor(Color.red);

}
/* <applet code=ControlLoopApplet width=300 height=300>
</applet> */

------------------------------------------------------------------------------
cone,Cylinder

import java.io.*;

import java.awt.*;
;
import java.applet.*;

//<Applet code="Cone" WIDTH="800" HEIGHT="400"></Applet>

public class Cone extends Applet


{
public void paint(Graphics g)

setBackground(Color.yellow);

g.setColor(Color.black);

/* To draw an cone*/

g.drawOval(200,80,200,50);
g.drawLine(200,100,300,500);

g.drawLine(400,100,300,500);

/* To draw a cyclinder*/

g.drawOval(500,60,200,50);

g.drawLine(500,80,500,300);

g.drawLine(700,80,700,300);

g.drawOval(500,280,200,50);
}
}
}

You might also like