Mayankajfile
Mayankajfile
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;
else
Mayank pg. 1
21010001010
Gateway Institute of Engineering & Technology (GIET)
Mayank pg. 2
21010001010
Gateway Institute of Engineering & Technology (GIET)
Output
Mayank pg. 3
21010001010
Gateway Institute of Engineering & Technology (GIET)
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.*;
int a,b,c;
a = in.nextInt( );
= in.nextInt( );
try { c = a / b;
catch(ArithmeticException e)
Neha pg. 4
21010001013
Gateway Institute of Engineering & Technology (GIET)
Output
Neha pg. 5
21010001013
Gateway Institute of Engineering & Technology (GIET)
Program -3
Write a program to read data from disk file.
Source Code:
package javaapplication13;
import java.io.*;
IOException{
int n;
System.out.print((char) n);
fis.close( );
}}
Neha pg. 6
21010001013
Gateway Institute of Engineering & Technology (GIET)
Output
Neha pg. 7
21010001013
Gateway Institute of Engineering & Technology (GIET)
Program -4
Write a program to illustrate method overloading.
Source Code:
void check(int n)
if(n%2 == 0)
System.out.println("No. is even");
else
System.out.println("No. is odd");
if(a%b== 0)
else
System.out.println("Not divisible");
}}
obj.check(8, 2);
obj.check(7);
Neha pg. 8
21010001013
Gateway Institute of Engineering & Technology (GIET)
Output
Neha pg. 9
21010001013
Gateway Institute of Engineering & Technology (GIET)
Program -5
Write a program to illustrate method overriding.
Source Code:
}}
class B extends A
{ void display( )
{ super.display( );
}}
Neha pg. 10
21010001013
Gateway Institute of Engineering & Technology (GIET)
Output
Neha pg. 11
21010001013
Gateway Institute of Engineering & Technology (GIET)
Program -6
Write a program to illustrate various forms of Inheritance.
Source Code:
SINGLE INHERITANCE:
package javaapplication81;
class A {
a=x; }
void putdata()
class B extends A
{ int b,c;
void accept(int y)
{ b=y;
void display()
{ c=a+b;
System.out.println("Sum = "+c);
}}
obj.getdata(7);
obj.accept(5);
Neha pg. 12
21010001013
Gateway Institute of Engineering & Technology (GIET)
obj.putdata();
obj.display(); } }
Output
Neha pg. 13
21010001013
Gateway Institute of Engineering & Technology (GIET)
MULTILTVEL INHERITANCE:
package javaapplication81;
class A { int a;
void getdata(int x)
{ a=x; }
void putdata()
}}
class B extends A
{ int b;
void accept(int y)
{ b=y; }
void display()
System.out.println("b = "+b);
}}
class C extends B
int c,d;
void contain(int z) {
c=z;
void show()
d=a+b+c;
}}
Neha pg. 14
21010001013
Gateway Institute of Engineering & Technology (GIET)
new C();
obj.getdata(7);
obj.accept(5);
obj.contain(8);
obj.putdata();
obj.display();
obj.show();
Output
Neha pg. 15
21010001013
Gateway Institute of Engineering & Technology (GIET)
MULTIPLE INHERITANCE:
package javaapplication85; interface
Product1
{ int pc = 101;
String nm = "abc";
void putdata();
class Product2
int qty;
double up;
{ qty = q;
up = u;
void display()
System.out.println("Quantity = "+qty);
}}
{ double amt;
void compute()
Neha pg. 16
21010001013
Gateway Institute of Engineering & Technology (GIET)
void show()
System.out.println("Amount = "+amt);
P.getdata(10,1000);
P.compute();
P.putdata();
P.display(); P.show();
Neha pg. 17
21010001013
Gateway Institute of Engineering & Technology (GIET)
Output
Neha pg. 18
21010001013
Gateway Institute of Engineering & Technology (GIET)
HIERARICHAL INHERITANCE:
package javaapplication85; abstract
class Shape
x, double y)
{ a = x; b = y; }
double ar1;
void area() {
ar1 = a*b;
}}
{ double ar2;
void area()
{ ar2 = 0.5*a*b;
}}
Triangle(); rect.getdata(7.5,6.1);
rect.area();
Neha pg. 19
21010001013
Gateway Institute of Engineering & Technology (GIET)
Output
Neha pg. 20
21010001013
Gateway Institute of Engineering & Technology (GIET)
Program -7
Write a program to display message Hello World on the Applet window.
Source Code:
import java.applet.Applet;
import java.awt.*;
g.drawString("Hello worls",10,100);
Neha pg. 21
21010001013
Gateway Institute of Engineering & Technology (GIET)
Output
Neha pg. 22
21010001013
Gateway Institute of Engineering & Technology (GIET)
Program -8
Write a program to demonstrate layout manager for positioning various swing controls.
Source Code:
import javax.swing.JOptionPane;
{ public NewJFrame()
{ initComponents();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("username"); jButton1.setText("Submit");
jButton1.addActionListener(new java.awt.event.ActionListener() ;
{ jButton1ActionPerformed(evt);
});
jLabel2.setText("Password");
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLay
out.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
Neha pg. 23
21010001013
Gateway Institute of Engineering & Technology (GIET)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addComponent(pf1))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addContainerGap(208, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tf1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlace
ment.RELATED)
Neha pg. 24
21010001013
Gateway Institute of Engineering & Technology (GIET)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(pf1, javax.swing.GroupLayout.PREFERRED_SIZE,
42)
.addComponent(jButton1)
.addContainerGap(162, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
if(nm.equals("System")&& pass.equals("Manager"))
{ java.awt.EventQueue.invokeLater(new Runnable() {
new NewJFrame().setVisible(true);
}); }
Neha pg. 25
21010001013
Gateway Institute of Engineering & Technology (GIET)
javax.swing.JTextField tf1; }
Output
Neha pg. 26
21010001013
Gateway Institute of Engineering & Technology (GIET)
Program-9
Write a program to display detail for all the Employees from the Emp table (Empno,
Source Code:
package javaapplication1;
import java.sql.*;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:mydsn","","");
while(rs.next( ))
int en = rs.getInt("Empno");
String nm = rs.getString("Ename");
String j = rs.getString("Job");
int s = rs.getInt("Sal");
con.close();
}}
Neha pg. 27
21010001013
Gateway Institute of Engineering & Technology (GIET)
Output
Neha pg. 28
21010001013
Gateway Institute of Engineering & Technology (GIET)
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:
package javaapplication16;
import java.sql.*;
import java.util.*;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
ps.executeUpdate();
System.out.println("Record Updated");
con.close();
Output
Neha pg. 29
21010001013
Gateway Institute of Engineering & Technology (GIET)
Program - 11
Write a program to create simple servlet for displaying hello message Source Code:
Index.html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<center>
</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;
Neha pg. 30
21010001013
Gateway Institute of Engineering & Technology (GIET)
out.println("<center><b>Hello World</b></center>");
}}
"Short description";
Neha pg. 31
21010001013
Gateway Institute of Engineering & Technology (GIET)
Output
Neha pg. 32
21010001013
Gateway Institute of Engineering & Technology (GIET)
Program -12
Write a program to create JSP file displaying current system date and time.
Source Code:
Index.html
<html>
<head>
</head>
<body>
</form>
</center>
</body>
</html>
newjsp.jsp
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%=dt%>
Neha pg. 33
21010001013
Gateway Institute of Engineering & Technology (GIET)
</center>
</body>
</html>
Neha pg. 34
21010001013
Gateway Institute of Engineering & Technology (GIET)
Output
Neha pg. 35
21010001013