Practical Java Program New
Practical Java Program New
Practical Java Program New
method of overloading.
-----------------------------------------------------------------------------------------------------------------------------
PROGRAM
import java.io.*;
class area
{
void findarea(int a,int b)
{
System.out.println("\n Area of rectangle with breadth"+a+" and length"+b+"is:"+a*b);
}
void findarea(int a)
{
System.out.println("\n Area of Square with the length "+a+"is:"+a*a);
}
void findarea(double r)
{
double pi=3.14; double area=pi*r*r;
System.out.println("\n The area of circle is:"+area);
}
public static void main(String p[]) throws IOException
{
while(true)
{
area d=new area();
BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n FIND THE AREA OF \n 1.RECTANGLE\n 2.CIRCLE\n 3.SQUARE\n
4.EXIT\n\n SELECT A CHOICE:");
int choice=Integer.parseInt(Br.readLine());
switch(choice)
{
case 1:
System.out.println("\n Enter the Breadth:");
int a=Integer.parseInt(Br.readLine());
System.out.println("\n Enter the Length:");
int b=Integer.parseInt(Br.readLine());
d.findarea(a,b);break;
case 2:
System.out.println("\n Enter the Radius of Circle:");
double x=Integer.parseInt(Br.readLine());
d.findarea(x);break;
case 3:
System.out.println("\n Enter the side of the Square:");
int r=Integer.parseInt(Br.readLine());
d.findarea(r); break;
case 4:
System.exit(0); break;
default:
System.out.println("invalid choice");
}
}
}
}
OUTPUT
AREA OF RECTANGLE
AREA OF CIRCLE
AREA OF SQUARE
EX NO: 2 write a program to sort the list of numbers using Command Line Arguments
--------------------------------------------------------------------------------------------------------------------
PROGRAM:
import java.io.*;
public class Sort
{
public static void main(String[] args)
{
int n, temp;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter no. of Elements you want in Array:");
n = Integer.parseInt(br.readLine());
int a[] = new int[n];
System.out.println("Enter all the Elements:");
for (int i = 0; i < n; i++)
{
a[i] = Integer.parseInt(br.readLine());
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];a[i] = a[j]; a[j] = temp;
}
}
}
System.out.println("\n\nSorted List...");
System.out.print("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
System.out.println("Descending Order");
for(i=size-1;i>=0;i--)
{
System.out.println(a[i]+"\t");
}
}
OUTPUT
Sorted List...
Ascending Order
5
16
26
62
75
89
Descending Order
89
75
62
26
16
5
EX NO: 3 W rite a Program to Multiply the given Two Matrices
----------------------------------------------------------------------------------
PROGRAM
import java.io.*;
class MatrixMultiplication
{
public static void main(String args[])throws IOException
{
int m, n, p, q, sum = 0, c, d, k;
BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of rows and columns of first matrix");
m = Integer.parseInt(Br.readLine());
n = Integer.parseInt(Br.readLine());
int first[][] = new int[m][n];
System.out.println("Enter Elements of first matrix");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
first[c][d] = Integer.parseInt(Br.readLine());
}}
System.out.println("Enter the number of rows and columns of second matrix");
p = Integer.parseInt(Br.readLine());
q = Integer.pasreInt(Br.readLine());
if (n != p)
System.out.println("The matrices can't be multiplied with each other.");else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println("Enter elements of second matrix");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
second[c][d] = Integer.parseInt(Br.readLine());
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
for (k = 0; k < p; k++)
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;sum = 0;
}
}
System.out.println("Product of the matrices:");for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
System.out.print(multiply[c][d]+"\t");
System.out.print("\n");
}
}
}
OUTPUT
EX NO: 4 write a program to design a class to represent a bank account. Include the following:
Data Members: Name of the depositor, Account number, Type of account, and Balance amount
in the account. Methods: To assign initial values, to deposit an amount, to withdraw an amount
after checking balance, and to display the name and balance.
-------------------------------------------------------------------------------------------------------------------------
PROGRAM:
import java.io.*; import java.lang.*;
import java.io.BufferedReader; import java.io.IOException;
import java.io.InputStreamReader;class Bank
{
public static void main(String args[])throws IOException
{
String[] name=new String[100]; String[] addr=new String[100]; int[] initialamount=new
int[100];int[] accno=new int[100];
int k=1,choice,deposit,i,j=0,withdraw,ac;while(true)
{
System.out.println("WELCOME to SBI"); System.out.println("**************");
System.out.println("1.CREATE"); System.out.println("2.DEPOSIT");
System.out.println("3.WITHDRAW"); System.out.println("4.BALANCE ENQUIREY");
System.out.println("5.EXIT"); System.out.println("ENTER YOUR CHOICE:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
choice =Integer.parseInt(input);
System.out.println("YOU ENTERED"+choice);
switch(choice)
{
case 1:
System.out.println("ENTER YOUR NAME:");
name[k]=br.readLine();
System.out.println("ENTER YOUR INITIAL AMOUNT:");
initialamount[k]=Integer.parseInt(br.readLine());
System.out.println("ENTER YOUR ADDRESS:");
addr[k]=br.readLine();
if(initialamount[k]==0)
{
System.out.println(" ");
initialamount[k]=Integer.parseInt(br.readLine());
}
accno[k]=k;
System.out.println("YOUR ACC NO IS SBI 0000"+accno[k]+k++);break;
case 2:
System.out.println("ENER YOUR ACCOUNT NO:");
ac=Integer.parseInt(br.readLine());
for(i=0;i<accno.length;i++)
{
if(ac==accno[i])
{
System.out.println("ENTER DEPOSIT AMOUNT:");
deposit=Integer.parseInt(br.readLine());
initialamount[i]=initialamount[i]+deposit;
System.out.println("TOTAL AMOUNT ="+initialamount[i]);
j=1;
}
}
if(j==0)
System.out.println("WRONG ACC NO");break;
case 3:
j=0;
System.out.println("ENTER ACCOUNT NO:");
ac=Integer.parseInt(br.readLine());
for(i=0;i<accno.length;i++)
{
if(ac==accno[i])
{
System.out.println("ENTER AMOUNT:");
withdraw=Integer.parseInt(br.readLine());
if(withdraw>initialamount[i])
System.out.println("LOW BALANCE");
else
{
initialamount[i]=initialamount[i]-withdraw;
System.out.println("YOUR BALANCE IS:"+initialamount[i]);
j=1;
break;
} } }
if(j==0)
{
if(j==0)
System.out.println("WRONG ACC NO");
break;
}
case 4:
System.out.println("ENTER ACCOUNT NO");
ac=Integer.parseInt(br.readLine());for(i=0;i<accno.length;i++)
{
if(ac==accno[i])
{
System.out.println("YOUR BALANCE IS"+initialamount[i]);j=1;
}
}
if(j==0)
System.out.println("WRONG ACC NO");break;
case 5:
System.exit(0);break;
default:
System.out.println("ENTER CURRECT OPTION:");
break;
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
OUTPUT
BALANCE ENQUIRY
EX NO 5: Write a program that import the user defined package and access the Member
variable ofclasses that contained by Package.
-----------------------------------------------------------------------------------------------------------------------------
PROGRAM:
1.PACKAGE:
package employee;import java.io.*;
import java.lang.*;
public class first
{
public String eno,ename;
public int basicpay,allow,deduct;
public void input()
{
try
{
BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Employee Number");
eno=d.readLine();
System.out.println("Enter Employee Name");
ename=d.readLine();
System.out.println("Enter the Basicpay");
basicpay=Integer.parseInt(d.readLine());
System.out.println("Enter the Allowance");
allow=Integer.parseInt(d.readLine());
System.out.println("enter deduction");
deduct=Integer.parseInt(d.readLine());
}
catch(IOException e)
{
System.out.println("error"+e);
}
}
}
MAIN PROGRAM
import employee.*;
import java.io.*;
interface second
{
void display();
}
}
class third extends first implements second
{
public void display()
{
System.out.println("PAY SLIP FOR THE EMPLOYEE”);
System.out.println("******************************”);
System.out.println("EMPLOYEE NUMBER"+eno);
System.out.println("EMPLOYEE NAME"+ename);
System.out.println("BASIC PAY"+basicpay);
System.out.println("ALLOWANCE"+allow);
System.out.println("DEDUCTION"+deduct);
int gpay=basic+allow;int npay=gpay-deduct;
System.out.println("GROSS PAY"+gpay);System.out.println("NETPAY"+npay);
}
}
class emp1
{
public static void main(String args[])
{
third t=new third();t.input();
t.display();
}
OUTPUT
EX NO: 6 Write a Program to handle the Exception using try and Multiple Catch Blocks.
------------------------------------------------------------------------------------------------------------------
PROGRAM
ExceptionHandling.java
import java.util.*;
import java.io.*;
class ExceptionHandling
{
public static void main(String args[])
{
try
{
int a=Integer.parseInt(args[0]);int b=Integer.parseInt(args[1]);
System.out.println("Answer:"+a/b);
}
catch(ArithmeticException e)
{
System.out.println("\n\tDivision Error");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("\n\tError in index value");
}
catch(NumberFormatException e)
{
System.out.println("\n\tData type error");
}
finally
{
System.out.println("\n\tFinally block executed");
}
}
}
Output:
C:\JavaLab>javac ExceptionHandling.java
C:\JavaLab>java ExceptionHandling 10 5
Answer:2
Finally block executed
C:\JavaLab>java ExceptionHandling 10 0
Division Error
Finally block executed
C:\JavaLab>java ExceptionHandling
Error in index value Finally block executed
C:\JavaLab>java ExceptionHandling 10 a
Data type error
Finally block executed
NO: 7 Write a Program to illustrate the use of Multi Threads.
---------------------------------------------------------------------------------
PROGRAM
PROGRAM
StudReg1.java
import java.awt.*; import java.applet.*; import java.awt.event.*;
public class StudReg1 extends Applet implements ActionListener
{
Label l,l1,l2,l3,l4,l5;TextField t1,t3; TextArea t2;
Button b1,b2; String msg; public void init()
{
l=new Label("Student's Registration Form",Label.CENTER);
l1=new Label("Name",Label.LEFT);
l2=new Label("Address",Label.LEFT);
l3=new Label("Sex",Label.LEFT);
l4=new Label("Class",Label.LEFT);
l5=new Label("Email Id",Label.LEFT);
t1=new TextField();
t2=new TextArea("",180,90,TextArea.SCROLLBARS_VERTICAL_ONLY);
CheckboxGroup cbg=new CheckboxGroup();
Checkbox cb1=new Checkbox("Male",false,cbg);
Checkbox cb2=new Checkbox("Female",false,cbg);
Choice c1=new Choice();
t3=new TextField();
Button b1=new Button("Submit");
Button b2=new Button("Reset"); setLayout(null);
add(l); add(l1); add(l2); add(l3);
add(l4); add(l5); add(t1); add(t2);
add(cb1); add(cb2); add(c1);
c1.add("I BSC CS"); c1.add("II BSC CS");
c1.add("III BSC CS"); c1.add("I MSC CS");
c1.add("II MSC CS");
add(t3); add(b1);
add(b2);
b1.addActionListener(this); b2.addActionListener(this); l.setBounds(5,30,290,20);
l1.setBounds(25,65,90,20); l2.setBounds(25,95,90,20); l3.setBounds(25,125,90,20);
l4.setBounds(25,155,90,20); l5.setBounds(25,185,90,20); t1.setBounds(120,65,170,20);
t2.setBounds(120,95,170,20);
cb1.setBounds(120,125,50,20);cb2.setBounds(170,125,60,20);
c1.setBounds(120,155,100,20);t3.setBounds(120,185,170,20);
b1.setBounds(120,350,50,30); b2.setBounds(220,350,50,30);
}
public void paint(Graphics g)
{
g.drawString(msg,100,250);
}
StudReg1.html
<html>
<body>
<applet code="StudReg1.class" width=500 height=500>
</applet>
</body>
</html>
OUTPUT
EX NO: 9 write a JAVA Applet program to draw the line, Rectangle, Oval, and Text using the
Graphics method.
PROGRAM
Graphicsdraw.java
import java.awt.*;
import java.applet.*;
public class Graphicsdraw extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red); g.drawLine(100,100,390,100);
g.drawString("Line",240,120); g.setColor(Color.green); g.drawRect(50,150,90,50);
g.drawString("Rectangle",70,220); g.setColor(Color.blue); g.fillRect(150,150,90,50);
g.drawString("Filled Rectangle",150,220);g.setColor(Color.magenta);
g.drawOval(250,150,90,50); g.drawString("Oval",280,220); g.setColor(Color.gray);
g.fillOval(350,150,90,50); g.drawString("Filled Oval",365,220);
}
}
Graphicsdraw.html
<html>
<body>
<applet code="Graphicsdraw.class" width=500 height=500>
</applet>
</body>
</html>
OUTPUT
10. Write a program to create a sequential file that could store about five products. Details
include product code, cost, and provide through the keyboard compute and print the total value
of all the fiveproducts.
--------------------------------------------------------------------------------------------------------------------------------------
PROGRAM
import java.io.*; import java.util.*;
public class Products1
{
public static void main(String arg[])
{
File file=new File("prod1");
int j=0;
try
{
FileOutputStream fos=new FileOutputStream(file);
DataOutputStream dos=new DataOutputStream(fos);
DataInputStream dis=new DataInputStream(System.in);
StringTokenizer st;
dos.writeBytes("Product Code"+"\t"+"Product Cost"+"\t"+"No.of items"+"\t"+"Total"+"\n");
for(int i=0;i<5;i++)
{
System.out.print("Please Enter the Product Code:");
st=new StringTokenizer(dis.readLine());
String pnum=new String(st.nextToken());
System.out.print("Please Enter the Product cost:");
st=new StringTokenizer(dis.readLine());
double cost=new Double(st.nextToken()).
doubleValue();System.out.print("Please Enter the Product Quantity:");
st=new StringTokenizer(dis.readLine());
int qty=new Integer(st.nextToken()).intValue();
double tot=cost*qty;
String strcost=Double.toString(cost);
String strqty=Integer.toString(qty);
String strtot=Double.toString(tot);
dos.writeBytes(pnum+"\t\t");
dos.writeBytes(strcost+"\t\t");
dos.writeBytes(strqty+"\t\t");
dos.writeBytes(strtot+"\n");
}
dos.close();
}
catch(IOException e)
{
System.out.print("IO Error:"+e);
} }
OUTPUT
C:\jdk1.3\bin\javapgm>java Products1
Please enter the product code: p101 Please enter the product cost:25.50
Please enter the product quantity:10 Please enter the product code:p102
Please enter the product cost:35.50 Please enter the product quantity:70
Please enter the product code:p103 Please enter the product cost:89.50
Please enter the product quantity:220 Please enter the product code:p104
Please enter the product cost:89 Please enter the product quantity:78
Please enter the product code:p105 Please enter the product cost:78
Please enter the product quantity:50
C:\jdk1.3\bin\javapgm>type prod1