Program -1
Write a program to store customer detail as Acno, Name, Balance in
a class. Compute Interest as 8% of Balance >= 75000, Interest as
6% if Balance >= 40000 else 3%. Display detail for the customer.
Source Code:
package javaapplication2;
import [Link].*;
class Customer
{
int ac, bal;
String nm;
double interest;
Customer( )
{
Scanner in = new Scanner([Link]);
[Link]("Enter Account no ");
ac = [Link]( );
[Link]( ); // To clear the buffer
[Link]("Enter name");
nm = [Link]( );
[Link]("Enter Balance ");
bal = [Link]( );
}
void compute( )
{
if(bal >= 75000)
interest = bal * .08;
else
if(bal >= 40000)
interest = bal * .06;
1
else
interest = bal * .03;
}
void putdata( )
{
[Link]("Acno = " + ac);
[Link]("Name = " + nm);
[Link]("Balance = " + bal);
[Link]("Interest = " + interest);
}
}
public class JavaApplication2 {
public static void main(String[] args) {
Customer cust = new Customer( );
[Link]( );
[Link]( );
}
}
2
Output
3
Program -2
Write a program to accept 2 numbers. Divide 1st no. by 2nd &
display the result. Display user defined error message if 2nd no. is
zero.
Source Code:
package javaapplication3;
import [Link].*;
public class JavaApplication3{
public static void main(String[] args) {
{
int a, b, c;
Scanner in = new Scanner ([Link]);
[Link]("Enter 1st number ");
a = [Link]( );
[Link]("Enter 2nd number ");
b = [Link]( );
try
{
c = a / b;
[Link](" Result = " + c);
}
catch(ArithmeticException e)
{
[Link]("Divide by zero error");
}
}
}
4
Output
5
Program -3
Write a program to read data from disk file.
Source Code:
6
Output
7
Program -4
Write a program to illustrate method overloading.
Source Code:
package javaapplication4;
class Sample
{
void check(int n)
{
if(n%2 == 0)
[Link]("No. is even");
else
[Link]("No. is odd");
}
void check(int a, int b)
{
if(a%b == 0)
[Link]("1st no. is div. by 2nd ");
else
[Link]("Not divisible");
}
}
public class JavaApplication4 {
public static void main(String[] args) {
Sample obj = new Sample( );
[Link](8, 2);
[Link](7);
}
}
8
Output
9
Program -5
Write a program to illustrate method overriding.
Source Code:
package javaapplication5;
class A {
void display( )
{
[Link](" This is class A ");
}
}
class B extends A {
void display( )
{
[Link]( );
[Link](" This is class B ");
}
}
public class JavaApplication5 {
public static void main(String[] args) {
B objb = new B( );
[Link]( );
}
}
10
Output
11
Program -6
Write a program to illustrate various forms of Inheritance.
Source Code:
SINGLE INHERITANCE:
package javaapplication81;
class A
{
int a;
void getdata(int x)
{
a=x;
}
void putdata()
{
[Link]("a = " +a);
}
}
class B extends A
{
int b,c;
void accept(int y)
{
b=y;
}
void display()
{
c=a+b;
12
[Link]("Sum = "+c);
}
}
public class JavaApplication81 {
public static void main(String[] args) {
B obj = new B();
[Link](7);
[Link](5);
[Link]();
[Link]();
}
}
13
Output
14
MULTILTVEL INHERITANCE:
package javaapplication81;
class A
{
int a;
void getdata(int x)
{
a=x;
}
void putdata()
{
[Link]("a = " +a);
}
}
class B extends A
{
int b;
void accept(int y)
{
b=y;
}
void display()
{
[Link]("b = "+b);
}
}
class C extends B
15
{
int c,d;
void contain(int z)
{
c=z;
}
void show()
{
d=a+b+c;
[Link](" Sum = "+d);
}
}
public class JavaApplication81 {
public static void main(String[] args) {
C obj = new C();
[Link](7);
[Link](5);
[Link](8);
[Link]();
[Link]();
[Link]();
}
}
16
Output
17
MULTIPLE INHERITANCE:
package javaapplication85;
interface Product1
{
int pc = 101;
String nm = "abc";
void putdata();
}
class Product2
{
int qty;
double up;
void getdata(int q, double u)
{
qty = q;
up = u;
}
void display()
{
[Link]("Quantity = "+qty);
[Link]("Unit Price = "+up);
}
}
class Product extends Product2 implements Product1
{
double amt;
void compute()
18
{
amt = qty * up;
}
void show()
{
[Link]("Amount = "+amt);
}
public void putdata()
{
[Link]("Product code = "+pc);
[Link]("Product name = "+nm);
}
}
public class JavaApplication85 {
public static void main(String[] args) {
Product P = new Product();
[Link](10,1000);
[Link]();
[Link]();
[Link]();
[Link]();
19
Output
20
HIERARICHAL INHERITANCE:
package javaapplication85;
abstract class Shape
{
double a,b;
void getdata(double x, double y)
{
a = x;
b = y;
}
abstract void area();
}
class Rectangle extends Shape
{
double ar1;
void area()
{
ar1 = a*b;
[Link]("Area of reactangle = " +ar1);
}
}
class Triangle extends Shape
{
double ar2;
void area()
{
ar2 = 0.5*a*b;
21
[Link]("Area of triangle = " +ar2);
}
}
public class JavaApplication85 {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
[Link](7.5,6.1);
[Link]();
[Link](5.2, 6.3);
[Link]();
}
}
22
Output
23
Program -7
Write a program to display message Hello World on the Applet
window.
Source Code:
24
Output
25
Program -8
Write a program to demonstrate layout manager for positioning
various swing controls.
Source Code:
import [Link];
public class NewJFrame extends [Link] {
public NewJFrame() {
initComponents();
}
private void initComponents()
jLabel1 = new [Link]();
jButton1 = new [Link]();
jLabel2 = new [Link]();
tf1 = new [Link]();
pf1 = new [Link]();
setDefaultCloseOperation([Link].EXIT_ON_CLOSE);
[Link]("username");
[Link]("Submit");
[Link](new [Link]() {
public void actionPerformed([Link] evt) {
jButton1ActionPerformed(evt);
}
});
[Link]("Password");
[Link] layout = new [Link](getContentPane());
getContentPane().setLayout(layout);
[Link](
26
[Link]([Link])
.addGroup([Link]()
.addGroup([Link]([Link])
.addGroup([Link]()
.addGap(97, 97, 97)
.addComponent(jButton1))
.addGroup([Link]()
.addGap(31, 31, 31)
.addGroup([Link]([Link]
NG, false)
.addGroup([Link]()
.addComponent(jLabel2)
.addGap(41, 41, 41)
.addComponent(pf1))
.addGroup([Link]()
.addComponent(jLabel1, [Link].PREFERRED_SIZE, 65,
[Link].PREFERRED_SIZE)
.addPreferredGap([Link]
)
.addComponent(tf1, [Link].PREFERRED_SIZE, 86,
[Link].PREFERRED_SIZE)))))
.addContainerGap(208, Short.MAX_VALUE))
);
[Link](
[Link]([Link])
.addGroup([Link]()
.addGap(19, 19, 19)
.addGroup([Link]([Link])
27
.addComponent(jLabel1, [Link].PREFERRED_SIZE, 28,
[Link].PREFERRED_SIZE)
.addComponent(tf1, [Link].PREFERRED_SIZE,
[Link].DEFAULT_SIZE, [Link].PREFERRED_SIZE))
.addPreferredGap([Link])
.addGroup([Link]([Link])
.addComponent(jLabel2)
.addComponent(pf1, [Link].PREFERRED_SIZE,
[Link].DEFAULT_SIZE, [Link].PREFERRED_SIZE))
.addGap(42, 42, 42)
.addComponent(jButton1)
.addContainerGap(162, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed([Link] evt) {
String nm,pass;
nm = [Link]();
pass = [Link]();
if([Link]("System")&& [Link]("Manager"))
[Link](null, "Authorized user");
else
[Link](null, "unauthorized user");
}
public static void main(String args[]) {
28
[Link](new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private [Link] jButton1;
private [Link] jLabel1;
private [Link] jLabel2;
private [Link] pf1;
private [Link] tf1;
// End of variables declaration
}
29
Output
30
Program -9
Write a program to display detail for all the Employees from the
Emp table (Empno, Ename, Job, Sal)in a Java program using JDBC
concept..
Source Code:
package javaapplication1;
import [Link].*;
public class JavaApplication1 {
public static void main(String[] args)
throws SQLException, ClassNotFoundException
{
[Link]("[Link]");
Connection con=[Link]("jdbc:odbc:mydsn","","");
Statement stmt = [Link]( );
ResultSet rs = [Link]("Select * from Emp");
[Link]("Empno \tEname \tJob \tSal");
while([Link]( ))
{
int en = [Link]("Empno");
String nm = [Link]("Ename");
String j = [Link]("Job");
int s = [Link]("Sal");
[Link](en + "\t" + nm + "\t" + j + "\t" + s);
}
[Link]();
}
}
31
32
Output
33
Program -10
Write a program to increase salary by 1000 for all the Employees
from the Emp table (Empno, Ename, Job, Sal) using JDBC concept.
Source Code:
34
Output
35
Program -11
Write a program to create simple servlet for displaying hello
message
Source Code:
[Link]
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<form method = "get" action ="NewServlet">
<input type ="Submit" value= "Click me!">
</form>
</center>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
36
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
[Link]("<center><b>Hello World</b></center>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
37
Output
38
Program -12
Write a program to create JSP file displaying current system date
and time.
Source Code:
[Link]
<html>
<head>
<title>Working with JSP</title>
</head>
<body>
<center><form name ="form" method ="get" action ="[Link]">
<input type ="Submit" value ="Click me">
</form>
</center>
</body>
</html>
[Link]
<%@page import ="[Link].*"%>
<%! Date dt = new Date();%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<center>Current Syntax Date & Timex
<%=dt%>
39
</center>
</body>
</html>
40
Output
41
42
43
44