Java Laboratory
Java Laboratory
Create a class Vehicle. The class should have two fields – no_of_seats, no_of_wheels
and a method showVehicle. Create two objects – motorcycle and car for this class.
Display the output to show the descriptions for car and motorcycle.
Code:
Output:
Details of Car:
Total Number of seats: 4
Total Number of wheels: 4
Details of Motorcycle:
Total Number of seats: 1
Total Number of wheels: 2
Program - 2
Write a program to make a package Balance which has Account class with display
method in it. Import Balance package in another program to access display method of
Account class to display account balance.
Code:
//Part-1 Package Creation
package balance;
import java.util.*;
public class Account
{
long acc,bal;
String name;
public void read()throws Exception
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name :");
name=in.nextLine();
System.out.println("Enter the account number :");
acc=Long.parseLong(in.nextLine());
System.out.println("Enter the account balance :");
bal=Long.parseLong(in.nextLine());
}
public void disp()
{
System.out.println("~~~~~~~~~~~~~~~~~");
System.out.println("--- Account Details ---");
System.out.println("~~~~~~~~~~~~~~~~~");
System.out.println("Name :"+name);
System.out.println("Account number :"+acc);
System.out.println("Balance :"+bal);
}
}
//Part – 2 Main Class
class BankBal
{
public static void main(String ar[])
{
try
{
balance.Account a=new balance.Account();
a.read(); //calling the method of Account class
a.disp();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Output:
Enter the name:
Rithik K M
Enter the account number:
1235678429
Enter the account balance:
2987
~~~~~~~~~~~~~~~~~
----Account Details----
~~~~~~~~~~~~~~~~~
Name: Rithik K M
Account number 1235678429
Balance: 2987
Program - 3
Design a super class called Employee with details as EmployeeId, name, Phone,
Salary. Extend this class by writing three subclasses namely Teaching (domain,
publications), Technical (skills), and Contract (period). Write a JAVA program to read
and display at least 3 Employee objects of all three categories.
Code:
import java.util.*;
class Employee
{
String EmpID;
String Empname;
long EmpPhone;
float EmpSalary;
public void accept()
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter Staff ID: ");
EmpID = obj.nextLine();
System.out.println("Enter Name: ");
Empname = obj.nextLine();
System.out.println("Enter Phone number: ");
EmpPhone = obj.nextLong();
System.out.println("Enter Salary: ");
EmpSalary = obj.nextFloat();
}
public void display()
{
System.out.println("Staff ID: " + EmpID);
System.out.println("Name: " + Empname);
System.out.println("Phone: " + EmpPhone);
System.out.println("Salary: " + EmpSalary);
}
}
class Teaching extends Employee
{
String domain;
int n;
public void accept()
{
super.accept();
Scanner obj = new Scanner(System.in);
System.out.println("Enter Domain:");
domain = obj.nextLine();
System.out.println("Enter number of Publications:");
n = obj.nextInt();
}
public void display()
{
super.display();
System.out.println("Doamin:" + domain);
System.out.println("Publications: " + n);
}
}
class Technical extends Employee
{
String skill;
public void accept()
{
super.accept();
Scanner obj = new Scanner(System.in);
System.out.println("Enter Technical Skills:");
skill = obj.nextLine();
}
public void display()
{
super.display();
System.out.println("Technical Skills: " + skill);
}
}
class Contract extends Employee
{
int period;
public void accept()
{
super.accept();
Scanner obj = new Scanner(System.in);
System.out.println("Enter Period:");
period = obj.nextInt();
}
public void display()
{
super.display();
System.out.println("Contract Period: " + period);
}
}
class EmployeeFour
{
public static void main(String[] args)
{
Teaching teach = new Teaching();
System.out.println("Enter the details of Teaching Staff:");
teach.accept();
Technical tech = new Technical();
System.out.println("Enter the details of Technical Staff:");
tech.accept();
Contract con = new Contract();
System.out.println("Enter the details of Contract Staff:");
con.accept();
System.out.println("The details of Teaching Staff:");
teach.display();
System.out.println("The details of Technical Staff:");
tech.display();
System.out.println("The details of Contract Staff:");
con.display();
}
}
Output:
Enter the details of Teaching Staff:
Enter Staff ID:
DS287
Enter Name:
Rahul M S
Enter Phone number:
7812356497
Enter Salary:
78567.89
Enter Domain:
Web Development
Enter number of Publications:
28
Code:
import java.util.Scanner;
class ExceptionDemo
{
public static void main(String[] args)
{
int a,b,result;
Scanner input =new Scanner(System.in);
System.out.println("Input two integers");
a=input.nextInt();
b=input.nextInt();
try
{
result=a/b;
System.out.println("Result = "+result);
}
catch(ArithmeticException e)
{
System.out.println("exception caught: Divide by zero
error"+e);
}
int array[]={2,3,4,5,6};
try
{
System.out.println("Input two integers"+array[5]);
}
catch(ArrayIndexOutOfBoundsException e1)
{
System.out.println("array index out of bound"+e1);
}
Output:
}
Input two numbers:
}
30
Exception caught: Divide by zero error
Java.lang.ArithmeticException: / by zero
Array Index Out of Bound
Java.lang.ArrayIndexOutofBoundException:
5
Program – 5
Implement Java Program to Get the Components of any give URL such as
Protocol, file, port and host.
Code:
import java.net.URL;
public class URLMain
{
public static void main(String[] args)
{
try
{
URL url = new
URL("https://www.example.com/path/to/file.html?key=value#fra
gment");
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Host: " + url.getHost());
System.out.println("Port: " + url.getPort());
System.out.println("Path: " + url.getPath());
System.out.println("Query: " + url.getQuery());
System.out.println("Fragment: " + url.getRef());
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
}
}
Output:
Protocol: https
Host: www.example.com
Port: -1
Path: /path/file.html
Query: key=value
Fragment: fragment
Program – 6
Implement client-server communication, where client can send the message and server
can receive the message without internet.
Code:
//Part-1 ServerSide.java
import java.io.*;
import java.net.*;
public class ServerSide
{
public static void main(String[]args)
{
try
{
ServerSocket ss=new ServerSocket(3306);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
//Part-2 ClientSide.java
import java.io.*;
import java.net.*;
public class ClientSide
{
public static void main(String[] args)
{
try
{
Socket s=new Socket("localhost",3306);
DataOutputStreamdout=new
DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}
catch(Exception e)
{
System.out.println(e);}
}
}
Output:
Message = Hello Server
Program - 7
Implement JDBC program to insert and retrieve student (student_name, student_usn,
student_dept) record from student database.
Code:
//Part - 1 Insert Details
import java.sql.*;
import java.util.*;
public class InsertDetails
{
public static void main(String[] args)
{
String usn, name, dept;
Scanner obj = new Scanner(System.in);
System.out.println("Enter Student Name:");
name = obj.nextLine();
System.out.println("Enter Student USN:");
usn = obj.nextLine();
System.out.println("Enter Student Dept:");
dept = obj.nextLine();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/Student","root","root");
Statement stmt = con.createStatement();
String q1 = "insert into student values('"
+usn+"','"+name+"','"+dept+"')";
int x = stmt.executeUpdate(q1);
if(x>0)
System.out.println("Successfully Inserted");
else
System.out.println("Insert Failed");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
Enter Student Name:
Shreyas
Enter Student USN:
1DS22AI010
Enter Student Dept:
AIML
Successfully Inserted
Code:
//Part-2 Retrieve Details
import java.sql.*;
public class RetrieveDetails
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/Student","root","root");
Statement stmt = con.createStatement();
ResultSet rs= stmt.executeQuery("select * from student");
while(rs.next())
System.out.println(rs.getString(1)+"
"+rs.getString(2)+" "+rs.getString(3));
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
Code:
Login.html
<html>
<head>
<title>Login</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="Login.jsp" method="post" id="styleform">
<h2>Login Authentication</h2><hr color="black"><br>
Username: <input type="text" name="user"/><br><br>
Password: <input type="password" name="pwd"/><br><br><br>
<input type="submit" value="Submit" id="stylesub"/>
</form>
</body>
</html>
Login.jsp
Output:
Program – 9
Structure the java servlet program to fetch the student details using JDBC.
Code:
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;
import java.sql.*;
Output:
Program - 10
Develop a Servlet program to demonstrate hit counter.
Code:
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;