Java LAB Manual 2015
Java LAB Manual 2015
Java Programming
B.C.A - V SEMESTER
Compiled by
Mr. Krishna Prasad K
Faculty, SIMS
2015
PART-B
Ex.No. Title
1 Write a menu driven program to accept a Number &
Reverse and sum of individual digits
To generate N Fibonacci Numbers.
2 Design a class to represent a bank account include the following
members (Using constructors). Use menu driven program.
To deposit an amount
To withdraw an amount after checking the minimum balance of
1000/=
To display the name and balance
(Data members: Name of the Depositor, Account number, Balance
amount in the account. Methods: To assign initial values
(constructors))
3 Develop a program to produce pay slip of an employee name, code,
and designation. And another base class consisting of the data
members, such as account number, date of joining and basic pay. The
Java Practical Manual Page 2 of 49
Srinivas Institute of Management Studies BCA-V Semester
derived class consists of data member of the other earning (PF, LIC,
TAX). (Implements using interface).
4 Program to demonstrate multithreading using runnable interface.
Define three different threads, one to calculate square of first n
integers, another to calculate cube of first n integers and third thread
is to find the square root of first n integers. Apply various thread
priority
5 Create a package to convert temperature in centigrade into
Fahrenheit, and one more package to calculate the simple interest.
Implements both package in the main( ) by accepting the required
inputs for each application.
PART-C
Ex.No. Title
1 Create an applet to implement simple calculator. The integer data are
to be entered through the text box and the operation that is to be
performed (operator) (+,-,*,/)to be given through the command
buttons. When the user press the compute button result should be
displayed.
Do the validation for empty text box for numbers.
Handle “Division by Zero”
2 Write an applet program to accept the employee name, employee
number and basic salary as parameters. Find the gross and net
salaries on the following conditions
3 Using the swing components, design the frame for shopping a book
that accepts book code, book name and price. Calculate the discount
on code as follows.
Code
101
102
103
Any other 5%
Find the discount amount and Net bill amount. Display the bill
4 Using the Swing components, design the form for an electricity
office for accepting meter no. customer name, previous reading, and
current reading. Use data validation to see that current reading is
more than previous reading. Produce the bill in neat format.
Bill is calculates as follows.
Compute total number of units consumed and total amount to be paid
by each consumer the condition is
If unit consumed is <= 150 charge is 200.
For next 50 units Rs 1.50 per units.
For next 100 units charge = Rs 2.00 per unit.
For next additional units charge is Rs 3.00 per unit or Rs 500
whichever is maximum
Database table:
Telebill
custname Phone
Anil 65839282
Arjun 65312338
Ashwin 22848898
Deepak 23455555
Raj 57388399
Ram 22365786
Mysql
mysql -u root –p
CREATE DATABASE telephone;
USE telephone
CREATE TABLE telebill (custname VARCHAR(50), phone BIGINT));
SHOW TABLES;
INSERT INTO authors (id,name,email) VALUES(“Anil”,2345678987);
...............
SELECT * FROM telebill;
Program:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;
public class Telephone
{
/* static block is executed when a class is loaded into memory
* this block loads MySQL's JDBC driver
*/
static
{
try
{
Connection con = DriverManager.getConnection(connectionUrl, dbUser, dbPwd);
Statement st= con.createStatement();
System.out.print("Enter the first few characters of the name of the user:");
ch=in.readLine();
ResultSet rec=st.executeQuery("select * from telebill where custname like
'"+ch+"%'");
while(rec.next())
{
System.out.print(rec.getString("custname"));
System.out.println("\t"+rec.getString("phone"));
flag=1;
}
if(flag==0)
System.out.print("No record found");
}
catch (SQLException sqle)
{
System.out.println("SQL Exception thrown: " + sqle);
}
catch (IOException exp)
{
System.out.println("SQL Exception thrown: " + exp);
}
}
Output:
E:\Krishna>javac telephone.java
E:\Krishna>java telephone
Enter the first few characters of the name of the user: A
Arjun 65312338
Ashwin 22848898
Anil 65839282
E:\Krishna>java telephone
Enter the first few characters of the name of the user: ar
Arjun 65312338
E:\Krishna>java telephone
Enter the first few characters of the name of the user: Li
No record found
Database table:
Stud
stud_no name Clas mark1 mark2
101 Ajith First BCA 70 55
103 Tom Third BCA 89 97
104 Raj Second BCom 77 45
105 Reena Second BCA 56 90
Mysql
mysql -u root –p
CREATE DATABASE student;
USE telephone
CREATE TABLE stud (stud_no int, name varchar(50), clas varchar(20), mark1 int,
mark2 int));
SHOW TABLES;
INSERT INTO stud (stud_no,name,clas,mark1,mark2) VALUES(101,”Ajith”,”First
BCA”, 70, 55);
...............
SELECT * FROM stud;
Program:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;
public class Student
{
/* static block is executed when a class is loaded into memory
* this block loads MySQL's JDBC driver
*/
static
{
try
{
// loads com.mysql.jdbc.Driver into memory
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException cnf)
try
{
con= DriverManager.getConnection(connectionUrl, dbUser, dbPwd);
Statement st= con.createStatement();
do
{
i=0;
System.out.println("\nMENU\n1.Input student information\n2.Delete
specific student information\n3.Display\n4.Exit");
System.out.println("Enter your choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1: System.out.println("How many students you want to
enter?");
n=Integer.parseInt(in.readLine());
while(i<n)
{
System.out.println("Enter the student number:");
no=Integer.parseInt(in.readLine());
System.out.println("Enter the student name:");
name=in.readLine();
System.out.println("Enter the student class:");
clas=in.readLine();
System.out.println("Enter the 1st mark:");
m1=Integer.parseInt(in.readLine());
System.out.println("Enter the 2nd mark:");
m2=Integer.parseInt(in.readLine());
num=st.executeUpdate("insert into stud
values("+no+",'"+name+"','"+clas+"',"+m1+","+m2+")");
i++;
Output:
E:\Krishna>javac student.java
E:\Krishna>java student
MENU
1.Input student information
2.Delete specific student information
3.Display
4.Exit
Enter your choice:
3
Enter the student number whose details you want:
104
Student number=104
Student name=Raj
Student class=Second BCom
Student Mark1=77
Student Mark2=45
MENU
1.Input student information
2.Delete specific student information
3.Display
4.Exit
Enter your choice:
2
Enter the student number whose details you want to delete:
102
MENU
1.Input student information
2.Delete specific student information
3.Display
4.Exit
Enter your choice:
3
MENU
1.Input student information
2.Delete specific student information
3.Display
4.Exit
Enter your choice:
4
Interface Code:
import java.rmi.*;
public interface tempintf extends Remote
{
double ctof(double c)throws RemoteException;
double ftoc(double f)throws RemoteException;
}
Implementation code:
import java.rmi.*;
import java.rmi.server.*;
public class tempimp extends UnicastRemoteObject implements tempintf
{
public double ctof(double c)throws RemoteException
{
double f=c*180.0/100+32.0;
return f;
}
public double ftoc(double f)throws RemoteException
{
double c=(f-32)*100/180.0;
return c;
}
public tempimp() throws RemoteException{};
}
Server code:
import java.rmi.*;
import java.net.*;
public class tempserver
{
public static void main(String args[])
{
try
{
tempimp x=new tempimp();
Naming.rebind("tempserver",x);
}
catch(Exception e){}
}
}
Output:
E:\krishna>rmic tempimp
E:\krishna>rmiregistry &
E:\krishna>java tempserver
E:\krishna>java tempclient
TEMPERATURE CONVERSION
Enter the celsius value:38
Celsius to fahrenhit is:100.4
Enter the fahrenhit value:100.4
Fahrenhit to celsius is:38.0000008477105
Interface Code:
import java.rmi.*;
public interface taxintf extends Remote
{
double tax(double t)throws RemoteException;
}
Implementation code:
import java.rmi.*;
import java.rmi.server.*;
public class taximp extends UnicastRemoteObject implements taxintf
{
public double tax(double t)throws RemoteException
{
double tax=0;
if(t<10000)
tax=0;
if((t>=10000)&&(t<=25000))
tax=0.025*t;
if((t>=25000)&&(t<=100000))
tax=0.05*t;
if((t>=100000)&&(t<=1000000))
tax=0.075*t;
if(t>1000000)
tax=0.10*t;
return tax;
}
public taximp()throws RemoteException{};
}
Server code:
import java.rmi.*;
import java.net.*;
public class taxserver
{
public static void main(String args[])
{
try
{
taximp x=new taximp();
Naming.rebind("taxserver",x);
}
catch(Exception e)
Interface Code:
import java.rmi.*;
public interface simpintf extends Remote
{
double simpleint(double p,double t,double r)throws RemoteException;
}
Implementation code:
import java.rmi.*;
import java.rmi.server.*;
public class simpimp extends UnicastRemoteObject implements simpintf
{
public double simpleint(double p,double t,double r)throws
RemoteException
{
double f=p*t*r/100;
return f;
}
public simpimp() throws RemoteException{};
}
Server code:
import java.rmi.*;
import java.net.*;
public class simpserver
{
public static void main(String args[])
{
try
{
simpimp x=new simpimp();
Naming.rebind("simpserver",x);
}
catch(Exception e)
{
System.out.println("ERROR:"+e);
Program:
import java.io.*;
class s_r_f
{
public static void main(String args[])
{
try
{
String atr;
int i,n,num,sum=0,rem,rev;
int ch=1,n1,fib1,fib2,fib3;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("****");
System.out.println("MENU");
System.out.println("---------------");
System.out.println("\n1.Sum and Reverse \n 2. Fibonacci series");
System.out.println("__________________________");
while(ch<=2)
{
System.out.println("Enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1: System.out.println("Enter the number for Sum and
Reverse");
num=Integer.parseInt(br.readLine());
n=num;
rev=0;
sum=0;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
sum=sum+rem;
n=n/10;
}
System.out.println("The reverse is : "+rev);
System.out.println("The sum of is : "+sum);
break;
case 2: System.out.println("Enter the first number for
fibonacci series");
catch(Exception e) {}
}
}
Output:
E:\krishna>java s_r_f
****
MENU
---------------
1.Sum and Reverse
2. Fibonacci series
____________
Enter your choice
1
Enter the number for Sum and Reverse
121
The reverse is : 121
The sum of is : 4
Enter your choice
2
Enter the first number for fibonacci series
5
First n fibonacci series :
0 11 2 3
Program:
Bank.java:
import java.io.*;
class bank
{
String name;
int acc_no;
long balance;
bank(String name,int acc_no,long balance)
{
this.name=name;
this.acc_no=acc_no;
this.balance=balance;
}
void deposit(long amount)
{
balance=balance+amount;
}
void withdraw(long amount)
{
if(balance>amount)
{
if((balance-amount)>=1000)
{
balance-=amount;
System.out.println("Amount is successfully withdrawn");
}
else
System.out.println("Unsufficient balance(Minimum
balance=1000)");
}
else
System.out.println("Amount should be less than the
balance(Unsufficient balance)");
}
void display()
{
}
}
}
catch(Exception e){}
}
}
Output:
E:\jeslin>java bank_trans
***********************
Enter the account number:
101
Enter the name of account holder:
Ram
Enter the initial amount of deposit:
2000
*************************
1.Deposit
2.Withdrawal
3.Display
4.Exit
*************************
Enter your option:
1
Enter the amount to be deposited:
1000
*************************
1.Deposit
2.Withdrawal
3.Display
4.Exit
*************************
Enter your option:
3
Name:Ram
Balance is:3000
*************************
1.Deposit
2.Withdrawal
3.Display
4.Exit
*************************
Enter your option:
Program:
Emp1.java:
import java.io.*;
class emp1
{
String name;
int code;
String desig;
void getdata1()
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter employee name : ");
name=br.readLine();
System.out.println("Enter employee code : ");
code=Integer.parseInt(br.readLine());
System.out.println("Enter deisgnatiom : ");
desig=br.readLine();
}
catch(Exception e)
{}
}
void putdata1()
{
System.out.println("Name "+name);
System.out.println("Code :"+code);
System.out.println("Designation :"+desig);
}
}
class emp2 extends emp1
{
int acc_no;
String doj;
int pay;
void getdata2()
Output:
E:\Krishna>java employeebio
Enter employee name :
Raj
Enter employee code :
1001
Enter deisgnatiom :
Manager
Enter employee account number
2234
Enter employee DOJ:
14 May
Enter salary :
60000
Enter employee DA :
100
Enter employee HRA :
250
Enter employee CCA :
150
Enter employee PF :
300
Enter employee LIC :
210
Enter employee TAX :
130
EMPLOYEE DETAILS
______________________________
Name Raj
Code :1001
Designation :Manager
Account number : 2234
DOJ: 14 May
Salary: 60000
DA :100
HRA :250
CCA :150
PF :300
LIC :210
TAX :130
Gross pay : 59860
Program:
import java.io.*;
class sqr implements Runnable
{
int n, i;
sqr(int no)
{
n=no;
}
public void run()
{
for (i=1;i<=n;i++)
System.out.println("square of " + i + "is: " + (i*i));
System.out.println("\n");
}
}
class cube implements Runnable
{
int n, j;
cube(int no)
{
n = no;
}
public void run()
{
for(j=1;j<=n;j++)
System.out.println("cube of "+j+"is: "+(j*j*j));
System.out.println("\n");
}
}
class str implements Runnable
{
int n,k;
str(int no)
{
n=no;
}
public void run()
{
for(k=1;k<=n;k++)
Main program:
import pack1.*;
import pack2.*;
class simple
{
public static void main(String args[])
{
System.out.println("Program output");
System.out.println("*****************");
converter con = new converter();
System.out.println("Temperature conversion: ");
con.cf();
System.out.println("");
con.fc();
System.out.println("");
simpleinterest simp = new simpleinterest();
System.out.println("");
Output:
Program output
*****************
Temperature conversion:
Enter the value of centigrade:
34
Fahrenheit value of given centigrade is 93.2
Enter the value of fahrenheit:
93.2
Centigrade value of given fahrenheit is 34.0
Simple Interest:
enter the principal
1000
enter the time: 5
enter the rate of interest
3
principle is:1000.0
time is:5.0
interest is:3.0
simple interest is:150.0
Program:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class cal extends Applet implements ActionListener
{
int num1,num2;
int result=0;
String r;
TextField txt1;
TextField txt2;
Label l1;
Label l2;
Label l3;
Label l4;
Button plus;
Button minus;
Button mul;
Button div;
Button compute;
Button clear;
public void init()
{
txt1=new TextField();
txt2=new TextField();
l1=new Label("ENTER THE FIRST NUMBER");
l2=new Label("ENTER THE SECOND NUMBER");
l3=new Label("Result");
l4=new Label("");
plus=new Button("+");
minus=new Button("-");
mul=new Button("*");
div=new Button("/");
compute=new Button("Compute");
setLayout(null);
add(txt1);
add(txt2);
add(l1);
if(d.getSource()==minus)
{
result=num1-num2;
}
if(d.getSource()==mul)
}
if(d.getSource()==div)
{
try
{
result=num1/num2;
}
catch(ArithmeticException e1)
{
l4.setText("Division by zero is invalid");
}
}
if(d.getSource()==compute)
{
r=Integer.toString(result);
l4.setText(r);
}
}
catch(NumberFormatException e)
{
l4.setText("Invalid Number");
}
}
}
HTML file for applet:
<html>
<head>
<title>
Calculator
</title>
</head>
<body>
<applet code=cal.class
width=400
height=400>
</applet>
</body>
</html>
Output:
E:\krishna>appletviewer cal.html
Program:
import java.awt.*;
import java.applet.*;
public class employee extends Applet
{
String ename;
int enumber;
double bsalary;
double netsalary;
double grosssalary;
double da,hra,pf,pt;
public void init()
{
ename = getParameter("param1");
enumber = Integer.parseInt(getParameter("param2"));
bsalary = Double.parseDouble(getParameter("param3"));
calculate();
}
public void paint(Graphics g)
{
g.drawString("Employee Name is: " + ename,20,20);
g.drawString("Employee Number is: " + enumber,20,40);
g.drawString("Basic Salary is: " + bsalary,20,60);
g.drawString("DA is: " + da,20,80);
g.drawString("HRA is: " + hra,20,100);
g.drawString("Gross Salary is: " + grosssalary,20,120);
g.drawString("PF is: " + pf,20,140);
g.drawString("PT is: " + pt,20,160);
g.drawString("Net Pay is: " + netsalary,20,180);
}
public void calculate()
{
if (bsalary <= 20000)
{
da=.40*bsalary;
hra=.10*bsalary;
grosssalary=da+hra;
pf=.12*grosssalary;
pt=100;
netsalary=grosssalary-(pf+pt);
}
Program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Shoppingbook1 implements ActionListener
{
JLabel lcode;
JLabel lname;
JLabel lprice;
JLabel ldiscount;
JLabel lnetbill;
JTextField tcode;
JTextField tname;
JTextField tprice;
JTextField tdiscount;
JTextField tnetbill;
JButton bill;
Shoppingbook1 ( )
{
JFrame jfrm=new JFrame("BOOK SHOPPING BILL");
jfrm.setLayout(null);
jfrm.setSize(300,300);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lcode = new JLabel("BooK Code:");
tcode= new JTextField ( );
lname = new JLabel("Book Name:");
tname = new JTextField ( );
lprice = new JLabel("Book Price:");
tprice = new JTextField ( );
ldiscount = new JLabel("Discount: ");
tdiscount = new JTextField ( );
lnetbill = new JLabel("Final Price:");
tnetbill = new JTextField ( );
bill=new JButton("Bill");
lcode.setBounds(20,20,80, 10);
Java Practical Manual Page 44 of 49
Srinivas Institute of Management Studies BCA-V Semester
tcode.setBounds(100,18,100,20);
lname.setBounds(20,50,80,10);
tname.setBounds(100,48,100,20);
lprice.setBounds(20,80,80,10);
tprice.setBounds(100,78,100,20);
ldiscount.setBounds(20,110,80,10);
tdiscount.setBounds(100,108,100,20);
lnetbill.setBounds(20,140,80,10);
tnetbill.setBounds(100,138,100,20);
bill.setBounds(100,180,60,30);
jfrm.add(lcode);
jfrm.add(tcode);
jfrm.add(lname);
jfrm.add(tname);
jfrm.add(lprice);
jfrm.add(tprice);
jfrm.add(ldiscount);
jfrm.add(tdiscount);
jfrm.add(lnetbill);
jfrm.add(tnetbill);
bill.addActionListener(this);
jfrm.add(bill);
jfrm.setVisible(true);
tdiscount.setEditable(false);
tnetbill.setEditable(false);
}
public void actionPerformed(ActionEvent ae)
{
double discount;
String discountamt,netbillamt;
double price;
double netbill;
try
{
int codeval=Integer.parseInt(tcode.getText());
if (codeval==101)
discount=15;
else if(codeval==102)
discount=20;
else if(codeval==103)
discount=25;
else
discount=5;
discountamt=Double.toString(discount);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable ( ) {
public void run() {
new Shoppingbook1( );
}
});
}
}
Output:
E:\Krishna>java Shoppingbook1
Program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
Electricity1( )
{
JFrame jfrm=new JFrame("ELECTRICITY BILL");
jfrm.setLayout(null);
jfrm.setSize(300,300);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lmeterno = new JLabel("Meter No: ");
tmeterno= new JTextField ( );
lcname = new JLabel("Customer Name:");
tcname = new JTextField ( );
lpreading = new JLabel("Previous Reading:");
}
catch(NumberFormatException e)
{
tamount.setText("Invalid Number");
}
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable ( ) {
public void run() {
new Electricity1( );
}
}
);
}
}
Output:
E:\Krishna>java Electricity1