0% found this document useful (0 votes)
11 views44 pages

Advanced Java Programming Lab

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views44 pages

Advanced Java Programming Lab

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

LAB ON ADVANCED JAVA SRI RAKSHA

3SU21SA135

INDEX

PART-A
SL NO DESCRIPTION PAGE NO
1 Write an applet program in Java to draw a 3-4
face
2 Write an applet program in Java to implement 5-9
a simple calculator
3 Write a Java program to copy a characters 10-11
from one to another
4 Write a Java program for reading and writing 12-13
a Random Access File
5 Write an applet program in Java to accept 14-16
employee number, name and basic as
parameter
6 Write a Java program using swing 17-20
component. Design a frame to accept a book
id, book code, book name and price
7 Write a Java program using swing 21-25
component. Design a frame to generate
electricity bill. Accept the meter number,
customer name, previous meter reading,
current meter reading.

PART-B
1 Write a JDBC program to process a bank 27-29
transaction
2 Write a JDBC program to retrieve the details 30-32
from telephone directory
3 Write a RMI program to convert temperature 33-35
to Fahrenheit and vice versa
4 Write a RMI program to calculate interest on 36-38
a particular amount
5 Write a RMI program to calculate Tax 39-41
depending on a salary
6 Write a RMI program to convert dollar to 42-44
rupees using command line argument

1
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

PART-A

2
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

PROGRAM-01
Write an applet program in Java to draw a face.
import [Link].*;
import [Link].*;
public class face extends Applet
{
public void paint(Graphics g)
{
setForeground([Link]);
[Link](40,40,120,150);
[Link](57,75,30,20);
[Link](110,75,30,20);
[Link](68,81,10,10);
[Link](121,81,10,10);
[Link](85,100,30,30);
[Link](60,125,80,40,180,180);
[Link](25,92,15,30);
[Link](160,92,15,30);
}
}

HTML CODE:
<html>
<body>
<applet code=”[Link]”
Height=”500”;
Weight=”500”;>
</applet>
</body>
</html>

3
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

OUTPUT

4
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

PROGRAM-02
Write an applet program in Java to implement a simple calculator
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class calculator extends Applet implements ActionListener
{
int n1,n2;
int res=0;
String str;
TextField txt1;
TextField txt2;
Label l1;
Label l2;
Label l3;
Label l4;
Button plus;
Button sub;
Button mul;
Button div;
public void init()
{
txt1=new TextField();
txt2=new TextField();
l1=new Label("enter 1st number");
l2=new Label("enter 2nd number");
l3=new Label("result");
l4=new Label("");
plus=new Button("ADD");

5
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

sub=new Button("MINUS");
mul=new Button("PRODUCT");
div=new Button("DIVIDE");
setLayout(null);
add(txt1);
add(txt2);
add(l1);
add(l2);
add(l3);
add(l4);
add(plus);
add(sub);
add(mul);
add(div);
[Link] (300,20,80,20);
[Link](300,50,80,20);
[Link](100,20,200,25);
[Link](100,40,200,25);
[Link](100,150,80,25);
[Link](200,150,100,25);
[Link](100,80,40,20);
[Link](200,80,40,20);
[Link](300,80,60,20);
[Link](400,80,60,20);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
repaint();
}

6
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

public void actionPerformed(ActionEvent d)


{
try
{
n1=[Link]([Link]());
n2=[Link]([Link]());
if([Link]()==plus)
{
res=n1+n2;
str=[Link](res);
[Link](str);
}
if([Link]()==sub)
{
res=n1-n2;
str=[Link](res);
[Link](str);
}
if([Link]()==mul)
{
res=n1*n2;
str=[Link](res);
[Link](str);
}
if([Link]()==div)
{
try
{
res=n1/n2;
str=[Link](res);

7
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

[Link](str);
}
catch(ArithmeticException e)
{
[Link]("error:Division by zero");
}
}
}
catch(NumberFormatException e)
{
[Link]("Invalid number");
}
}
}

HTML CODE:
<html>
<applet code=”[Link]” width=250 height=200>
</applet>
</html>

OUTPUT

8
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

9
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

PROGRAM-03
Write a Java program to copy a characters from one to another
import [Link].*;
class copycharacter
{
public static void main(String args[])
{
File infile=new File("[Link]");
File outfile=new File("[Link]");
FileReader ins=null;
FileWriter outs=null;
try
{
ins=new FileReader(infile);
outs=new FileWriter(outfile);
int ch;
while((ch=[Link]())!=-1)
{
[Link](ch);
}
}
catch(IOException e)
{
[Link](e);
[Link](-1);
}
finally
{
try
{

10
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

[Link]();
[Link]();
}
catch(IOException e)
{}
}
}
}
OUTPUT

11
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

PROGRAM-04
Write a Java program for reading and writing a Random Access File
import [Link].*;
public class Random
{
public static void main(String args[])
{
RandomAccessFile file=null;
try{
file=new RandomAccessFile("[Link]","rw");
[Link]('X');
[Link](555);
[Link](3.1412);
[Link](0);
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link](2);
[Link]([Link]());
[Link]([Link]());
[Link](false);
[Link](4);
[Link]([Link]());
}
catch(IOException e)
{
[Link](e);
}
finally
{

12
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

try
{
[Link]();
}
catch(IOException e)
{}
}
}
}
OUTPUT

13
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

PROGRAM-05
Write an applet program in Java to accept employee number, name and basic as
parameter. Find the gross and net salary depending on the following conditions:
If basic>20000 then DA-50% , HRA-15%, PF-12%, PT-200
If basic<=20000 then DA-40% , HRA-10%, PF-12%, PT-100
Gross=basic+da+hra
Net=gross-(PT+PF)
import [Link].*;
import [Link].*;
public class employee extends Applet
{
String ename;
int ecode;
double basic;
double net;
double gross;
double da,hra,pt,pf;
public void init()
{
ename=getParameter("Param 1");
ecode=[Link](getParameter("Param 2"));
basic=[Link](getParameter("Param 3"));
calculate();
}
public void paint(Graphics g)
{
[Link]("basic salary:"+basic,20,60);
[Link]("DA:"+da,20,80);
[Link]("HRA:"+pf,20,100);
[Link]("PF:"+pf,20,120);
[Link]("PT:"+pt,20,140);
14
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

[Link]("Gross Salary:"+gross,20,160);
[Link]("Net Salary:"+net,20,180);
}
public void calculate()
{
if(basic<=20000)
{
da=0.4*basic;
hra=0.1*basic;
gross=basic+da+hra;
pf=0.12*basic;
pt=100;
net=gross-(pf-pt);
}
if(basic>20000)
{
da=0.5*basic;
hra=0.15*basic;
gross=basic+da+hra;
pf=0.12*basic;
pt=200;
net=gross-(pf-pt);
}
}
}

15
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

HTML CODE:
<html>
<body>
<applet code=”[Link]” height=500 width=500>
<param name=”param1” value=”Raksha”></param>
<param name=”param2” value=”1001”></param>
<param name=”param3” value=”90000”></param>
</applet>
</body>
</html>

OUTPUT:

16
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

PROGRAM-06

Write a Java program using swing component. Design a frame to accept a


book id, book code, book name and price. Calculate the discount based on
the following condition.
BOOK CODE DISCOUNT
A 15%
B 20%
C 25%
Any other 5%
Calculate the discount and display the bill.
import [Link].*;
import [Link].*;
import [Link].*;
public class book implements
ActionListener
{
JLabel Icode;
JLabel Iname;
JLabel Iprice;
JLabel Idisc;
JLabel Ibill;
JTextField tcode;
JTextField tname;
JTextField tprice;
JTextField tdisc;
JTextField tbill;
JButton bill;
book()
{
JFrame jfrm=new JFrame("Book Shop");

17
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

[Link](null);
[Link](300,300);
[Link](JFrame.EXIT_ON_CLOSE);
Icode=new JLabel("BOOk Code");
tcode=new JTextField();
Iname=new JLabel("Book Name");
tname=new JTextField();
Iprice=new JLabel("Book Price");
tprice=new JTextField();
Idisc=new JLabel("Discount");
tdisc=new JTextField();
Ibill=new JLabel("Net Price");
tbill=new JTextField();
bill=new JButton("BILL");
[Link](100,25,100,25);
[Link](100,50,100,25);
[Link](100,75,100,25);
[Link](100,100,100,25);
[Link](100,125,100,25);
[Link](200,25,100,25);
[Link](200,50,100,25);
[Link](200,75,100,25);
[Link](200,100,100,25);
[Link](200,125,100,25);
[Link](200,200,125,25);
[Link](Icode);
[Link](Iname);
[Link](Iprice);
[Link](Idisc);
[Link](Ibill);

18
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

[Link](tcode);
[Link](tname);
[Link](tprice);
[Link](tdisc);
[Link](tbill);
[Link](bill);
[Link](this);
[Link](true);
[Link](false);
[Link](false);
}
public void actionPerformed(ActionEvent d)
{
double discount;
String discant,billamt;
int price;
double netbill;
try
{
if([Link]().equals("A"))discount=15;
else if([Link]().equals("B"))discount=20;
else if([Link]().equals("C"))discount=25;
else discount=5;
discant=[Link](discount);
if(!([Link]().equals(""))&&
!([Link]().equals(""))&&
!([Link]().equals("")))
[Link](discant +"%");
price=[Link]([Link]());
netbill=price-(price*discount)/100;

19
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

billamt=[Link](netbill);
[Link](billamt);
}
catch(NumberFormatException e)
{
[Link]("Invalid Number!!!");
}
}
public static void main(String args[])
{
new book();
}
}

OUTPUT:

20
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

PROGRAM-07
Write a Java program using swing component. Design a frame to generate electricity
bill. Accept the meter number, customer name, previous meter reading, current meter
reading. Use data validation to check whether the current meter reading is greater than
the previous meter reading. Produce a bill in neat format. Calculate the bill based on
consumption as follows:
Modules<150-Rs.200
For next 50 Modules
Rs.1.50/Module For next 100
Modules Rs.2.00/Module
For next additional Modules Rs.3.00/Module or Rs.500 whichever is maximum.
import [Link].*;
import [Link].*;
import [Link].*;
public class ElectricBill implements
ActionListener
{
JLabel Imno;
JLabel Icname;
JLabel Ipmr;
JLabel Icmr;
JLabel Iunit;
JLabel Iamt;
JTextField tmno;
JTextField tcname;
JTextField tpmr;
JTextField tcmr;
JTextField tunit;
JTextField tamt;
JButton bill;
ElectricBill()
{

21
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

JFrame jfrm=new JFrame("Electricity Bill");


[Link](null);
[Link](300,300);
[Link](JFrame.EXIT_ON_CLOSE);
Imno=new JLabel("Meter number:");
tmno=new JTextField();
Icname=new JLabel("Customer name:");
tcname=new JTextField();
Ipmr=new JLabel("Previous Meter reading:");
tpmr=new JTextField();
Icmr=new JLabel("Current Meter reading:");
tcmr=new JTextField();
Iunit=new JLabel("Unit consumed:");
tunit=new JTextField();
Iamt=new JLabel("Total Amount:");
tamt=new JTextField();
bill=new JButton("BILL");
[Link](10,25,200,25);
[Link](10,50,200,25);
[Link](10,75,200,25);
[Link](10,100,200,25);
[Link](10,125,200,25);
[Link](10,150,200,25);
[Link](200,25,200,25);
[Link](200,50,200,25);
[Link](200,75,200,25);
[Link](200,100,200,25);
[Link](200,125,200,25);
[Link](200,150,200,25);
[Link](200,200,120,25);

22
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

[Link](Imno);
[Link](tmno);
[Link](Icname);
[Link](tcname);
[Link](Ipmr);
[Link](tpmr);
[Link](Icmr);
[Link](tcmr);
[Link](Iunit);
[Link](tunit);
[Link](Iamt);
[Link](tamt);
[Link](bill);
[Link](this);
[Link](true);
[Link](false);
}
public void actionPerformed(ActionEvent d)
{
int unit, diff, cmr, pmr;
String net=null;
double charge=0;
try
{
pmr=[Link]([Link]());
cmr=[Link]([Link]());
if(cmr>pmr)
{
unit=cmr-pmr;
if(unit<=150)

23
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

charge=200;
else
if(unit>150&&unit<=200)
{
diff=unit-150;
charge=200+(diff*1.5);
}
else
if(unit>200&&unit<=300)
{
diff=unit-200;
charge=200+(unit*2);
}
else
{
diff=unit-150;
charge=[Link](200+diff*3,500);
}
net=[Link](charge);
[Link]([Link](unit));
[Link](net);
}
else
[Link]("Invaild reading");
}
catch(NumberFormatException e)
{
[Link]("Invalid Number!!!");
}
}

24
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

public static void main(String args[])


{
new ElectricBill();
}
}

OUTPUT:

25
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

PART-B

PROGRAM-01
Write a JDBC program to process a bank transaction
import [Link];

26
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
public class Bank
{
static
{
try
{
[Link]("[Link]");
}
catch(ClassNotFoundException cnf)
{
[Link]("Driver could not be loaded"+cnf);
}
}
public static void main(String args[])
{
try
{
String ConnectionUrl="jdbc:mysql://localhost:3307/ashish";
String dbuser="root";
String pswd="password";
DataInputStream in=new DataInputStream ([Link]);
String sql;
int vaccno,flag=0;
try
{

27
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

Connection con =
[Link](ConnectionUrl,dbuser,pswd);
Statement st=[Link]();
[Link]("enter account number");
vaccno=[Link]([Link]());
sql="select * from account where accno ="+vaccno;
ResultSet rec=[Link](sql);
while([Link]())
{
[Link]([Link]("accno"));
[Link]("\t"+[Link]("name"));
[Link]("\t"+[Link]("bal"));
flag=1;
}
if(flag==0)
{
[Link]("no record found");
}
}
catch(SQLException e1)
{
[Link]("SQL Exception:"+e1);
}
}
catch(IOException e2)
{
[Link]("IOException:"+e2);
}
}
}
OUTPUT:
28
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

PROGRAM-02
Write a JDBC program to retrieve the details from telephone directory
import [Link];

29
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
public class Telephone
{
static
{
try
{
[Link]("[Link]");
}
catch(ClassNotFoundException cnf)
{
[Link]("Driver could not be loaded"+cnf);
}
}
public static void main(String args[])
{
try
{
String ConnectionUrl="jdbc:mysql://localhost:3306/ashish";
String dbuser="root";
String pswd="password";
DataInputStream in=new DataInputStream ([Link]);
String ch;
int flag=0;
try{
Connection con = [Link](ConnectionUrl,dbuser,pswd);

30
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

Statement st=[Link]();
[Link]("Enter a few character of user name");
ch=[Link]();
ResultSet rec=[Link]
("select * from tele where cname like'"+ch+"%'");
while([Link]())
{
[Link]([Link]("cname"));
[Link]("\t"+[Link]("phone"));
flag=1;
}
if(flag==0)
{
[Link]("no record found");
}
}
catch(SQLException e1)
{
[Link]("SQL Exception:"+e1);
}
}
catch(IOException e2)
{
[Link]("IOException:"+e2);
}
}
}
OUTPUT:

31
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

PROGRAM-03

32
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

Write a RMI program to convert temperature to Fahrenheit and vice versa


Interface:
import [Link].*;
public interface Tempintf extends Remote
{
double cen_fah(double c) throws RemoteException;
double fah_cen(double f) throws RemoteException;
}

Implementation:
import [Link].*;
import [Link].*;
public class TempConvert extends UnicastRemoteObject implements Tempintf
{
public double cen_fah(double c) throws RemoteException
{
double f=c*1.8+32;
return f;
}
public double fah_cen(double f) throws RemoteException
{
double c=(f-32)/1.8;
return c;
}
public TempConvert()throws RemoteException {};
}

Server:
import [Link].*;
import [Link].*;

33
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

public class TempServer


{
public static void main(String args[])
{
try
{
TempConvert t=new TempConvert();
[Link]("TempServer",t);
}
catch(Exception e)
{}
}
}

Client:
import [Link].*;
import [Link].*;
public class TempClient
{
public static void main(String args[])
{
try
{
DataInputStream in=new DataInputStream([Link]);
Tempintf t=(Tempintf)[Link]("rmi://localhost/TempServer");
[Link]("Temperature converion");
[Link]("Enter celsius Temperature");
float c=[Link]([Link]());
[Link]("Fehrenhit value is:"+t.cen_fah(c));
[Link]("Enter Fahrenhit Temperature");

34
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

float f=[Link]([Link]());
[Link]("celsius value is:"+t.fah_cen(f));
}
catch(Exception e)
{}
}
}

OUTPUT:

PROGRAM-04

35
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

Write a RMI program to calculate interest on a particular amount


Interface:
import [Link].*;
public interface Simpleintf extends Remote
{
double simplecal(double p,double t,double r)throws RemoteException;
}

Implementation:
import [Link].*;
import [Link].*;
public class Simpleimp extends UnicastRemoteObject implements Simpleintf
{
public double simplecal(double p,double t, double r)throws RemoteException
{
double si=(p*t*r)/100;
return si;
}
public Simpleimp() throws RemoteException {};
}

Server:
import [Link].*;
import [Link].*;
public class SimpleServer
{
public static void main(String args[])
{
try
{

36
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

Simpleimp s=new Simpleimp();


[Link]("SimpleServer",s);
}
catch(Exception e)
{}
}
}

Client:
import [Link].*;
import [Link].*;
public class SimpleClient
{
public static void main(String args[])
{
try
{
DataInputStream in=new DataInputStream ([Link]);
Simpleintf s=(Simpleintf)
[Link]("rmi://localhost/SimpleServer");
[Link]("Simple Interest");
[Link]("Enter Principle Amount:");
double p=[Link]([Link]());
[Link]("Enter Time:");
double t=[Link]([Link]());
[Link]("Enter Rate of Interest:");
double r=[Link]([Link]());
[Link]("Simple Interest is:"+[Link](p,t,r));
}
catch(Exception e)
{}
37
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

}
}

OUTPUT:

PROGRAM-05
Write a RMI program to calculate Tax depending on a salary

38
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

Interface:
import [Link].*;
public interface Taxintf extends Remote
{
double Taxcal(double t) throws RemoteException;
}

Implementation:
import [Link].*;
import [Link].*;
public class Taximp extends UnicastRemoteObject implements Taxintf
{
public double Taxcal (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.1*t;
return tax;
}
public Taximp()throws RemoteException{};
}

39
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

Server:
import [Link].*;
import [Link].*;
public class Taxserver
{
public static void main(String args[])
{
try
{
Taximp x=new Taximp();
[Link]("Taxserver",x);
}
catch(Exception e)
{}
}
}

Client:
import [Link].*;
import [Link].*;
public class Taxclient
{
public static void main(String args[])
{
try
{
DataInputStream in=new DataInputStream([Link]);
Taxintf t=(Taxintf)[Link]("rmi://localhost/Taxserver");
[Link]("enter Salary:");
double s=[Link]([Link]());

40
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

double it=[Link](s);
if(it==0)
{
[Link]("salary less than 10000");
[Link]("No Tax Deduction");
[Link](0);
}
[Link]("Tax is"+it);
}
catch(Exception e)
{}
}
}

OUTPUT:

PROGRAM-06
Write a RMI program to convert dollar to rupees using command line argument

41
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

Interface:
import [Link].*;
public interface Dolintf extends Remote
{
double dollar (double i)throws RemoteException;
}

Implementation:
import [Link].*;
import [Link].*;
public class DolConvert extends UnicastRemoteObject implements Dolintf
{
public double dollar (double i)throws RemoteException
{
double d=i*74;
return d;
}
public DolConvert()throws RemoteException{};
}

Server:
import [Link].*;
import [Link].*;
public class DolServer
{
public static void main(String args[])
{
try
{
DolConvert x=new DolConvert();

42
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

[Link]("DolServer",x);
}
catch(Exception e)
{}
}
}

Client:
import [Link].*;
import [Link].*;
public class DolClient
{
public static void main(String args[])
{
try
{
DataInputStream in=new DataInputStream([Link]);
Dolintf x=(Dolintf)[Link]("rmi://localhost/DolServer");
[Link]("dollar conversion");
[Link]("enter dollar");
double i=[Link]([Link]());
[Link]("value dollars in rupee is:"+[Link](i));
}
catch(Exception e)
{}
}
}

OUTPUT:

43
LAB ON ADVANCED JAVA SRI RAKSHA
3SU21SA135

44

Common questions

Powered by AI

The Java program using JDBC retrieves data from a bank database by establishing a connection using the DriverManager and executing a SQL query to fetch account details based on user input for the account number. It utilizes the ResultSet to process the retrieved data. The program handles errors through try-catch blocks, catching SQLExceptions to provide feedback if database operations fail, and IOException for input-output errors, ensuring robust error handling during database interactions .

The Java Swing program calculates discounts by accepting book details like code and price through JTextField components. The discount percentage is determined based on predefined conditions: 15% for book code A, 20% for B, 25% for C, and 5% for others. Using ActionListener, net bill amount is calculated by subtracting the discount from the initial price. The JLabel components update the display to show the calculated net price and discount percentage once the calculation is triggered by the user interface .

In the Java Swing frame designed for generating an electricity bill, input validation involves checking that the current meter reading exceeds the previous one. This is achieved in the actionPerformed method by parsing integer values from the JTextField inputs and comparing the readings. In the event of validation failure, an error message 'Invalid reading' is set to notify the user. This ensures data accuracy before the bill is calculated using specified consumption rates .

In the Java applet program implementing a calculator, error handling is performed using try-catch blocks to handle exceptions like NumberFormatException when input conversion fails and ArithmeticException for division by zero errors. User input validation involves parsing integers from text fields and checking conditions before operations, ensuring the display of error messages like 'Invalid number' or 'error: Division by zero' when applicable, thereby preventing application crashes and maintaining user resilience .

The RMI process for converting temperature between Celsius and Fahrenheit involves several components: an interface (Tempintf) defining the remote methods cen_fah and fah_cen for conversion, an implementation class (TempConvert) that performs the conversion operations, a server class (TempServer) binding the implementation to RMI registry, and a client class (TempClient) interfacing with the server. The client inputs temperature values and retrieves converted values by invoking remote methods, leveraging RMI for remote operation execution .

The Java program employs FileReader and FileWriter for reading characters from one file and writing to another. It utilizes a while loop to read characters until the end of the file is reached. Error handling is implemented using try-catch-finally blocks, where IOException is caught and handled to prevent disruptions from file read/write errors. Finally, resources are closed in a finally block to ensure proper release and to avoid potential memory leaks .

The Java applet program calculates the gross and net salary by accepting parameters like the employee's basic salary, name, and employee number. The calculation includes conditional logic: if the basic salary is more than 20,000, DA is 50%, HRA is 15%, PF is 12%, and PT is 200. If basic is 20,000 or less, DA is 40%, HRA is 10%, PF is 12%, and PT is 100. The gross salary is computed as the sum of basic, DA, and HRA, while the net salary is determined by subtracting PF and PT from the gross salary .

The calculation logic in the Java RMI program for tax determination is as follows: No tax is applied for salaries up to 10,000. A salary above 10,000 and up to 25,000 incurs a 2.5% tax. For salaries over 25,000 up to 100,000, a 5% tax is applied. Salaries between 100,000 and 1,000,000 incur a 7.5% tax, and salaries over 1,000,000 incur a 10% tax. This logic is implemented to progressively apply a higher tax rate with increasing salary brackets .

In Java, a Random Access File is used to enable both read and write operations at specific file locations. The program creates a RandomAccessFile instance, writes various data types at different offsets using methods like writeChar, writeInt, and writeDouble, and employs seek() to navigate to these positions for reading using corresponding read methods. This enables precise control over file data, allowing non-sequential access patterns, which is crucial for applications requiring fixed file positions .

The key design elements of the Swing-based Java program to generate an electricity bill include JFrame for the window setup, JLabel for input fields like meter number, customer name, and meter readings, JTextField for user data entry, and JButton for triggering the bill calculation. It also features validation to ensure the current meter reading is greater than the previous reading and calculates the bill based on unit consumption employing a tiered billing structure. This design ensures user inputs are structured and the interaction is facilitated through a graphical interface .

You might also like