EX.
NO : 01
METHOD OVERLOADING
DATE :
Aim:
To develop a java program to find area of square, rectangle, circle
using method overloading.
Algorithm:
Step 1: Start the program.
Step 2: Declare three methods of same name but with
different number of arguments or with different data
types.
Step 3: Call these methods using objects, corresponding methods
will be called as per the number of arguments and types
of arguments.
Step 4: Find Area of Square, Rectangle and Circle using Method
Overloading.
Step 5: Display the result.
Step 6: Stop the program.
Coding:
class Overload
{
double area(float l)
{
return l * l ;
}
int area(int l, int b)
{
return l * b;
}
float area(int r)
{
return ((22/7) * r * r);
}
}
public class MethodOverloading
{
public static void main(String args[])
{
Overload overload = new Overload();
double square = overload.area(5);
System.out.println("Area of Square is " + square);
System.out.println("");
double rectangle = overload.area(5,4);
System.out.println("Area of Rectangle is " + rectangle);
System.out.println("");
float circle = overload.area(4);
System.out.println("Area of Circle is " + circle);
}
}
Output
Area of Square is 75.0
Area of Rectangle is 20.0
Area of Circle is 48.0
RESULT:
Thus the above program has been executed successfully and verified.
EX.NO : 02 SORT THE LIST OF NUMBER USING
DATE : COMMAND LINE ARGUMENT
Aim:
To develop a java program to sort the list of number using command
line argument.
Algorithm:
Step 1: Start the program.
Step 2: Declare an array variable to get values from command
prompt.
Step 3: Using for Loop and If Statement sort the list of numbers.
Step 4: Print the sorted values.
Step 5: Display the result.
Step 6: Stop the program.
Coding:
import java.io.*;
class Sorting
{
public static void main(String args[ ])throws IOException
{
DataInputStream k=new DataInputStream(System.in);
int a[ ]=new int[10];
System.out.println("your input data are");
for(int i=0;i<a.length;i++)
a[i]=Integer.parseInt(k.readLine());
for(int i=0;i<a.length;i++)
for(int j=0;j<a.length;j++)
if(a[i]<a[j])
{
int t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
System.out.println("**********************************************");
System.out.println("Sort the list of numbers using commandline
argument");
System.out.println("**********************************************");
System.out.println("sorting order of given no is");
for(int i=0;i<a.length;i++)
System.out.println(" "+a[i]);
}
}
Output:
4
5
2
6
1
7
8
9
1
0
****************************************************************
Sort the list of numbers using commandline argument
****************************************************************
sorting order of given no is
0
1
1
2
4
5
6
7
8
9
Result:
Thus the program is successfully executed and the output is verified.
EX.NO : 03
MATRIX MULTIPLICATION
DATE :
Aim:
To write a java program to multiply the given two matrixes.
Algorithm:
Step 1: Start the program.
Step 2: Declare variables and initialize necessary variables.
Step 3: Enter the element of matrices by row wise using loops.
Step 4: Check the number of rows and column of first and second
matrices.
Step 5: Multiply the matrices using nested loops.
Step 6: Display the result.
Step 7: Stop the program.
Coding:
Public class MatrixMultiplication
{
Public static void main (String args[ ])
{
int a[ ][ ]={{1,1,1},{2,2,2},{3,3,3}};
int b[ ][ ]={{1,1,1},{2,2,2},{3,3,3}};
int c[ ][ ]=new int [3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
C[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j] + “ ”);
}
System.out.println( );
}
}
}
Output:
6 6 6
12 12 12
18 18 18
Result:
Thus the program is successfully executed and the output is verified
EX.NO : 04
BANKING MANAGEMENT SYSTEM
DATE :
Aim:
To develop a java program for creating bank account and perform the
operations such as deposit & withdraw the amount and checking balance
Algorithm:
Step 1: Start the program.
Step 2: Declare the variables to get account details.
Step 3: Create functions for deposit ( ) , withdrawal( ),display ( )
and dbal( ).
Step 4: Using switch statement create individual case operation.
1. Deposit
2. Withdraw
3. Display
4. Quit.
Step 5: Select appropriate option to get the result.
Step 6: Display the result.
Step 7: Stop the program.
Coding:
import java.util.*;
class BankAccount
{
static Scanner input = new Scanner(System.in);
String name, actype;
int accNo, bal, amt;
BankAccount(String name, int accNo, String actype, int bal)
{
this.name = name;
this.accNo = accNo;
this.actype = actype;
this.bal = bal;
}
int deposit()
{
System.out.print("Enter amount to deposit:");
amt = input.nextInt();
if (amt < 0)
{
System.out.println("Invalid Amount");
return 1;
}
bal = bal + amt;
return 0;
}
int withdraw()
{
System.out.println("Your Balance=" + bal);
System.out.print("Enter amount to withdraw:");
amt = input.nextInt();
if (bal < amt)
{
System.out.println("Not sufficient balance.");
return 1;
}
if (amt < 0)
{
System.out.println("Invalid Amount");
return 1;
}
bal = bal - amt;
return 0;
}
void display()
{
System.out.println("Name:" + name);
System.out.println("Account No:" + accNo);
System.out.println("Balance:" + bal);
}
void dbal()
{
System.out.println("Balance:" + bal);
}
public static void main(String args[])
{
System.out.println("Enter your Name: ");
String nn = input.nextLine();
System.out.println("Enter Account Number: ");
int num = input.nextInt();
System.out.println("Enter Account Type: ");
String type = input.next();
System.out.println("Enter Initial Balance: ");
int bal = input.nextInt();
BankAccount b1 = new BankAccount(nn, num, type, bal);
int menu;
System.out.println("Menu");
System.out.println("1. Deposit Amount");
System.out.println("2. Withdraw Amount");
System.out.println("3. Display Information");
System.out.println("4. Exit");
boolean quit = false;
do
{
System.out.print("Please enter your choice: ");
menu = input.nextInt();
switch (menu)
{
case 1:
b1.deposit();
break;
case 2:
b1.withdraw();
break;
case 3:
b1.display();
break;
case 4:
quit = true;
break;
}
} while (!quit);
}
Output
Enter your Name: Bhuvaneswari
Enter Account Number: 30121999
Enter Account Type: Saving
Enter Initial Balance: 500
Menu
1. Deposit Amount
2. Withdraw Amount
3. Display Information
4. Exit
Please enter your choice: 1
Enter amount to deposit: 20000
Please enter your choice: 2
Enter amount to withdraw: 1500
Please enter your choice: 3
Name: Bhuvaneswari
Account No: 30121999
Balance: 19500
Result:
Thus the program is successfully executed and the output is verified
EX.NO : 05
USER DEFINED PACKAGE
DATE :
Aim:
To develop a java program to import the user defined package and
access the Member variable of classes that contained by Package.
Algorithm:
Step 1: Start the program.
Step 2: Define a package P1.
Step 3: Create a class output.
Step 4: Declare the variable to get the data from the user.
Step 5: Define the methods get( ), Cal( ) and Display( ).
Step 5: Create the main class as ‘empsalary’ in the package P1.
Step 6: First run the package class and followed by main class.
Step 7: Display the result.
Step 8: Stop the program.
Coding:
package p1;
import java.io.*;
interface scale
{
double cca=0.5,hra=0.07,da=0.1,ta=0.08,pf=0.12;
}
class input
{
int bp, eno;
String ename;
DataInputStream d = new DataInputStream (System.in);
public void get( ) throws Exception
{
System.out.println (“Enter Employee Name\n”);
ename=d.readLine( );
System.out.println (“Enter Employee Number\n”);
eno=Integer.parseInt(d.readLine( ));
System.out.println (“Enter Employee Basic Pay\n”);
bp=Integer.parseInt(d.readLine( ));
}
}
public class output extends input implements scale
{
double gp, np;
public void cal( )
{
gp=bp + (bp*cca) + (bp*hra) + (bp*da)+(bp*ta);
np=gp-(bp*pf);
}
public void display( )
{
System.out.println("\n\t\t\t Employee Name :"+ename);
System.out.println("\n\t\t\t Employee Number :"+eno);
System.out.println("\n\t\t\t Basic Pay :"+bp);
System.out.println("\n\t\t\t Gross Pay :"+gp);
System.out.println("\n\t\t\t Net Pay :"+np);
}
}
/* MAIN PROGRAM */
import p1.*;
class empsalary
{
public static void main(String arg[ ])throws Exception
{
output s=new output( );
s.get( );
s.cal( );
s.display( );
}
}
Output
Enter Employee Name
Bhuvaneswari
Enter Employee Number
30121999
Enter Employee Basic Pay
20000
Employee Name : Bhuvaneswari
Employee Number : 30121999
Basic Pay : 20000
Gross Pay : 35800.0
Net Pay : 33400.0
Result:
Thus the program is successfully executed and the output is verified
EX.NO : 06
EXCEPTION HANDLING
DATE :
Aim:
To develop a java program to handle the Exception using try and
multiple catch blocks.
Algorithm:
Step 1: Start the program.
Step 2: Declare try and catch block in main class to catch error.
Step 3: A single try block can have any number of catch blocks.
Step 4: Catch block can handle the exceptions like
ArrayIndexOutOfBoundsException/ArithmeticException/
NegativeArrayException.
Step 5: If error occurs it goes to the corresponding exception
Step 6: Display the exception.
Step 7: Stop the program.
Coding:
import java.io.*;
class excep
{
public static void main(String args[ ])throws IOException
{
int a,b,sum=0;
double c;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("\t\t\n EXCEPTION HANDLING");
System.out.println("\t\t Enter a value");
a=Integer.parseInt(in.readLine( ));
System.out.println("Enter b value");
b=Integer.parseInt(in.readLine( ));
c=a/b;
System.out.println("value of:"+c);
System.out.println("enter the size of Array");
int size=Integer.parseInt(in.readLine());
int x[]=new int[size];
System.out.println("enter the Array elements");
for(int i=0;i<size;i++)
{ x[i]=Integer.parseInt(in.readLine());
sum=sum+x[i]; }
System.out.println("sum="+sum);
System.out.println("enter the index of Array element you need");
int ind=Integer.parseInt(in.readLine());
System.out.println("element at index "+ind+" is: "+x[ind]);
}
catch (ArithmeticException ae)
{
System.out.println("error"+ae);
}
catch (ArrayIndexOutOfBoundsException be)
{
System.out.println("error"+be);
}
catch (NegativeArraySizeException ae)
{
System.out.println("error"+ae);
}
}
}
Output
EXCEPTION HANDLING
Enter a value
2
Enter b value
0
errorjava.lang.ArithmeticException: / by zero
EXCEPTION HANDLING
Enter a value
4
Enter b value
2
value of:2.0
enter the size of Array
3
enter the Array elements
10
67
90
sum=167
enter the index of Array element you need
3
errorjava.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
EXCEPTION HANDLING
Enter a value
8
Enter b value
6
value of:1.0
enter the size of Array
-3
errorjava.lang.NegativeArraySizeException: -3
Result:
Thus the program is successfully executed and the output is verified
EX.NO : 07
MULTI THREADS
DATE :
Aim:
To develop a java program to illustrate the use of multi threads
Algorithm:
Step 1: Start the program.
Step 2: Declare a run( ) method in different classes.
Step 3: Initiate the Thread object for each class.
Step 4: By using the thread object call the start ( ) method
Step 5: It executes run( ) method in all classes concurrently.
Step 6: Display the result.
Step 7: Stop the program.
Coding:
import java.io.*;
class A extends Thread
{
public void run( )
{
System.out.println("Thread A started");
for(int i=1;i<=5;i++)
{
System.out.println(i+"*2="+(i*2));
}
System.out.println("Exit from Thread A");
}
}
class B extends Thread
{
public void run( )
{
System.out.println("Thread B started");
for(int i=1;i<=5;i++)
{
System.out.println(i+"*3="+(i*3));
}
System.out.println("Exit from Thread B");
}
}
class C extends Thread
{
public void run( )
{
System.out.println("Thread C started");
for(int i=1;i<=5;i++)
{
System.out.println(i+"*4="+(i*4));
}
System.out.println("Exit from Thread C");
}
}
class MultiThread
{
public static void main(String args[ ])
{
System.out.println("\t\t\t\n MULTITHREAD\t\t");
A Ta=new A( );
Ta.start( );
B Tb=new B( );
Tb.start( );
C Tc=new C( );
Tc.start( );
}
}
Output
MULTITHREAD
Thread A started
1*2=2
2*2=4
3*2=6
4*2=8
5 * 2 = 10
Exit from thread A
Thread B started
1*3=3
2*3=6
3*3=9
4 * 3 = 12
5 * 3 = 15
Exit from thread B
Thread C started
1*4=4
2*4=8
3 * 4 = 12
4 * 4 = 16
5 * 4 = 20
Exit from thread C
Result:
Thus the program is successfully executed and the output is verified
EX.NO : 08 STUDENT REGISTRATION FORM USING
DATE : APPLET
Aim:
To develop a java program to create student registration form using
applet with Name, Address, Sex, Class, Email-id.
Algorithm:
Step 1: Start the program.
Step 2: Declare the Applet header files.
Step 3: Create an applet window to display Name, Address, Sex,
Class, E mail-id. .
Step 4: Create Submit button to submit the form details.
Step 5: Create cancel button to clear the entire Field.
Step 6: Run the Program using applet
viewer. Step 7: Display the result.
Step 8: Stop the Program.
Coding:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Myapp extends Applet
{
TextField name,pass, ad, email, cla;
Button b1,b2;
public void init()
{
Label n=new Label("Name:",Label.CENTER);
Label r=new Label("Address:",Label.CENTER);
Label p=new Label("Gender:",Label.CENTER);
Label m=new Label("Class:",Label.CENTER);
Label s=new Label("Email:",Label.CENTER);
name=new TextField(20);
address=new TextField(20);
gender=new TextField(20);
class=new TextField(20);
email=new TextField(20);
b1=new Button("submit");
b2=new Button("cancel");
add(n);
add(name);
add(r);
add(address);
add(g);
add(gender);
add(c);
add(class);
add(e);
add(email);
add(b1);
add(b2);
n.setBounds(70,90,90,60);
r.setBounds(90,130,90,60);
g.setBounds(100,170,90,60);
c.setBounds(120,170,90,60);
e.setBounds(120,170,90,60);
name.setBounds(280,210,90,60);
address.setBounds(200,140,90,20);
gender.setBounds(160,180,90,20);
class.setBounds(160,180,90,20);
email.setBounds(100,140,90,20);
b1.setBounds(100,260,70,40);
b2.setBounds(180,260,70,40);
}
public void paint(Graphics g)
{
repaint( );
}
}
<applet code="Myapp.class" width=300 height=300></applet>
Output:
Result:
Thus the program is successfully executed and the output is verified.
EX.NO : 09
GRAPHICS METHODS
DATE :
Aim:
To write a java program to draw the line, rectangle, oval , text using the
graphics methods.
Algorithm:
Step 1: Start the program.
Step 2: Declare the Applet header files.
Step 3: Create a class as graphics methods which extends Applet.
Step 4: Create and execute the paint (Graphics g) methods.
Step 5: Create the string , line , rectangle , oval , text in
the applet window.
Step 6: Run the Program.
Step 7: Display the result.
Step 8: Stop the Program.
Coding:
import java.applet.Applet;
import java.awt.*;
public class graphicsmethods extends Applet
{
public void paint (Graphics g)
{
g.SetColor(Color.red);
g.drawString(“Welcome”,100,100);
g.drawLine(380,100,200,180);
g.drawRect(160,5,60,60);
g.fillRect(500,15,70,90);
g.drawOval(70,200,30,30);
g.SetColor(Color.yellow);
g.fillOval(700,140,50,150);
}
}
Output
Result:
Thus the program is successfully executed and the output is verified.
EX.NO : 10
SEQUENTIAL FILE
DATE :
Aim:
To develop a program to create a sequential file that could store details
about five products that include product code, cost, and number of items
and print the total value of all the five products.
Algorithm:
Step 1: Start the program.
Step 2: Declare the file
Step 3: Read product code, cost and quantity.
Step 4: Calculate the total value of quantity.
Step 5: Print and display the details of five products.
Step 6: Run the Program.
Step 7: Display the result.
Step 8: Stop the Program.
Coding:
import java.io.*;
import java.util.*;
public class Products
{
public static void main(String arg[]){
File file = new File("products");
String pcode;
double pcost;
int qty;
try{
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
DataInputStream dis = new DataInputStream(System.in);
for(int i=0; i<5; i++){
System.out.print("Please enter the product code, cost and qty of product:"+
(i+1)+"\n");
pcode=(dis.readLine());
pcost=Double.parseDouble(dis.readLine());
qty=Integer.parseInt(dis.readLine());
dos.writeUTF(pcode);
dos.writeDouble(pcost);
dos.writeInt(qty);
}
dos.close( );
double Total = 0.0;
System.out.println("Details of all FIVE products");
}
catch (IOException e)
{ System.out.println( "IO error:" + e );
}
}
}
Output:
Please enter the product code,cost and qty of product:1
001
100
1
Please enter the product code,cost and qty of product:2
002
200
1
Please enter the product code,cost and qty of product:3
003
300
1
Please enter the product code,cost and qty of product:4
004
400
1
Please enter the product code,cost and qty of product:5
005
500
1
Details of all FIVE products
Product code cost qty
001 100 1
002 200 1
003 300 1
004 400 1
005 500 1
Total: 1500
Result:
Thus the program is successfully executed and the output is verified.