0% found this document useful (0 votes)
0 views

adv. java file

The document contains multiple Java programs demonstrating various concepts such as customer detail storage, error handling, method overloading, method overriding, inheritance, and JDBC operations. Each program includes source code and a brief description of its functionality. Additionally, there are examples of applets, Swing layout management, servlets, and JSP for web applications.

Uploaded by

sameer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

adv. java file

The document contains multiple Java programs demonstrating various concepts such as customer detail storage, error handling, method overloading, method overriding, inheritance, and JDBC operations. Each program includes source code and a brief description of its functionality. Additionally, there are examples of applets, Swing layout management, servlets, and JSP for web applications.

Uploaded by

sameer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

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 java.util.*;
class Customer
{
int ac, bal;
String nm;
double interest;
Customer( )
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Account no ");
ac = in.nextInt( );
in.nextLine( ); // To clear the buffer
System.out.print("Enter name");
nm = in.nextLine( );
System.out.print("Enter Balance ");
bal = in.nextInt( );
}
void compute( )
{
if(bal >= 75000)
interest = bal * .08;
else
if(bal >= 40000)
interest = bal * .06;

1
else
interest = bal * .03;
}
void putdata( )
{
System.out.println("Acno = " + ac);
System.out.println("Name = " + nm);
System.out.println("Balance = " + bal);
System.out.println("Interest = " + interest);
}
}
public class JavaApplication2 {
public static void main(String[] args) {
Customer cust = new Customer( );
cust.compute( );
cust.putdata( );
}
}

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 java.util.*;
public class JavaApplication3{
public static void main(String[] args) {
{
int a, b, c;
Scanner in = new Scanner (System.in);
System.out.print("Enter 1st number ");
a = in.nextInt( );
System.out.print("Enter 2nd number ");
b = in.nextInt( );
try
{
c = a / b;
System.out.println(" Result = " + c);
}
catch(ArithmeticException e)
{
System.out.println("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)
System.out.println("No. is even");
else
System.out.println("No. is odd");
}
void check(int a, int b)
{
if(a%b == 0)
System.out.println("1st no. is div. by 2nd ");
else
System.out.println("Not divisible");
}
}
public class JavaApplication4 {
public static void main(String[] args) {
Sample obj = new Sample( );
obj.check(8, 2);
obj.check(7);
}
}

8
Output

9
Program -5
Write a program to illustrate method overriding.
Source Code:
package javaapplication5;
class A {
void display( )
{
System.out.println(" This is class A ");
}
}
class B extends A {
void display( )
{
super.display( );
System.out.println(" This is class B ");
}
}
public class JavaApplication5 {
public static void main(String[] args) {
B objb = new B( );
objb.display( );
}
}

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()
{
System.out.println("a = " +a);
}
}
class B extends A
{
int b,c;
void accept(int y)
{
b=y;
}
void display()
{
c=a+b;

12
System.out.println("Sum = "+c);
}
}
public class JavaApplication81 {
public static void main(String[] args) {
B obj = new B();
obj.getdata(7);
obj.accept(5);
obj.putdata();
obj.display();
}
}

13
Output

14
MULTILTVEL INHERITANCE:
package javaapplication81;
class A
{
int a;
void getdata(int x)
{
a=x;
}
void putdata()
{
System.out.println("a = " +a);
}
}
class B extends A
{
int b;
void accept(int y)
{
b=y;
}
void display()
{
System.out.println("b = "+b);
}
}
class C extends B

15
{
int c,d;
void contain(int z)
{
c=z;
}
void show()
{
d=a+b+c;
System.out.println(" Sum = "+d);
}
}
public class JavaApplication81 {
public static void main(String[] args) {
C obj = new C();
obj.getdata(7);
obj.accept(5);
obj.contain(8);
obj.putdata();
obj.display();
obj.show();
}
}

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()
{
System.out.println("Quantity = "+qty);
System.out.println("Unit Price = "+up);
}
}
class Product extends Product2 implements Product1
{
double amt;
void compute()

18
{
amt = qty * up;
}
void show()
{
System.out.println("Amount = "+amt);
}
public void putdata()
{
System.out.println("Product code = "+pc);
System.out.println("Product name = "+nm);
}
}
public class JavaApplication85 {
public static void main(String[] args) {
Product P = new Product();
P.getdata(10,1000);
P.compute();
P.putdata();
P.display();
P.show();

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;
System.out.println("Area of reactangle = " +ar1);
}
}
class Triangle extends Shape
{
double ar2;
void area()
{
ar2 = 0.5*a*b;

21
System.out.println("Area of triangle = " +ar2);
}
}
public class JavaApplication85 {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
rect.getdata(7.5,6.1);
rect.area();
tri.getdata(5.2, 6.3);
tri.area();
}
}

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 javax.swing.JOptionPane;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
private void initComponents()
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
tf1 = new javax.swing.JTextField();
pf1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("username");
jButton1.setText("Submit");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel2.setText("Password");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(

26
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(97, 97, 97)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(41, 41, 41)
.addComponent(pf1))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 65,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED
)
.addComponent(tf1, javax.swing.GroupLayout.PREFERRED_SIZE, 86,
javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(208, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

27
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tf1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(pf1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(42, 42, 42)
.addComponent(jButton1)
.addContainerGap(162, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

String nm,pass;
nm = tf1.getText();
pass = pf1.getText();
if(nm.equals("System")&& pass.equals("Manager"))
JOptionPane.showMessageDialog(null, "Authorized user");
else
JOptionPane.showMessageDialog(null, "unauthorized user");
}
public static void main(String args[]) {

28
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField pf1;
private javax.swing.JTextField 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 java.sql.*;
public class JavaApplication1 {
public static void main(String[] args)
throws SQLException, ClassNotFoundException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:mydsn","","");
Statement stmt = con.createStatement( );
ResultSet rs = stmt.executeQuery("Select * from Emp");
System.out.println("Empno \tEname \tJob \tSal");
while(rs.next( ))
{
int en = rs.getInt("Empno");
String nm = rs.getString("Ename");
String j = rs.getString("Job");
int s = rs.getInt("Sal");
System.out.println(en + "\t" + nm + "\t" + j + "\t" + s);

}
con.close();
}
}

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:
Index.html
<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>

NewServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

36
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<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:

Index.html
<html>

<head>

<title>Working with JSP</title>


</head>
<body>

<center><form name ="form" method ="get" action ="newjsp.jsp">

<input type ="Submit" value ="Click me">

</form>

</center>

</body>

</html>

newjsp.jsp
<%@page import ="java.util.*"%>

<%! 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

You might also like