Practical Assignment
Name :- Sourabh Ashok Lamdade
Subject :- Java Programming PRN :- 2020080496
Q.1 Write a program that displays Welcome Java message
class Welcome
public static void main (String[]args)
System.out.println("Welcome to Java");
Output:
Q.2 Write a program in Java that demonstrate default constructor & parameterised
constructor.
Default Constructor
class Main
int a;
boolean b;
public static void main (String [] args)
Main obj= new Main();
System.out.println("Default Value:");
System.out.println("a=" + obj.a);
System.out.println("b=" + obj.b);
Output:
Parameterised Constructor
class Main
String languages;
Main(String lang)
languages = lang;
System.out.println(languages + " Programming Language");
public static void main(String[] args)
Main obj1 = new Main("Java");
Main obj2 = new Main("Python");
Main obj3 = new Main("C");
Output:
Q.3 Write a program to Calculate a simple interest.
import java.util.Scanner;
class Main
public static void main(String[] args)
Scanner input = new Scanner(System.in);
System.out.print("Enter the principal: ");
double principal = input.nextDouble();
System.out.print("Enter the rate: ");
double rate = input.nextDouble();
System.out.print("Enter the time: ");
double time = input.nextDouble();
double interest = (principal * time * rate) / 100;
System.out.println("Principal: " + principal);
System.out.println("Interest Rate: " + rate);
System.out.println("Time Duration: " + time);
System.out.println("Simple Interest: " + interest);
input.close();
Output:
Q.4 Write a program to find the average and sum of the ‘N’ numbers using user input.
import java.util.Scanner;
public class sum
public static void main (String [] args)
Scanner sc= new Scanner(System.in);
System.out.println("Enter the array size:");
int len= sc.nextInt();
int arr[]= new int[len];
double sum= 0;
double avg= 0;
System.out.println("Enter the 5 array element");
for(int i=0;i<len;i++)
arr[i]= sc.nextInt();
for(int i=0;i<len;i++)
sum += arr[i];
avg= sum/len;
System.out.println("sum of"+len+"number in" + sum);
System.out.println("Average is" + avg);
}
Output:
Q.5 Write a program to create simple class to findout area and perimeter of rectangle of
box using super keyword.
import java.util.*;
class Rectangle
double length;
double width;
void Area()
double area;
area = this.length * this.width;
System.out.println("Area of rectangle is : " + area);
void Perimeter()
double perimeter;
perimeter = 2 * (this.length + this.width);
System.out.println("Perimeter of rectangle is : " + perimeter);
class UseRectangle {
public static void main(String args[])
Rectangle rect = new Rectangle();
rect.length = 15.854;
rect.width = 22.65;
System.out.println("Length = " + rect.length);
System.out.println("Width = " + rect.width);
rect.Area();
rect.Perimeter();
Output:
Q.6 Write a program to design class shape using abstract method and classes.
abstract class Shape
abstract void draw();
class Rectangle extends Shape
void draw()
System.out.println("drawing rectangle");
class Circle1 extends Shape
void draw()
System.out.println("drawing circle");
class Abstraction1
public static void main(String args[])
Shape s=new Circle1();
s.draw();
} }
Output:
Q.7 Write a program using calendar that displays current year, month, date.
import java.util.Date;
import java.time.Month;
import java.time.LocalDate;
class Calendar
public static void
getDayMonthYear(String date)
LocalDate currentDate= LocalDate.parse(date);
int day = currentDate.getDayOfMonth();
Month month = currentDate.getMonth();
int year = currentDate.getYear();
System.out.println("Day: " + day);
System.out.println("Month: " + month);
System.out.println("Year: " + year);
public static void main(String args[])
String date = "2021-08-09";
getDayMonthYear(date);
}}
Output:
Q.8 Write a program using calendar current year, day of week, week of month, hours,
minute, seconds, milisec.
Q.9 Write a program to create package that displays Welcome to Package.
import java.util.*;
class simple
public static void main(String[] args)
Scanner myObj = new Scanner(System.in);
String Message;
System.out.println("Enter You Message");
Message = myObj.nextLine();
System.out.println("Your Message is : " + Message);
Output:
Q.10 Write a program to create package array list using 10 elements.
import java.util.ArrayList;
class Package1
public static void main (String [] args)
ArrayList<Integer> list= new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(8);
list.add(9);
list.add(10);
System.out.println(list);
Output:
Q.11 Implement program to calculate area of a square take method calc( ) in super
class as well in sub class(using method overriding).
Java Code :
import java.util.Scanner;
class shape
{
float result ;
public void calc (float a)
{
result = a * a ;
}
}
class rectangle extends shape
{
public void calc(float a )
{
super.calc(a);
System.out.println("Area of Square:" + super.result);
}
}
public class TestRectangle
{
public static void main(String[] args)
{
float a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the side of Square:");
a = sc.nextFloat();
rectangle s = new rectangle();
s.calc(a);
}
}
Output :
Q.12 Write a program to execute select query using JDBC(Create one table Employee)
Java Code :
import java.sql.*;
class JdbcSelect
public static void main(String args[])
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Drivers are loaded");
Connection con = DriverManager.getConnection("jdbc:odbc:Demo");
System.out.println("Connection is Created");
Statement st = con.createStatement();
st.execute("CREATE TABLE Employee(EmpID int,FirstName
varchar(255),LastName varchar(255),Department varchar(50),Salary
int);");
System.out.println("Table is created");
st.execute("INSERT INTO Employee
VALUES(1,'Allen','Max','Manager',50000);");
st.execute("INSERT INTO Employee
VALUES(2,'Matt','demon','Developer',20000);");
st.execute("INSERT INTO Employee
VALUES(3,'Alex','mac','Sounds',10000);");
st.execute("INSERT INTO Employee
VALUES(4,'Kale','poes','Visuals',52000);");
st.execute("INSERT INTO Employee
VALUES(5,'Lowis','Mills','Producer',54000);");
System.out.println("Recored Inserted");
ResultSet rs = st.executeQuery("SELECT * FROM Employee;");
while(rs.next())
for(int i=1;i<6;i++)
System.out.println(rs.getString(i));
catch(Exception e)
System.out.println(e);
}
Output :
Q.13 Write a Program using jdbc which shows how to delete records from table.
Java code :
import java.sql.*;
class JdbcDelete
public static void main(String args[])
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Drivers are loaded");
Connection con = DriverManager.getConnection("jdbc:odbc:Demo");
System.out.println("Connection is Created");
Statement st = con.createStatement();
st.execute("DELETE FROM Employee WHERE FirstName='Alex'");
System.out.println("Record Deleted");
con.close();
catch (Exception e)
System.out.println(e);
}
Output :
Q.14 Write a program implement servlet for displaying Hello.
Java code :
// index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TO DO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>Click here to go <a href="NewServlet">Click</a></h2>
</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;
public class NewServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello....! Welcome to My first servlet </h1>");
out.println("</body>");
out.println("</html>");
}
}
Output :
Q.15 Write a program to implement Servlet to take values from client and display it.
Java code :
// index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TO DO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form name="loginForm" method="get" action="ClientServlet">
Name : <input type="text" name="name"/> <br/><br/>
Address : <input type="text" name="address"/> <br/><br/>
Mobile No. : <input type="text" name="number"/> <br/><br/>
E-mail : <input type="text" name="email"/> <br/><br/>
<input type="submit" value="Login" />
</form>
</body>
</html>
//ClienServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/ClientServlet"})
public class ClientServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
/* TODO output your page here. You may use following sample code. */
String Name = request.getParameter("name");
String Address = request.getParameter("address");
String mobile = request.getParameter("number");
String email = request.getParameter("email");
System.out.println("Name: " + Name);
System.out.println("Address: " + Address);
System.out.println("mobile: " + mobile);
System.out.println("Email: " + email);
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title></title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Your name is:" + Name + "</h2>");
out.println("<h2>Your Address is:" + Address + "</h2>");
out.println("<h2>Your Mobile number is:" + mobile + "</h2>");
out.println("<h2>Your E-mail ID is:" + email + "</h2>");
out.println("</body>");
out.println("</html>");
}
Output :
Sourabh Lamdade
Miraj
8698423695
[email protected]
SSourabh Lamdade
MirajMiraj
8698423695
[email protected]
Q.16 Write a program to implement Session Management using all four types.
Java code :
// index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="US-ASCII">
<title>Login Page</title>
</head>
<body>
<form action="LoginServlet" method="get">
Username: <input type="text" name="username">
<br><br>
Password: <input type="password" name="password">
<br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
// LoginServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//response.getWriter().append("Served at:
").append(request.getContextPath());
try {
PrintWriter out=response.getWriter();
String username=request.getParameter("username");
String password=request.getParameter("password");
out.println("<p> Welcome: "+username + "</p>");
out.println("<p> Here is your password: "+password + "</p>");
HttpSession session=request.getSession();
session.setAttribute("username",username);
session.setAttribute("password",password);
out.println("<a href='NewServlet'>View </a>");
out.close();
}catch (Exception e) {
// TODO: handle exception
System.out.println(e);
// NewServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {
public NewServlet() {
super();
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at:
").append(request.getContextPath());
try{
response.setContentType("text/html");
PrintWriter p=response.getWriter();
HttpSession session = request.getSession(false);
String myName=(String)session.getAttribute("username");
String mypass=(String)session.getAttribute("password");
p.println("<p>Youre username is : "+myName + "</p>");
p.println("<p> Youre Password is : "+mypass + "</p>");
p.close();
}catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
Output :
sourabh
Sourabh123
Sourabh
Sourabh123
sourabh
Sourabh123
Q.17 Write a Program using jdbc which shows how to update records in table.
Java Code :
import java.sql.*;
class JdbcUpdate
public static void main(String args[])
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Drivers are loaded");
Connection con = DriverManager.getConnection("jdbc:odbc:Demo");
System.out.println("Connection is Created");
Statement st = con.createStatement();
st.execute("UPDATE Employee SET
FirstName='Johnny',Department='Janitor',Salary=9000 WHERE
LastName='poes'");
System.out.println("Record Updated");
con.close();
catch(Exception e)
System.out.println(e);
}
}
Output:
Q.18 WAP to implement RMI(Show only msg welcome to rmi)
Java Code :
// Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote
String sayHello() throws RemoteException;
// HelloImp.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class HelloImp extends UnicastRemoteObject implements Hello
protected HelloImp() throws RemoteException
super();
public String sayHello()
return "Hello world...!";
// HelloClient.java
import java.rmi.Naming;
public class HelloClient
public static void main(String[] args)
try
Hello h = (Hello) Naming.lookup("//127.0.0.1:1099/calculatorservice");
System.out.println(h.sayHello());
catch(Exception e)
System.out.print("Exception occer:");
// HelloServer.java
import java.rmi.Naming;
public class HelloServer
HelloServer()
try
Hello h = new HelloImp();
Naming.rebind("rmi://localhost:1099/calculatorservice",h);
}
catch(Exception e)
System.out.println("Exception occurs:");
public static void main(String[] args)
new HelloServer();
Output :
Q.19 JSP Program to display given number in words.
Java Code :
Output :
Q.20 Write a program using RMI which shows the different operations like addition,
subtraction, division, multiplication. (For implementing this use scanner class)
Java Code :
// Operation.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Operation extends Remote
public long addition(long a , long b) throws RemoteException;
public long substraction(long a , long b) throws RemoteException;
public long multiplication(long a , long b) throws RemoteException;
public long division(long a , long b) throws RemoteException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
// OperationImp.java
public class OperationImp extends UnicastRemoteObject implements Operation
protected OperationImp() throws RemoteException
super();
public long addition (long a,long b) throws RemoteException
return a+b;
}
public long substraction(long a,long b) throws RemoteException
return a-b;
public long multiplication(long a,long b) throws RemoteException
return a*b;
public long division(long a,long b) throws RemoteException
return a/b;
// OperationClient.java
import java.rmi.Naming;
import java.util.Scanner;
public class OperationClient
public static void main(String[] args)
try
{
Operation c = (Operation)
Naming.lookup("//127.0.0.1:1099/calculatorservice");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number:");
long a = sc.nextLong();
System.out.println("Enter the second number:");
long b = sc.nextLong();
sc.close();
System.out.println("Addition:" +c.addition( a , b));
System.out.println("Substraction:" +c.substraction(a , b));
System.out.println("multiplication:" +c.multiplication( a , b));
System.out.println("division:" +c.division( a , b));
catch(Exception e)
System.out.print("Exception occer:");
// OperationServer.java
import java.rmi.Naming;
public class OperationServer
OperationServer()
try
{
Operation c = (Operation) new OperationImp();
Naming.rebind("rmi://127.0.0.1:1099/calculatorservice",c);
catch(Exception e)
System.out.println("Exception occurs:");
public static void main(String[] args)
new OperationServer();
Output :