Advanced Java Lab Manual
Advanced Java Lab Manual
COURSE SYLLABUS
SEMESTER-III
Course Name: Advanced Java Programming Course Code: BISAJ317
No. of Lecture hours / week: 00 CIE Marks: 50
No. of Tutorial hours / week: 04 SEE Marks: 50
Total No. of Lecture+ SEE Duration: 02hr
Tutorial/Practical hours 50
L: T: P: 0:0:4 Credits: 02
Course Prerequisites: Basic understanding of Java programming with OOPS.
Course Overview: The focus of this course is on design and implementation of advanced java
concepts through hands on experience to develop real world applications.
Course Learning Objectives (CLO)
This course will enable students to,
• Familiarize advanced features of Java.
• Develop core java applications using multiple threads, JDBC, JSP, Applets and Servlets.
PART A : INTRODUCTION
Following are Basic core Java programs.
1. Write a Java program that prompts the user for an integer N and generates all the prime
numbers up to N.
2. Write a Java program to create a class box with instance variable width, height, depth and
create an object using default constructors and parameterized constructors.
3. Write a Java program for adding two numbers using method overloading.
4. Write a Java program to convert an integer 257 to byte using narrowing type conversion and
widening type conversion.
5. Write a Java program to sort the string elements in a 1-dimensional array.
6. Write a Java program to sum all the elements of an array using for-each version of for loop.
7. Create a Java class Customer with the following details as variables within it: CustID, Name,
Age, Phone, Place. Write a Java program to create n Customers objects and print the CustID,
Name, Age, Phone and Place of these objects with suitable headings using “this” keyword.
8. Design a super class called Employee with details as EmpID, Name, Phone, Salary, extend this
class by writing two subclasses namely Tester (ProjectID, ProjectName), Developer
(ProjectName). Write a Java Program to read and display at least 2 Employee objects of all two
categories using Inheritance.
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 1 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
PART B – Advanced Concepts
1. Develop a Java program to create an interface named Shape that contains a method named
printArea(). Provide three classes named Rectangle, Triangle and Circle such that each one
of the classes extends the interface Shape. Each one of the classes contains only the method
printArea() that prints the area of the given shape.
2. Create a class Book which contains four members: name, author, price, num of pages. Include
a constructor to set the values for the members. Include methods to set and get the details
of the objects. Include a toString() method that could display the complete details of the
book. Develop a Java program to create n book objects.
3. Write a program that demonstrates handling of exceptions in inheritance. Create a base class
called “Father” and derived class called “Son” which extends the base class. In Father class,
implement a constructor which takes the age and throws the exception Wrong Age () i.e
when the input age is equal to father’s age.
4. Write a program which creates two threads, one thread displaying “Vidyavardhaka College
of Engineering” once every ten seconds and another displaying “CSE” once every two
seconds.
5. Write a program that sorts an array of strings using compareTo() to determine bubble sort
ordering.
6. Develop a java program to create an enum as session and demonstrate the usage of value(),
valueOf() and ordinal() methods.
7. Write a Java program to implement the SQL commands using JDBC.
8. Write a JSP program which shows a Sample Order Form.
PART C - Open Ended Experiments
Students shall solve a problem (either given by the staff or students may come up with their own
problem) using the design techniques.
1. Develop JSP program which displays the System date and time.
2. Develop a Swings program to display image on the button.
3. Develop Session Handling using servlets.
4. Develop Chat Server using Java.
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 2 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
PART A: INTRODUCTION
1. Write a Java program that prompts the user for an integer N and generates all the prime numbers
up to N.
import java.util.Scanner;
// If i is prime, print it
if (isPrime) {
System.out.print(i + " ");
}
}
OUTPUT:
Enter an integer N: 10
Prime numbers up to 10:
2 3 5 7
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 3 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
2. Write a Java program to create a class box with instance variable width, height, depth and create an
object using default constructors and parameterized constructors.
class Box {
double width;
double height;
double depth;
// Default constructor
public Box() {
width = 1.0;
height = 1.0;
depth = 1.0;
}
// Parameterized constructor
public Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 4 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
OUTPUT:
3. Write a Java program for adding two numbers using method overloading.
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 5 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
System.out.println("Sum of 2, 4, and 6 is: " + sum3);
}
}
OUTPUT:
OUTPUT:
import java.util.Arrays;
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 6 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
String[] stringArray = {"Apple", "Orange", "Banana", "Grape",
"Pineapple"};
}
}
OUTPUT:
6. Write a Java program to sum all the elements of an array using for-each version of for loop.
OUTPUT:
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 8 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
}
}
OUTPUT:
Customer ID: 1
Name: Alice
Age: 25
Phone: 1234567890
Place: New York
------------------------------
Customer ID: 2
Name: Bob
Age: 30
Phone: 9876543210
Place: London
------------------------------
Customer ID: 3
Name: Charlie
Age: 28
Phone: 5551234567
Place: Sydney
------------------------------
8. Design a super class called Employee with details as EmpID, Name, Phone, Salary, extend this class by
writing two subclasses namely Tester (ProjectID, ProjectName), Developer (ProjectName). Write a
Java Program to read and display at least 2 Employee objects of all two categories using Inheritance.
//Superclass Employee
class Employee {
int empID;
String name;
String phone;
double salary;
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 9 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
public void displayDetails() {
System.out.println("Employee ID: " + empID);
System.out.println("Name: " + name);
System.out.println("Phone: " + phone);
System.out.println("Salary: $" + salary);
}
}
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 10 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
System.out.println("------------------------------");
}
}
OUTPUT:
Tester 1 Details:
Employee ID: 1
Name: Alice
Phone: 1234567890
Salary: $60000.0
Project ID: 101
Project Name: ProjectA
------------------------------
Tester 2 Details:
Employee ID: 2
Name: Bob
Phone: 9876543210
Salary: $65000.0
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 11 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
Project ID: 102
Project Name: ProjectB
------------------------------
Developer 1 Details:
Employee ID: 3
Name: Charlie
Phone: 5551234567
Salary: $70000.0
Project Name: ProjectC
------------------------------
Developer 2 Details:
Employee ID: 4
Name: David
Phone: 9998887776
Salary: $75000.0
Project Name: ProjectD
------------------------------
1. Develop a Java program to create an interface named Shape that contains a method named printArea().
Provide three classes named Rectangle, Triangle and Circle such that each one of the classes extends
the interface Shape. Each one of the classes contains only the method printArea() that prints the area of
the given shape.
//Interface Shape
interface Shape {
void printArea();
}
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 13 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
circle.printArea();
}
}
OUTPUT:
class Book1 {
private String name;
private String author;
private double price;
private int numOfPages;
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 15 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
}
}
}
OUTPUT:
3. Write a program that demonstrates handling of exceptions in inheritance. Create a base class called
“Father” and derived class called “Son” which extends the base class. In Father class, implement a
constructor which takes the age and throws the exception Wrong Age () i.e when the input age is equal
to father’s age.
import java.util.Scanner;
class Father
{
int Fage;
Scanner input = new Scanner(System.in);
Father()
{
System.out.println("Enter father's age:");
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 16 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
Fage=input.nextInt();
}
}
class Son extends Father
{
int Sage;
Scanner input = new Scanner(System.in);
Son()
{
//super();
System.out.println("Enter son's age:");
Sage=input.nextInt();
}
}
class ExceptionDemo
{
public static void main(String args[]) throws WrongAgeException
{
Son s=new Son();
try {
if(s.Sage>=s.Fage)
throw new WrongAgeException("Exception:");
else
System.out.println("You have entered a Valid Age");
}
catch(WrongAgeException e) {
System.out.println(e + " SON'S AGE >= FATHER'S AGE");
}
}
}
OUTPUT1:
OUTPUT2:
// Starting threads
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 18 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
collegeNameThread.start();
departmentThread.start();
}
}
OUTPUT:
6. Write a program that sorts an array of strings using compareTo() to determine bubble sort ordering.
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 19 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
}
OUTPUT:
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 20 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
}
}
OUTPUT:
import java.sql.*;
public class Database {
public static void main(String[] args) {
try{
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver loaded successfully");
Connection con=DriverManager.getConnection("jdbc:mysql:"
+ "//localhost:3306/student","root","root@123");
System.out.println("Connection established sucessfully"+con);
Statement stmt=con.createStatement();
stmt.executeUpdate("insert into student (Name, USN, Sem) "
+ "values ('Kennedy','4VV21CS001',3)");
ResultSet rs=stmt.executeQuery("select * from student");
System.out.println("Name \tUSN \t\tSem");
while(rs.next())
System.out.println(rs.getString(1)+"\t"+
rs.getString(2)+"\t"+rs.getInt(3));
con.close();
}
catch(Exception e) {
System.out.println(e);
}
}
}
OUTPUT:
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 21 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
Conenction established
sucessfully...com.mysql.cj.jdbc.ConnectionImpl@7d9d0818
Name USN Sem
Kennedy 4VV21CS001 3
Alice 4VV21IS001 4
Bob 4VV21IS002 3
Charlie 4VV21IS003 3
Danny 4VV21IS004 4
Esha 4VV21IS005 4
final.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 22 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
<p>Successfully odered</p>
</body>
</html>
OUTPUT:
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 23 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
APPENDIX
1. How to connect Java program with MySQL database?
Step 1: https://dev.mysql.com/downloads/installer/
i. Download and install mysql server or enterprise edition and J connector.
ii. Note the port number(default:3306), username and password during installation.
Step 2: Open mysql command line from start. -->Enter password(entered during installation).
i. Create a database.
ii. use database.
iii. create a table.
iv. Enter 1 or more rows in table.
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 24 Dept. of IS&E, VVCE, Mysuru - 02
Vidyavardhaka College of Engineering
Gokulam III stage, Mysuru – 570 002
Autonomous Institute under Visvesvaraya Technological University (VTU)
Accredited by NBA (2020- 2023) & NAAC with ‘A’ Grade (2018 - 2023)
Experimental Weightage:
Course Outcomes:
CO1 Apply and demonstrate the usage of various programming constructs
CO2 Design and Develop applications to solve problems across various technical and
real-world domains
CO-PO-PSO Mapping
PO PSO
CO
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1 3 3
CO2 3 3
CO3 2 2 2
AV
3 3 2 2 2.66
G.
----------------------------------------------------------------------------------------------------------------------------- ---------
Manjesh R, Assistant Professor 25 Dept. of IS&E, VVCE, Mysuru - 02