Java Labb
Java Labb
Semester: IV
List of Programs
1
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 1
1. Algorithm:
Note: Installing Java requires that you can gain administrator access to Windows on your
computer.
2
Lab Manual: Java Lab Department of CSE-
AIML
Oracle has partnered with companies that offer various products. The installer may
present you with option to install these programs when you install Java. After ensuring
that the desired programs are selected, click the Next button to continue the installation.
A few brief dialogs confirm the last steps of the installation process; click Close on the
last dialog. This will complete Java installation process.
3
Lab Manual: Java Lab Department of CSE-
AIML
4
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 2
AIM:
Write a program that accepts two numbers from the user and print their sum.
PROGRAM:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = s.nextInt();
System.out.print("Enter second number: ");
int num2 = s.nextInt();
int sum = num1 + num2;
System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
s.close();
}
}
OUTPUT:
5
Lab Manual: Java Lab Department of CSE-
AIML
6
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 3
AIM:
PROGRAM:
import java.util.Scanner;
public class Main
{
// Method prototype for addition
public static int add(int n1, int n2)
{
return n1 + n2;
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = s.nextInt();
System.out.print("Enter second number: ");
int num2 = s.nextInt();
// Call the add method and store the result
int sum = add(num1, num2);
System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
s.close();
}
}
OUTPUT:
7
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 4
AIM:
PROGRAM:
OUTPUT:
8
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 5
AIM:
PROGRAM:
class Box {
double length, width, height;
// Default constructor
Box() {
length = width = height = 0;
}
9
Lab Manual: Java Lab Department of CSE-
AIML
OUTPUT:
10
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 6
PROGRAM:
// Base class
class Person {
String name;
int age;
// Constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
// Derived class
class Student extends Person {
String studentId;
String course;
// Constructor
Student(String name, int age, String studentId, String course) {
super(name, age); // Call parent class constructor
this.studentId = studentId;
this.course = course;
}
11
Lab Manual: Java Lab Department of CSE-
AIML
OUTPUT:
Name: Sandeep
Age: 26
Student ID: 0105AL23001
Course: Computer Science-AIML
12
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 7
AIM:
PROGRAM:
Key Concepts:
Here's a simple Java program that demonstrates the package concept, including how to:
1. Create a package
2. Define a class inside the package
3. Import and use the class from the package in another file
13
Lab Manual: Java Lab Department of CSE-
AIML
}
}
javac mypack/Student.java
javac Main.java
java Main
OUTPUT:
14
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 8
AIM:
PROGRAM:
// Main class
public class InterfaceDemo {
public static void main(String[] args) {
Operation op = new Operation();
OUTPUT:
Square of 4 is: 16
Cube of 3 is: 27
15
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 9
AIM:
PROGRAM:
Key Concepts:
import java.util.Scanner;
try {
// Input from user
System.out.print("Enter numerator: ");
int numerator = scanner.nextInt();
// Attempt division
int result = numerator / denominator;
16
Lab Manual: Java Lab Department of CSE-
AIML
OUTPUT:
Output 1:
Enter numerator: 10
Enter denominator: 0
Error: Division by zero is not allowed.
Program execution completed.
Output 2:
Enter numerator: 20
Enter denominator: 5
Result: 4
Program execution completed.
17
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 10
AIM:
PROGRAM:
// Constructor
MyThread(String name) {
threadName = name;
18
Lab Manual: Java Lab Department of CSE-
AIML
try {
} catch (InterruptedException e) {
// Main class
// Start threads
t1.start();
t2.start();
OUTPUT:
Thread-1 - Count: 1
Thread-2 - Count: 1
Thread-1 - Count: 2
Thread-2 - Count: 2
...
Thread-1 finished.
Thread-2 finished.
19
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 11
AIM:
Program to demonstrate JDBC concept using create a GUI based application for student
information.
PROGRAM: Here's a Java GUI-based application that demonstrates JDBC (Java Database
Connectivity) to manage student information. The program allows the user to input student
details (name, age, and course), save them to a database, and display all records.
Requirements:
Steps:
USE studentdb;
name VARCHAR(100),
age INT,
20
Lab Manual: Java Lab Department of CSE-
AIML
course VARCHAR(100)
);
2. Java Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
// DB credentials
// Components
JTextArea outputArea;
public StudentApp() {
// GUI Setup
setTitle("Student Information");
setSize(500, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
21
Lab Manual: Java Lab Department of CSE-
AIML
formPanel.add(new JLabel("Name:"));
formPanel.add(nameField);
formPanel.add(new JLabel("Age:"));
formPanel.add(ageField);
formPanel.add(new JLabel("Course:"));
formPanel.add(courseField);
formPanel.add(addButton);
formPanel.add(displayButton);
add(formPanel, BorderLayout.NORTH);
22
Lab Manual: Java Lab Department of CSE-
AIML
// Button Actions
void addStudent() {
return;
String sql = "INSERT INTO students (name, age, course) VALUES (?, ?, ?)";
stmt.setString(1, name);
stmt.setInt(2, Integer.parseInt(ageText));
stmt.setString(3, course);
stmt.executeUpdate();
ex.printStackTrace();
23
Lab Manual: Java Lab Department of CSE-
AIML
void displayStudents() {
outputArea.setText("");
while (rs.next()) {
ex.printStackTrace();
24
Lab Manual: Java Lab Department of CSE-
AIML
OUTPUT:
Features Demonstrated
JDBC Connection
Swing-based GUI
25
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 12
AIM:
PROGRAM:
import java.applet.Applet;
import java.awt.Graphics;
/*
</applet>
*/
javac HelloWorldApplet.java
<html>
<body>
26
Lab Manual: Java Lab Department of CSE-
AIML
</applet>
</body>
</html>
appletviewer HelloApplet.html
OUTPUT:
27
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 13
AIM:
PROGRAM:
Here's a simple Java Applet program that adds user controls like TextField, Button, and Label
to demonstrate interactive input/output using an applet.
Objective
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*
</applet>
*/
TextField nameField;
Button greetButton;
Label outputLabel;
28
Lab Manual: Java Lab Department of CSE-
AIML
// Initialize components
add(new Label("Name:"));
add(nameField);
add(greetButton);
add(outputLabel);
greetButton.addActionListener(this);
javac UserControlApplet.java
29
Lab Manual: Java Lab Department of CSE-
AIML
<html>
<body>
</applet>
</body>
</html>
appletviewer UserControlApplet.html
OUTPUT:
Features Demonstrated
30
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 14
AIM:
PROGRAM:
Here is a simple Java Swing application that demonstrates the basic concepts of Swing
components, event handling, and a GUI-based interaction.
Objective
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JTextField nameField;
JButton greetButton;
JLabel greetingLabel;
public SwingGreetingApp() {
31
Lab Manual: Java Lab Department of CSE-
AIML
setSize(350, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
// Initialize components
greetButton.addActionListener(this);
add(nameLabel);
add(nameField);
add(greetButton);
add(greetingLabel);
// Make it visible
setVisible(true);
32
Lab Manual: Java Lab Department of CSE-
AIML
if (!name.isEmpty()) {
} else {
new SwingGreetingApp();
How to Run
javac SwingGreetingApp.java
java SwingGreetingApp
OUTPUT:
Features Demonstrated
33
Lab Manual: Java Lab Department of CSE-
AIML
EXPERIMENT NO. - 15
AIM:
PROGRAM:
Here's a complete Java Servlet-based web application that demonstrates student registration
with session management.
Features
Technologies Used
Project Structure
StudentRegistrationApp/
├── WEB-INF/
│ ├── web.xml
│ └── classes/
│ └── StudentServlet.class
├── register.html
<!DOCTYPE html>
<html>
34
Lab Manual: Java Lab Department of CSE-
AIML
<head>
<title>Student Registration</title>
</head>
<body>
</form>
</body>
</html>
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
35
Lab Manual: Java Lab Department of CSE-
AIML
session.setAttribute("name", name);
session.setAttribute("email", email);
session.setAttribute("course", course);
response.setContentType("text/html");
// Display confirmation
out.println("<html><body>");
out.println("<h2>Registration Successful</h2>");
out.println("</body></html>");
<servlet>
<servlet-name>StudentServlet</servlet-name>
36
Lab Manual: Java Lab Department of CSE-
AIML
<servlet-class>StudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>
How to Deploy
StudentRegistrationApp/WEB-INF/classes/
http://localhost:8080/StudentRegistrationApp/register.html
OUTPUT:
37
Lab Manual: Java Lab Department of CSE-
AIML
JVM
JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the
runtime environment in which java byte code can be executed.
JVMs are available for many hardware and software platforms (so JVM is plate form
dependent).
JRE
JRE stands for Java Runtime Environment. It is the implementation of JVM and physically
exists.
JDK
JDK is an acronym for Java Development Kit. It physically exists. It contains JRE +
development tools.
Many types:
3. Class(Method) Area
4. Heap
5. Stack
6. Program Counter Register
7. Native Method Stack
Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the
byte code that have similar functionality at the same time, and hence reduces the amount of
time needed for compilation. Here the term “compiler” refers to a translator from the
instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
38
Lab Manual: Java Lab Department of CSE-
AIML
4) What is platform?
A platform is basically the hardware or software environment in which a program runs. There
are two types of platforms software-based and hardware-based. Java provides software-based
platform.
5) What is the main difference between Java platform and other platforms?
The Java platform differs from most other platforms in the sense that it's a software-based
platform that runs on top of other hardware-based platforms. It has two components:
1. Runtime Environment
2. API(Application Programming Interface)
6) What gives Java its 'write once and run anywhere' nature?
The bytecode. Java is compiled to be a byte code which is the intermediate language between
source code and machine code. This byte code is not platform specific and hence can be fed to
any platform.
7) What is classloader?
The classloader is a subsystem of JVM that is used to load classes and interfaces.There are
many types of classloaders e.g. Bootstrap classloader, Extension classloader, System
classloader, Plugin classloader etc.
Yes, save your java file by .java only, compile it by javac .java and run by java
yourclassname Let's take a simple example:
run it by java A
No.
39
Lab Manual: Java Lab Department of CSE-
AIML
10) If I don't provide any arguments on the command line, then the String array of Main
method will be empty or null?
11) What if I write static public void instead of public static void?
The local variables are not initialized to any default value, neither primitives nor object
references.
There is given more than 50 OOPs (Object-Oriented Programming and System) Viva
questions. But they have been categorized in many sections such as constructor Viva
questions, static Viva questions, Inheritance Viva questions, Abstraction Viva question,
Polymorphism Viva questions etc. for better understanding.
13) What is difference between object oriented programming language and object based
programming language?
Object based programming languages follow all the features of OOPs except Inheritance.
Examples of object based programming languages are JavaScript, VBScript etc.
14) What will be the initial value of an object reference which is defined as an instance
variable?
Ans:yes, that is current instance (You cannot use return type yet it returns a value).
40
Lab Manual: Java Lab Department of CSE-
AIML
because object is not required to call static method if It were non-static method,jvm creates
object first then call main() method that will lead to the problem of extra memory allocation.
25) What if the static modifier is removed from the signature of the main method?
26) What is difference between static (class) method and instance method?
static or class method instance method
1)A method i.e. declared as static is known as static method. A method i.e. not
declared as static is
known as instance
method.
41
Lab Manual: Java Lab Department of CSE-
AIML
directly. be accessed in
instance methods.
4)For example: public static int cube(int n){ return n*n*n;} For example:
public void msg()
{...}.
Inheritance is a mechanism in which one object acquires all the properties and behavior of
another object of another class. It represents IS-A relationship. It is used for Code Reusability
and Method Overriding.
Object class.
42
Lab Manual: Java Lab Department of CSE-
AIML
Signature
43