You are on page 1of 20

Ex.

No: 1 Program to illustrate the use of classes and objects


Aim:
Write Program to illustrate the use of classes and objects

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

Ex.No: 2 Program to illustrate the use of String class.


Aim:
2
Write Program to illustrate the use of String class.
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:
ladladlad
Output:
D:\Java>javac chararray.java
D:\Java>java chararray
String 1 is UNIVERSITY
String 2 is MALAWI
Char at index 3 in strOb1: V
Concatenated String is : UNIVERSITY MALAWI
S1! = S2
S1 = S3

Ex.No: 3 Program to illustrate the use of final and static keyword.


Aim:
3
Write Program to illustrate the use of final and static keyword.
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 (a): Static keyword
class Company
{
int id;
String name;
static long salary;
static void change()
{
salary=salary + 50000;
}
Company(int i,String n,long s)
{
id=i;
name=n;
salary=s;
}
void show()
{
System.out.println("Id of employee is "+id);
System.out.println("Name of Employee is "+name);

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

Ex.No: 4 Program to illustrate the use of Inheritance


Aim:
Write Program to illustrate the use of Inheritance.
Procedure:
6
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 A
{
int i, j;
void showij()
{
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A
{
int k;
void showk()
{
System.out.println("k: " + k); }
void sum()
{
System.out.println("i+j+k: " + (i+j+k));
}
}
class SimpleInheritance
{

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();
}
}

Ex.No: 5 Program to illustrate the use of Interfaces


Aim:
Write Program to illustrate the use of Interfaces.
Procedure:

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:

Ex.No: 6 Program to illustrate the use of Multithreading


Aim:
Write Program to illustrate the use of Multithreading.
Procedure:

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

Ex.No: 7 Program to illustrate the use of Exception Handling


Aim:
Write Program to illustrate the use of Exception Handling.
Procedure:

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:

Ex.No: 8 Program to create and read file.


Aim:
Write Program to create and read file .
Procedure:

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:

Ex.No: 9 Program to create applet and pass parameter to it.


Aim:
Write Program to create applet and pass parameter to it.
Procedure:

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:

Ex.No: 10 Program to illustrate handling of mouse event.


Aim:
Write Program to illustrate handling of mouse event.
Procedure:
17
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.io.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/* <APPLET CODE=Mouse.class WIDTH=200 HEIGHT=200 > </APPLET> */
public class Mouse extends Applet implements
MouseListener,MouseMotionListener
{
String txt="";
int x=10,y=30;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
txt="Mouse Clicked";
setForeground(Color.pink);
repaint();

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

You might also like