Practical 1
a. Write a Java program to print 'Hello World!'
Code:
class Hello {
public static void main(String[] args){
[Link]("Hello World!");
[Link]("Name: Jayveer singh ");
}
}
Output:
Hello World!
Name: Jayveer singh
b. Ensure JDK is correctly installed and configured in your IDE.
JDK (Java Development Kit) is a complete package used for developing Java programs.
It
contains:
JRE (Java Runtime Environment)
Compiler (javac)
Tools like debugger, archiver, etc.
JDK = Everything needed to write + compile + run Java programs
What is JRE?
JRE (Java Runtime Environment) is used to run Java programs only. It contains:
JVM (Java Virtual Machine)
Libraries and supporting files
JRE = Only needed to run Java programs (not for development)Difference Between JDK
and JRE
1
Practical 2. Variables, Data Types, and Operators
a. Write a program to calculate the area of a rectangle given its length and width.
Code:
class Rect {
public static void main(String[] args){
int l=6,w=3;
[Link]("Area="+(l*w));
[Link]("Name: Jayveer singh ");
}
}
Output:
Area=18
Name: Jayveer singh
b. Implement a temperature converter program that converts Celsius to Fahrenheit.
Code:
class Temp {
public static void main(String[] args){
int c=30;
float f=(c*9/5)+32;
[Link]("F="+f);
[Link]("Name: Jayveer singh ");
}
}
Output:
F=86.0
Name: Jayveer singh
2
Practical 3. Control Statements
a. Create a program that checks whether a given number is even or odd using if-else
statements.
Code:
class EO {
public static void main(String[] args){
int n=8;
if(n%2==0) [Link]("Even");
else [Link]("Odd");
[Link]("Name: Jayveer singh ");
}
}
Output:
Even
Name: Jayveer singh
b. Implement a calculator program using switch-case statements for basic arithmetic
operations.
Code:
class Calc {
public static void main(String[] args){
int a=10,b=2;
char op='*';
switch(op){
case '+': [Link](a+b); break;
case '-': [Link](a-b); break;
case '*': [Link](a*b); break;
case '/': [Link](a/b); break;
}
[Link]("Name: Jayveer singh ");
}
}
Output:
20
Name: Jayveer singh
3
Practical 4. Object Creation
Create a class Student with attributes such as name, age, and grade. Write methods to
set and get these attributes and demonstrate object creation.
Code:
class Student {
String name="Jayveer singh ";
int age=21;
void show(){
[Link](name+" "+age);
}
public static void main(String[] args){
new Student().show();
}
}
Output:
Jayveer singh 21
Practical 5. Inheritance and Polymorphism
a. Define a base class Shape with methods to calculate area and perimeter.
Code:
class Shape {
void area() {
[Link]("Area");
void perimeter() {
[Link]("Perimeter");
4
public class Main {
public static void main(String[] args) {
Shape s = new Shape();
[Link]();
[Link]();
Output:
Area
Perimeter
b. Create derived classes Rectangle and Circle that inherit from Shape and override
these methods.
Code:
class Rectangle extends Shape {
void area(){[Link]("Rectangle Area");}
}
class Circle extends Shape {
void area(){[Link]("Circle Area");}
}
class Test {
public static void main(String[] args){
new Rectangle().area();
new Circle().area();
[Link]("Name: Jayveer singh ");
}
}
Output:
Rectangle Area
Circle Area
Name: Jayveer singh
5
Practical 6. Interfaces and Abstract Classes
a. Define an interface Drawable with a method draw() and implement it in classes
like Circle, Rectangle, etc.
Code:
interface Drawable {
void draw();
}
class Rect implements Drawable {
public void draw(){[Link]("Rectangle");}
}
class Test {
public static void main(String[] args){
new Rect().draw();
[Link]("Name: Jayveer singh ");
}
}
Output:
Rectangle
Name: Jayveer singh
b. Create an abstract class Vehicle with abstract methods like start() and stop(), and
implement it in derived classes Car and Motorcycle.
Code:
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start(){[Link]("Car Start");}
}
class Test {
public static void main(String[] args){
new Car().start();
[Link]("Name: Jayveer singh ");
}
}
6
Output:
Car Start
Name: Jayveer singh
Practical 7. Packages and Access Modifiers
a. Create a package [Link] and move the Rectangle and Circle classes
into it.
Code:
package [Link];
public class Rectangle {
public void area() {
[Link]("Area of Rectangle");
public void perimeter() {
[Link]("Perimeter of Rectangle");
// Circle class
public class Circle {
public void area() {
[Link]("Area of Circle");
public void perimeter() {
[Link]("Perimeter of Circle");
} }
7
b. Demonstrate usage of access modifiers (public, private, protected, default) across
classes.
Access Modifiers in Java control the visibility of variables and methods.
Types:
• private → accessible only within the same class
• public → accessible from anywhere
• protected → accessible within same package + subclasses
• default → accessible within the same package
Code:
class Demo {
private int x = 5; // private variable
public int y = 10; // public variable
protected int z = 15; // protected variable
int a = 20; // default variable
public static void main(String[] args) {
Demo d = new Demo();
[Link](d.x);
[Link](d.y);
[Link](d.z);
[Link](d.a);
[Link]("Name: Jayveer singh ");
8
Output:
5
10
15
20
Name: Jayveer singh
Practical 8. Working with Strings
a. Write a program to reverse a given string using StringBuffer or StringBuilder.
Code:
class Rev {
public static void main(String[] args){
String s="Java";
[Link](new StringBuilder(s).reverse());
[Link]("Name: Jayveer singh ");
}
}
Output:
avaJ
Name: Jayveer singh
b. Implement a program to count occurrences of a specific word in a sentence using
String methods.
Code:
class Count {
public static void main(String[] args){
String s="hello hello";
int count=[Link]("hello",-1).length-1;
[Link](count);
[Link]("Name: Jayveer singh ");
9
}
}
Output:
2
Name: Jayveer singh
Practical 9. Collections Framework
a. Create a program to manage a list of student names using ArrayList.
Code:
import [Link].*;
class A {
public static void main(String[] args) {
ArrayList<String> l = new ArrayList<>();
// Adding student names
[Link]("Ram");
[Link]("Shyam");
// Displaying the list
[Link](l);
[Link]("Name: Jayveer singh ");
Output:
[Ram, Shyam]
Name: Jayveer singh
b. Implement a program using HashMap to store student IDs and names, and perform
basic operations like adding, retrieving, and iterating.
10
Code:
import [Link].*;
class H {
public static void main(String[] args){
HashMap<Integer,String> m=new HashMap<>();
[Link](1,"A");
[Link](m);
[Link]("Name: Jayveer singh ");}}
Output:
{1=A}
Name: Jayveer singh
Practical 10. Exception Handling
a. Write a program to handle ArithmeticException and NullPointerException.
Code:
class Ex {
public static void main(String[] args){
try{int a=10/0;}
catch(Exception e){[Link]("Error");}
[Link]("Name: Jayveer singh ");}}
Output:
Error
Name: Jayveer singh
b. Create a custom exception InvalidAgeException and use it in a program to validate
age.
Code:
class MyEx extends Exception{}
class Test{
public static void main(String[] args){
try{throw new MyEx();}
catch(Exception e){[Link]("Custom Exception");}
[Link]("Name: Jayveer singh ");}}
11
Output:
Custom Exception
Name: Jayveer singh
Practical 11. AWT Basics
Develop a simple GUI application using Frame, Button, and Label to perform basic
operations (e.g., calculator).
import [Link].*;
import [Link].*;
public class SimpleCalculator extends Frame implements ActionListener {
Label l1, l2, result;
TextField t1, t2;
Button add, sub, mul, div;
SimpleCalculator() {
setLayout(new FlowLayout());
l1 = new Label("Enter First Number:");
t1 = new TextField(10);
l2 = new Label("Enter Second Number:");
t2 = new TextField(10);
add = new Button("Add")
sub = new Button("Subtract");
mul = new Button("Multiply");
div = new Button("Divide");
result = new Label("Result: ");
add(l1); add(t1);
add(l2); add(t2);
add(add); add(sub); add(mul); add(div);
12
add(result);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
setTitle("Simple Calculator");
setSize(300, 250);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose(); } }); }
public void actionPerformed(ActionEvent e) {
try {
double num1 = [Link]([Link]());
double num2 = [Link]([Link]());
double res = 0;
if ([Link]() == add) {
res = num1 + num2;
} else if ([Link]() == sub) {
res = num1 - num2;
} else if ([Link]() == mul) {
res = num1 * num2;
} else if ([Link]() == div) {
res = num1 / num2; }
[Link]("Result: " + res);
13
} catch (Exception ex) {
[Link]("Invalid Input!");}}
public static void main(String[] args) {
new SimpleCalculator; }
Output:
14
Practical 12. Java Swing
a. Create a more advanced GUI application using JFrame, JPanel, JButton, and
JTextField.
import [Link].*;
import [Link].*;
import [Link].*;
public class main extends JFrame {
private JPanel mainPanel, inputPanel, buttonPanel, displayPanel;
private JTextField nameField, emailField, ageField, resultField;
private JButton addButton, clearButton, submitButton;
private JTextArea outputArea;
private JLabel titleLabel, nameLabel, emailLabel, ageLabel;
public main() {
// Frame settings
setTitle("Advanced User Information System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 500);
setLocationRelativeTo(null);
setResizable(true);
// Main panel with BorderLayout
mainPanel = new JPanel(new BorderLayout(10, 10));
[Link]([Link](15, 15, 15, 15));
15
[Link](new Color(240, 240, 240));
// Title label
titleLabel = new JLabel("User Registration Form");
[Link](new Font("Arial", [Link], 24));
[Link]([Link]);
[Link](new Color(0, 102, 204));
[Link](titleLabel, [Link]);
// Input panel with GridLayout
inputPanel = new JPanel(new GridLayout(3, 2, 10, 10));
[Link](new Color(240, 240, 240));
// Name field
nameLabel = new JLabel("Name:");
[Link](new Font("Arial", [Link], 12));
nameField = new JTextField();
[Link](new Font("Arial", [Link], 12));
[Link](nameLabel);
[Link](nameField);
// Email field
emailLabel = new JLabel("Email:");
[Link](new Font("Arial", [Link], 12));
emailField = new JTextField();
16
[Link](new Font("Arial", [Link], 12));
[Link](emailLabel);
[Link](emailField);
// Age field
ageLabel = new JLabel("Age:");
[Link](new Font("Arial", [Link], 12));
ageField = new JTextField();
[Link](new Font("Arial", [Link], 12));
[Link](ageLabel);
[Link](ageField);
[Link](inputPanel, [Link]);
// Button panel
buttonPanel = new JPanel(new FlowLayout([Link], 10, 10));
[Link](new Color(240, 240, 240));
addButton = new JButton("Add User");
[Link](new Font("Arial", [Link], 12));
[Link](new Color(0, 153, 76));
[Link]([Link]);
[Link](false);
[Link](new Cursor(Cursor.HAND_CURSOR));
[Link](new ActionListener() {
17
@Override
public void actionPerformed(ActionEvent e) {
addUser();
});
clearButton = new JButton("Clear");
[Link](new Font("Arial", [Link], 12));
[Link](new Color(255, 102, 0));
[Link]([Link]);
[Link](false);
[Link](new Cursor(Cursor.HAND_CURSOR));
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearFields();
});
submitButton = new JButton("Submit");
[Link](new Font("Arial", [Link], 12));
[Link](new Color(0, 102, 204));
[Link]([Link]);
[Link](false);
[Link](new Cursor(Cursor.HAND_CURSOR));
18
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
submitForm();
});
[Link](addButton);
[Link](clearButton);
[Link](submitButton);
[Link](buttonPanel, [Link]);
// Display panel with JTextArea
displayPanel = new JPanel(new BorderLayout(10, 10));
[Link](new Color(220, 220, 220));
[Link]([Link]("User Records"));
outputArea = new JTextArea();
[Link](false);
[Link](new Font("Courier New", [Link], 11));
[Link]([Link]);
[Link](true);
[Link](true);
19
JScrollPane scrollPane = new JScrollPane(outputArea);
[Link](new Dimension(550, 150));
[Link](scrollPane, [Link]);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, mainPanel,
displayPanel);
[Link](0.6);
add(splitPane);
setVisible(true);
private void addUser() {
String name = [Link]().trim();
String email = [Link]().trim();
String age = [Link]().trim();
if ([Link]() || [Link]() || [Link]()) {
[Link](this, "Please fill all fields!", "Input Error",
JOptionPane.ERROR_MESSAGE);
return;
try {
int ageValue = [Link](age);
if (ageValue < 0 || ageValue > 150) {
20
[Link](this, "Please enter a valid age!", "Input
Error", JOptionPane.ERROR_MESSAGE);
return;
} catch (NumberFormatException e) {
[Link](this, "Age must be a number!", "Input Error",
JOptionPane.ERROR_MESSAGE);
return;
String record = [Link]("Name: %-20s | Email: %-25s | Age: %-3s\n", name,
email, age);
[Link](record);
[Link](this, "User added successfully!", "Success",
JOptionPane.INFORMATION_MESSAGE);
clearFields();
private void clearFields() {
[Link]("");
[Link]("");
[Link]("");
[Link]();
private void submitForm() {
21
int count = [Link]().split("\n").length - 1;
if (count == 0) {
[Link](this, "No users added yet!", "Info",
JOptionPane.INFORMATION_MESSAGE);
} else {
[Link](this, "Total users registered: " + count,
"Submission Report", JOptionPane.INFORMATION_MESSAGE);
public static void main(String[] args) {
[Link](new Runnable() {
@Override
public void run() {
new main();
});
Output:
22
b. Use different layout managers (FlowLayout, BorderLayout, GridLayout) to organize
components.
Code:
import [Link].*;
import [Link].*;
import [Link].*;
public class MultiLayoutCalculator {
public static void main(String[] args) {
JFrame frame = new JFrame("Multi Layout Calculator");
[Link](400, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new BorderLayout());
JPanel topPanel = new JPanel();
[Link](new FlowLayout());
JLabel label = new JLabel("Calculator");
JTextField display = new JTextField(20);
[Link](label);
[Link](display);
[Link](topPanel, [Link]);
JPanel centerPanel = new JPanel();
[Link](new GridLayout(4, 4, 5, 5));
String[] buttons = {
"7","8","9","/",
"4","5","6","*",
"1","2","3","-",
"0","C","=","+" };
23
for (String text : buttons) {
JButton btn = new JButton(text);
[Link](btn);
[Link](centerPanel, [Link]);
JPanel bottomPanel = new JPanel();
[Link](new FlowLayout());
JButton clearBtn = new JButton("Clear");
JButton exitBtn = new JButton("Exit");
[Link](clearBtn);
[Link](exitBtn);
[Link](bottomPanel, [Link]);
[Link](e -> [Link](0));
[Link](e -> [Link](""));
[Link](true); }
Output:
24