Professional Documents
Culture Documents
Procedure:
Step1: Go to Start->Programs->select accessories->notepad, Notepad window will open.
Step2: Write the coding or tags to design a form.
Step3: Run the program using command prompt.
Program:
class Rectangle
{
int length;
int width;
void insert(int l,int w)
{
length=l;
width=w;
}
void calculateArea()
{
System.out.println(“The area of Rectangle is :”+length*width);
}
public static void main(String args[])
{
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
1
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
OUTPUT:
The area of Rectangle is: 55
The area of Rectangle is: 45
4
System.out.println("Salary of Employee is "+salary);
}
public static void main(String arg[])
{
Company.change();
Company c1=new Company(100,"Thibisha",100000);
Company c2=new Company(101,"Vagini",200000);
Company c3=new Company(102,"Sowmiya",150000);
c1.show();
c2.show();
c3.show();
}
}
Output
Id of employee is 100
Name of Employee is Thibisha
Salary of Employee is 150000
Id of employee is 101
Name of Employee is Vagini
Salary of Employee is 150000
Id of employee is 102
Name of Employee is Sowmiya
Salary of Employee is 150000
Program (b): Final Keyword
class Bike
{
5
final int speedlimit;
int no=3154;
char name[10]=”Pulsar”;
Bike
{
speedlimit=70;
System.out.println(“Name :”+name);
System.out.println(“Bike number:”+no);
System.out.println(“Speed limit”+speedlimit);
}
public static void main(String args[]){
new Bike();
}
}
Output:
Name: Pulsar
Bike Number: 3154
Speed limit:70
7
public static void main(String args[])
{
A superOb = new A();
B subOb = new B();
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}
}
8
Step1: Go to Start->Programs->select accessories->notepad, Notepad window will
open.
Step2: Write the coding or tags to design a form.
Step3: Run the program using command prompt.
Program:
public class Main
{
public static void main(String[] args) {
shapeA circleshape = new circle();
circleshape.Draw();
}
}
interface shapeA {
public String baseclass = "shape";
public void Draw();
}
interface shapeB extends shapeA {
public String baseclass = "shape2";
public void Draw2();
}
interface shapeC {
public String baseclass = "shape3";
public void Draw3();
}
9
class circle implements shapeA, shapeB, shapeC {
public void Draw() {
System.out.println("Drawing Circle here:" + shapeA.baseclass);
}
public void Draw2() {
System.out.println("Drawing Circle here:" + shapeB.baseclass);
}
public void Draw3() {
System.out.println("Drawing Circle here:" + shapeC.baseclass);
}
}
Output:
10
Step1: Go to Start->Programs->select accessories->notepad, Notepad window will
open.
Step2: Write the coding or tags to design a form.
Step3: Run the program using command prompt.
Program:
class ThreadDemo
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
t.setName("My Thread");
System.out.println("After name change: " + t);
try
{
for(int n = 5; n > 0; n--)
{
System.out.println(n);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted");
}
}
11
}
Output:
D:\Java>javac ThreadDemo.java
D:\Java>java ThreadDemo
Current thread: Thread[main,5,main]
After name change: Thread[My Thread,5,main]
5
4
3
2
1
12
Step1: Go to Start->Programs->select accessories->notepad, Notepad window will
open.
Step2: Write the coding or tags to design a form.
Step3: Run the program using command prompt.
Program:
class Excep{
public static void main(String args[]){
try{
int a[]=new int[7];
a[4]=30/0;
System.out.println("First print statement in try block");
}
catch(ArithmeticException e){
System.out.println("Warning: ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Warning: ArrayIndexOutOfBoundsException");
}
catch(Exception e){
System.out.println("Warning: Some Other exception");
}
System.out.println("Out of try-catch block...");
}
}
13
Output:
14
Step1: Go to Start->Programs->select accessories->notepad, Notepad window will
open.
Step2: Write the coding or tags to design a form.
Step3: Run the program using command prompt.
Program:
import java.io.*;
class C{
public static void main(String args[])throws Exception{
FileInputStream fin=new FileInputStream("C.java");
FileOutputStream fout=new FileOutputStream("M.java");
int i=0;
while((i=fin.read())!=-1){
fout.write((byte)i);
}
fin.close();
}
}
Output:
15
Step1: Go to Start->Programs->select accessories->notepad, Notepad window will
open.
Step2: Write the coding or tags to design a form.
Step3: Run the program using command prompt.
Step4: output will be display in browser.
Program:
import java.awt.*;
import java.applet.*;
public class ParameterExample extends Applet
{
int parameter2;
int parameter3;
int result;
public void init()
{
parameter1 = getParameter("param1");
parameter2 = Integer.parseInt(getParameter("param2"));
parameter3 = Integer.parseInt(getParameter("param3"));
result = parameter2 + parameter3;
}
public void paint(Graphics g)
{
g.drawString("Parameter 1 is: " + parameter1,20,20);
g.drawString("Parameter 2 is: " + parameter2,20,40);
g.drawString("Parameter 3 is: " + parameter3,20,60);
g.drawString("Parameter 2 + parameter 3 is: " + result,20,80);
16
}
}
HTML File:
/*
<APPLET CODE="ParameterExample" WIDTH=200 HEIGHT=100>
<param name="param1" value="Hello">
<param name="param2" value="14">
<param name="param3" value="2">
</APPLET>
*/
Output:
18
}
public void mouseEntered(MouseEvent me)
{
txt="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
txt="Mouse Exited";
setForeground(Color.blue);
repaint();
}
public void mousePressed(MouseEvent me)
{
txt="Mouse Pressed";
setForeground(Color.blue);
repaint();
}
public void mouseMoved(MouseEvent me)
{
txt="Mouse Moved";
setForeground(Color.red);
repaint();
}
public void mouseDragged(MouseEvent me)
{
19
txt="Mouse Dragged";
setForeground(Color.green);
repaint();
}
public void mouseReleased(MouseEvent me)
{
txt="Mouse Released";
setForeground(Color.yellow);
repaint();
}
public void paint(Graphics g)
{
g.drawString(txt,30,40);
showStatus("Mouse events Handling");
}
}
Output:
20