Advanced Java Programming Examples
Advanced Java Programming Examples
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
PROGRAM:
import [Link];
class Box{
int width,height,depth;
Box(int w,int h,int d)
{
width=w;
height=h;
depth=d;
}
Box(int n)
{
width=height=depth=n;
}
int volume(){
return width*height*depth;
}
}
public class BoxModel {
public static void main(String[] args) {
Scanner s=new Scanner([Link]);
[Link]("Enter the Box 1 values:");
w=[Link]();
h=[Link]();
d=[Link]();
Page No:
Department of IT, AAGAC-VPM
Advance Java
vol=[Link]();
[Link]("The volume of box 2:"+ [Link]());
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
2. IMPLEMENTING CLASSES.
PROGRAM:
import [Link];
class Overloading{
int square,rect;
double circle;
void area(int a){
square=a*a;
[Link]("Area of the square:"+ square);
}
void area(int l,int b){
rect=l*b;
[Link]("Area of the Rectangle:"+ rect);
}
void area(double r){
double pi=3.14;
circle=pi*r*r;
[Link]("Area of the Circle:"+ circle);
}
}
class Main{
public static void main(String[] args){
int a,l,b;
double r;
[Link]("Implementation classes:");
Scanner s=new Scanner([Link]);
[Link]("Enter the value of Square:");
a=[Link]();
[Link]("Enter the value of Rectangle:");
l=[Link]();
b=[Link]();
[Link]("Enter the value of Circle:");
r=[Link]();
Overloading O1=new Overloading();
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link](a);
[Link](l,b);
[Link](r);
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
3. STRING.
PROGRAM:
import [Link];
class StringHandling{
public static void main(String[] args){
String v1,v2,upper,lower,length,app,ins,rev,rep,substr;
int n,a,b,d,e,f,start,end;
char c;
Boolean rm,ew,sw;
[Link]("String Handling:");
Scanner s=new Scanner([Link]);
[Link]("Enter the First String:");
v1=[Link]();
[Link]("Enter the Second String:");
v2=[Link]();
// Using StringBuffer
StringBuffer l1=new StringBuffer(v1);
StringBuffer l2=new StringBuffer(v2);
//Using charAt
[Link]("Enter the values of CharAt:");
n=[Link]();
c=[Link](n);
[Link]("String CharAt is:"+ c);
//Using regionMatches
rm=[Link](1,"form",2,4);
[Link](" regionMatches:" + rm);
Page No:
Department of IT, AAGAC-VPM
Advance Java
sw=[Link]("Tech");
[Link]("The values of startsWith:" + sw);
lower=[Link]();
[Link]("LowerCase of the String:"+ lower);
//SubString
[Link]("Enter the value of SubString:");
a=[Link]();
b=[Link]();
substr=[Link](a,b);
[Link]("Sub String:"+ substr);
app=[Link](" ").append(l2).toString();
[Link]("Append of the two Strings:"+ app);
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
4. COLLECTION.
PROGRAM:
import [Link].*;
class Example
{
public static void main(String args[])
{
// creating an array
[Link](" CREATING AN ARRAY ");
Scanner sc = new Scanner([Link]);
[Link](" Enter the size of array : ");
int n = [Link]();
int[] ar = new int[n];
int i;
[Link](" Enter the values for array : ");
for(i=0; i<n; i++)
{
ar[i] = [Link]();
}
[Link](" Array elements are : ");
for(i=0; i<n; i++)
{
[Link](ar[i] + " " );
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]();
Page No:
Department of IT, AAGAC-VPM
Advance Java
float avg;
avg = tot/[Link];
[Link](" Average of the array is : " +avg);
[Link]();
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
PROGRAM:
import [Link].*;
class DateCompare{
public static void main(String[] args) {
String months[]={"Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"};
Calendar calendar = [Link]();
[Link]("DATE :");
[Link](months[[Link]([Link])]);
[Link](" "+[Link]([Link])+" ");
[Link]([Link]([Link]));
[Link]("TIME:");
[Link]([Link]([Link])+":");
[Link]([Link]([Link])+" :");
[Link]([Link]([Link]));
Page No:
Department of IT, AAGAC-VPM
Advance Java
// After
boolean isAfter = [Link](date2);
[Link]("Date 1 is after date 2: " + isAfter);
// Before
boolean isBefore = [Link](date2);
[Link]("Date 1 is before date 2: " + isBefore);
// Equal
boolean isEqual = [Link](date2);
[Link]("Date 1 is equal to date 2: " + isEqual);
// Compare
int compare = [Link](date2);
[Link]("Date 1 is compareTo date 2:" + compare);
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
6. PACKAGES.
PROGRAM:
package Mypack;
Page No:
Department of IT, AAGAC-VPM
Advance Java
import Mypack.*;
import [Link];
public class BankingApp {
while (true) {
[Link]("\nDo you want to deposit or withdraw?:");
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]("1. Deposit");
[Link]("2. Withdraw");
[Link]("3. Check Balance");
[Link]("4. Exit");
switch (choice) {
case 1:
[Link]("Enter deposit amount: ");
double depositAmount = [Link]();
[Link](depositAmount);
break;
case 2:
[Link]("Enter withdrawal amount: ");
double withdrawalAmount = [Link]();
[Link](withdrawalAmount);
break;
case 3:
[Link]();
break;
case 4:
[Link]("Exiting...");
[Link]();
return;
default:
[Link]("Invalid option. Please try again.");
}
}
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
7. EXCEPTION HANDLING.
PROGRAM:
import [Link];
class ExHandling{
public static void main(String[] args) {
int a=[Link];
Scanner s=new Scanner([Link]);
[Link]("value of a is : "+a);
try{
if(a==0){
int b=24;
a=b/(b-b);
}
if(a==1){
int A[]={1};
A[16]=20;
}
if(a==2){
[Link]("Enter Array size: ");
int n=[Link]();
int[] B=new int[n];
B[17]=15;
}
}
catch(ArithmeticException e){
[Link]("Do not use zero as a divisor. "+e);
}
catch(ArrayIndexOutOfBoundsException e){
[Link]("Array size exceeds. "+e);
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
catch(NegativeArraySizeException e ){
[Link]("Array size can not be negative value. "+e);
}
finally{
[Link](" Exceptions Completed. ");
}
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
8. THREADS.
PROGRAM:
class Q {
int n;
boolean valueSet = false;
synchronized int get() {
if(!valueSet)
try {
wait();
} catch(InterruptedException e) {
[Link]("InterruptedException caught");
}
[Link]("Got: " + n);
valueSet = false;
notify();
return n;
}
synchronized void put(int n) {
if(valueSet)
try {
wait();
} catch(InterruptedException e) {
[Link]("InterruptedException caught");
}
this.n = n;
valueSet = true;
[Link]("Put: " + n);
notify();
}
}
class Producer implements Runnable {
Q q;
Producer(Q q){
this.q = q;
Page No:
Department of IT, AAGAC-VPM
Advance Java
int i = 1;
while(i<=10) {
[Link](i++);
}
}
}
class Consumer implements Runnable {
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
while(true) {
[Link]();
}
}
}
class PC {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
[Link]("Press Control-C to stop.");
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
PROGRAM:
import [Link].*;
public class Ex9a {
public static void main(String args[]) {
try {
// Load the MySQL JDBC driver
[Link]("[Link]");
// Establish connection
Connection con = [Link](url, user, password);
Statement stmt = [Link]();
[Link]("Created DB Connection....");
// Execute query
ResultSet rs = [Link]("SELECT id,name,dept FROM abi11");
ResultSetMetaData rsmd = [Link]();
int ccount = [Link]();
// Print results
while ([Link]()) {
[Link]([Link]("id") + "\t");
[Link]([Link]("name") + "\t");
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]([Link]("dept") + "\t");
// [Link]([Link]("salary"));
[Link]();
}
// Close resources
[Link]();
[Link]();
} catch (ClassNotFoundException e) {
[Link]();
} catch (SQLException e) {
[Link]();
}
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
10. APPLET.
PROGRAM:
import [Link].*;
import [Link].*;
/*
<applet code="SimpleBanner" width=700 height=50>
</applet>
*/
public class SimpleBanner extends Applet implements Runnable {
String msg = " Accept Everything Nothing is Permanent.";
Thread t = null;
int state;
boolean stopFlag;
// Set colors and initialize thread.
public void init() {
setBackground([Link]);
setForeground([Link]);
}
// Start thread
public void start() {
t = new Thread(this);
stopFlag = false;
[Link]();
}
// Entry point for the thread that runs the banner
public void run() {
char ch;
// Display banner
for( ; ; ) {
try {
repaint();
[Link](250);
ch = [Link](0);
msg = [Link](1, [Link]());
Page No:
Department of IT, AAGAC-VPM
Advance Java
msg += ch;
if(stopFlag)
break;
} catch(InterruptedException e) {}
}
}
// Pause the banner.
public void stop() {
stopFlag = true;
t = null;
}
// Display the banner.
public void paint(Graphics g) {
[Link](msg, 500, 300);
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
PROGRAM:
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code = "Text" width=1200 height=1200>
</applet>
*/
add(value1);
add(a);
add(value2);
add(b);
add(ab);
[Link](this);
Page No:
Department of IT, AAGAC-VPM
Advance Java
repaint();
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
//@WebServlet("/vote")
public class vote extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = [Link]("name");
int age = [Link]([Link]("age"));
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html><body>");
if (age < 18) {
[Link]("<h2>" + name + " is not eligible to vote.</h2>");
} else {
[Link]("<h2>" + name + " is eligible to vote.</h2>");
}
[Link]("</body></html>");
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Voting Eligibility Check</title>
</head>
<body>
<h2>Check Voting Eligibility</h2>
<form action="vote" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" required><br><br>
<input type="submit" value="Check Eligibility">
</form>
</body>
</html>
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
<form action="LoginServlet" method="POST">
Name: <input type="text" name="username">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]("WelcomeServlet");
}
}
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class WelcomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
Page No:
Department of IT, AAGAC-VPM
Advance Java
if (username != null) {
[Link]("<h2>Welcome, " + username + "!</h2>");
} else {
[Link]("<h2>No user found. Please log in.</h2>");
[Link]("<a href='[Link]'>Go to Login</a>");
}}}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Age Verification</title>
</head>
<body>
<h2>Enter Your Details</h2>
<form action="VerifyAgeServlet" method="POST">
Name: <input type="text" name="username" required><br>
Age: <input type="number" name="age" required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<h2>You are not eligible to vote.</h2>
<p>You must be 18 or older to vote. Please try again later.</p>
<a href="[Link]">Go back to the form</a>
</body>
Page No:
Department of IT, AAGAC-VPM
Advance Java
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
try {
String username = [Link]("username");
int age = [Link]([Link]("age"));
if (age >= 18) {
RequestDispatcher dispatcher = [Link]("Welcome");
[Link]("username", username); // Pass the username as a request attribute
[Link](request, response);
//[Link]("WelcomeServlet");
} else {
[Link]("[Link]");
}
} finally {
[Link]();
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet(urlPatterns = {"/Welcome"})
public class Welcome extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
try {
String username = (String) [Link]("username");
[Link]("<h2>Welcome, " + username + "!</h2>");
[Link]("<p>You are eligible to vote.</p>");
} finally {
[Link]();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="LoginServlet" method="post">
<h2>Login Form</h2>
Username: <input type="text" name="username" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" value="Login">
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
public class DatabaseConnection {
private static final String URL = "jdbc:derby://localhost:1527/sample";
private static final String USER = "app";
private static final String PASSWORD = "app";
public static Connection initializeDatabase() throws SQLException, ClassNotFoundException {
// [Link]("[Link]");
[Link]("[Link]");
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = null;
try {
out = [Link]();
String username = [Link]("username");
String password = [Link]("password");
try {
Connection conn = [Link]();
String sql = "SELECT * FROM STUD WHERE USERN=? AND PASSWORD=?";
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]
import student.*;
public class Teststudent {
public static void main(String[] args) {
Student s1 = new Student();
[Link]("Pooja");
[Link](20);
[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
[Link](s1);
}
}
Package student:[Link]
package student;
public class Student {
private String name;
private int id;
public Student() {
}
// Constructor with parameters (optional)
public Student(String name, int id) {
[Link] = name;
[Link] = id;
}
// Getter and Setter for 'name'
public String getName() {
Page No:
Department of IT, AAGAC-VPM
Advance Java
return name;
}
public void setName(String name) {
[Link] = name;
}
// Getter and Setter for 'age'
public int getid() {
return id;
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Ex. No:
Date:
Page No:
Department of IT, AAGAC-VPM
Advance Java
Page No:
Department of IT, AAGAC-VPM
Advance Java
[Link]
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[Link]
<%@ page import="[Link]" %>
<html>
<head>
<title>Student Details</title>
</head>
<body>
<h2>Student Details</h2>
<%
// Create an instance of the Student bean
Student student = new Student();
[Link]("Priya");
[Link](15);
// Set the bean in the request scope
[Link]("student", student);
%>
<p>Name: ${[Link]}</p>
<p>ID: ${[Link]}</p>
</body>
</html>
Package student:[Link]
package student;
public class student {
Page No:
Department of IT, AAGAC-VPM
Advance Java
public student() {
}
private String name;
public String getName() {
return name;
}
private int id;
public int getId() {
return id;
}
public void setId(int id) {
[Link] = id;
}
public void setName(String name) {
[Link] = name;
}
}
Page No:
Department of IT, AAGAC-VPM
Advance Java
OUTPUT:
Page No:
Department of IT, AAGAC-VPM