[Type text]
[Type text]
Multiple Constructors
Source Code:
import [Link].*;
class Student
String name;
int regno;
int marks1,marks2,marks3;
Student()
name="raju";
regno=12345;
marks1=56;
marks2=47;
marks3=78;
Student(String n,int r,int m1,int m2,int m3)
name=n;
regno=r;
marks1=m1;
marks2=m2;
marks3=m3;
Student(Student s)
{
[Type text]
name=[Link];
regno=[Link];
marks1=s.marks1;
marks2=s.marks2;
marks3=s.marks3;
void display()
[Link](name+"\t"+regno+"\t"+marks1+"\t"+marks2+"\t"+marks3);
class Studentdemo
public static void main(String args[])
Student s1 = new Student();
Student s2 = new Student("john",34266,58,96,84);
Student s3 = new Student(s1);
[Link]();
[Link]();
[Link]();
}
[Type text]
[Type text]
Inheritance:
Source Code:
import [Link].*;
class Name
String name="Swathi";
int age=20;
class Mark extends Name
int m1=30,m2=30,m3=30;
class Student extends Mark
int total;
void calc()
total=m1+m2+m3;
void show()
[Link]("\nNAME:"+name+"\nAGE:"+age+"\nMARK1="+m1+"\nMARK2="+m
2+"\nMARK3="+m3+"\nTOTAL="+total);
class MultilevelInheritance
[Type text]
public static void main(String args[])
Student ob =new Student();
[Link]();
[Link]();
}
[Type text]
[Type text]
Overriding Methods:
Source Code:
import [Link].*;
class Super
int x;
Super(int x)
this.x=x;
void display()
[Link]("super x="+x);
class Sub extends Super
int y;
Sub(int x,int y)
super(x);
this.y=y;
void display()
{
[Type text]
[Link]("super x="+x);
[Link]("sub y="+y);
class OverrideTest
public static void main(String args[])
Sub s1=new Sub(100,200);
[Link]();
}
[Type text]
[Type text]
One Dimensional Arrays:
Source Code:
import [Link].*;
class NumberSorting
public static void main(String args[])
int number[]={55,20,40,60,80,75,65,15,71,93};
int n = [Link];
[Link]("Given list:");
for(int i=0;i<n;i++)
[Link](" "+number[i]);
[Link]("/n");
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(number[i]<number[j])
int temp=number[i];
number[i]=number[j];
number[j]=temp;
[Type text]
[Link]("Sorted List:");
for(int i=0;i<n;i++)
[Link](" "+number[i]);
[Link](" ");
}
[Type text]
[Type text]
Two Dimensional Array
Source Code:
import [Link];
class AddMatrix
public static void main(String args[])
int row,col,i,j;
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows");
row = [Link]();
[Link]("Enter the no of columns");
col=[Link]();
int mat1[][]=new int[row][col];
int mat2[][]=new int[row][col];
int res[][]=new int[row][col];
[Link]("Enter the elements of matrix1");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
mat1[i][j]=[Link]();
[Link]();
[Link]("Enter the elements of matrix2");
[Type text]
for(i=0;i<row;i++)
for(j=0;j<col;j++)
mat2[i][j]=[Link]();
[Link]();
for(i=0;i<row;i++)
for(j=0;j<col;j++)
res[i][j]=mat1[i][j]+mat2[i][j];
[Link]("Sum of matrics:-");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
[Link](res[i][j]+"\t");
[Link]();
}
[Type text]
[Type text]
Implementing Interface:
Source Code:
import [Link].*;
import [Link].*;
interface Exam
void percent_cal();
class Student
String name;
int roll_no,mark1,mark2;
Student(String n,int r,int m1,int m2)
name=n;
roll_no=r;
mark1=m1;
mark2=m2;
void display()
[Link]("\n Name of Student:"+name);
[Link]("RollNo of Student:"+roll_no);
[Link]("Mark of Subject1:"+mark1);
[Type text]
[Link]("Mmark of Subject2:"+mark2);
class Result extends Student implements Exam
Result (String n,int r,int m1,int m2)
super(n,r,m1,m2);
public void percent_cal()
int total=(mark1+mark2);
float percent=total*100/200;
[Link]("Percentage:"+percent+"%");
void display()
[Link]();
class MultipleinheritaneDemo
public static void main(String args[])
Result R = new Result("Anoop Reddy",12,93,84);
[Link]();
[Type text]
R.percent_cal();
}
[Type text]
[Type text]
Create and deal multiple threads
Source Code:
import [Link].*;
class first extends Thread
public void run()
for(int i=0; i<=20; i+=2)
[Link]("\n Even Number:"+i);
class second extends Thread
public void run()
for(int i=1; i<=20; i+=2)
[Link]("\n Even Number:"+i);
class MultiThread
public static void main(String args[])
[Link]("Thread is started.....");
[Type text]
first obj=new first();
second obj1=new second();
[Link]();
[Link]();
[Link]("Thread is executed....");
}
[Type text]
[Type text]
Create and Import Package:
Source Code: [Link]
package mypack;
public class Add
public void addition()
int a = 100;
int b = 200;
[Link]("The sum is :"+(a+b));
[Link]
import [Link];
public class myclass
public static void main(String args[])
Add a = new Add();
[Link]();
}
[Type text]
[Type text]
Throwing your own Exception:
Source Code:
import [Link].*;
class ExceptionDemo
public static void main(String args[])
int a=15;
for(int i=3;i>=0;i--)
try
[Link](a/i);
catch(ArithmeticException e)
[Link](e);
}
[Type text]
[Type text]
Applet to Design a Webpage:
Source Code:
[Link]
import [Link].*;
import [Link].*;
public class HelloworldApplet extends Applet
public void paint(Graphics g)
[Link]("Hellow World",25,50);
[Link]
<html>
<title> The Helloe World Applet</title>
<applet code=”[Link]” width=”320”height=”120”>
</applet>
</hr>
</html>
[Type text]
[Type text]
Mouse Events handling:
Source Code:
import [Link].*;
import [Link].*;
public class MouseListenerExample2 extends Frame implements MouseListener
MouseListenerExample2()
addMouseListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
public void mouseClicked(MouseEvent e)
Graphics g=getGraphics();
[Link]([Link]);
[Link]([Link](),[Link](),30,30);
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
public void mousePressed(MouseEvent e)
[Type text]
public void mouseReleased(MouseEvent e)
public static void main(String[]args)
new MouseListenerExample2();
Keyboard Events Handling:
Source Code:
import [Link].*;
import [Link].*;
import [Link];
/*<applet code="Demokey" width=300 height=300></applet>*/
public class Demokey extends Applet implements KeyListener
String msg;
int x=50;
int y=100;
public void init()
{
[Type text]
addKeyListener(this);
requestFocus();
public void keyPressed(KeyEvent e)
showStatus("Key Pressed");
repaint();
public void keyReleased(KeyEvent e)
showStatus("Key Released");
repaint();
public void keyTyped(KeyEvent e)
msg+=[Link]();
repaint();
public void paint(Graphics g)
[Link](msg,x,y);
}
[Type text]