B.
Tech
CSE- 5thSem
Session 2023-27
PROGRAMMING IN JAVA LAB
(LC-CSE-327G)
Submitted To Submitted By
Mr. Chanchal Rohilla Kashish
142922
Certificate
This is to certify that Kashish d/o Mr. Prem Singh Kaushik has submitted a
practical file for fulfilment of Btech 5th sem lab course for Programming In Java
Lab.
Submitted To Submitted By
Mr. Chanchal Rohilla Kashish
142922
INDEX
S. EXPERIMENT DATE SIGNATURE
NO.
1. Write a program that accepts two numbers from the
user and print their sum.
2. Write a program to demonstrate function
overloading.
3. Write a program to demonstrate overloaded
constructor for calculating box volume.
4. WRITE A PROGRAM TO SHOW THE DETAIL OF
STUDENTS USING CONCEPT OF INHERITANCE.
5. Write a program to demonstrate package concept.
6. Write a program to demonstrate implementation of
an interface.
7. Write a program to demonstrate exception handling
in case of division by zero error.
8. Write a program to demonstrate multithreading in
java.
9. Write a program to add user controls to applets.
10. Write a program to create an application using
swing.
EXPERIMENT 1: Write a program that accepts two numbers from the user and
print their sum.
INPUT:
import [Link]; // Import the Scanner class to read user input
public class SumOfTwoNumbers {
public static void main(String[] args) {
// Create a Scanner object to read input from the console
Scanner input = new Scanner([Link]);
// Prompt the user to enter the first number
[Link]("Enter the first number: ");
// Read the first integer input from the user
int number1 = [Link]();
// Prompt the user to enter the second number
[Link]("Enter the second number: ");
// Read the second integer input from the user
int number2 = [Link]();
// Calculate the sum of the two numbers
int sum = number1 + number2;
// Print the sum to the console
[Link]("The sum is: " + sum);
// Close the scanner to release system resources
[Link]();
}
}
EXPERIMENT 2: Write a program to demonstrate function overloading.
INPUT:
class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b;
// Overloaded method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
// Overloaded method to add two doubles
public double add(double a, double b) {
return a + b;
// Overloaded method to concatenate two strings
public String add(String s1, String s2) {
return s1 + s2;
public class MethodOverloadingExample {
public static void main(String[] args) {
Calculator calc = new Calculator();
// Calling the add method with two integers
[Link]("Sum of two integers: " + [Link](10, 20));
// Calling the add method with three integers
[Link]("Sum of three integers: " + [Link](10, 20, 30));
// Calling the add method with two doubles
[Link]("Sum of two doubles: " + [Link](10.5, 20.3));
// Calling the add method with two strings
[Link]("Concatenated string: " + [Link]("Hello", " World"));
OUTPUT:
EXPERIMENT 3: Write a program to demonstrate overloaded constructor for
calculating box volume.
INPUT:
// Java program to illustrate
// Constructor Overloading
class Box {
double width, height, depth;
// constructor used when all dimensions
// specified
Box(double w, double h, double d)
width = w;
height = h;
depth = d;
// constructor used when no dimensions
// specified
Box() { width = height = depth = 0; }
// constructor used when cube is created
Box(double len) { width = height = depth = len; }
// compute and return volume
double volume() { return width * height * depth; }
// Driver code
public class Test {
public static void main(String args[])
// create boxes using the various
// constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = [Link]();
[Link]("Volume of mybox1 is " + vol);
// get volume of second box
vol = [Link]();
[Link]("Volume of mybox2 is " + vol);
// get volume of cube
vol = [Link]();
[Link]("Volume of mycube is " + vol);
OUTPUT:
EXPERIMENT 4: WRITE A PROGRAM TO SHOW THE DETAIL OF STUDENTS
USING CONCEPT OF INHERITANCE.
INPUT:
// Parent class representing a generic Person
class Person {
String name;
int age;
// Constructor for the Person class
public Person(String name, int age) {
[Link] = name;
[Link] = age;
// Method to display basic person details
public void displayPersonDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
// Child class representing a Student, inheriting from Person
class Student extends Person {
int studentId;
String major;
// Constructor for the Student class
public Student(String name, int age, int studentId, String major) {
super(name, age); // Call the constructor of the parent class (Person)
[Link] = studentId;
[Link] = major;
}
// Method to display student-specific details
public void displayStudentDetails() {
displayPersonDetails(); // Call the parent class method to display common details
[Link]("Student ID: " + studentId);
[Link]("Major: " + major);
// Main class to test the inheritance concept
public class StudentDetails {
public static void main(String[] args) {
// Create a Student object
Student student1 = new Student("Alice Smith", 20, 1001, "Computer Science");
// Display the details of the student
[Link]("Student Details:");
[Link]();
[Link]("\n--- Another Student ---");
Student student2 = new Student("Bob Johnson", 22, 1002, "Electrical Engineering");
[Link]();
}
OUTPUT:
EXPERIMENT 5: Write a program to demonstrate package concept.
INPUT:
package mypack; // Declaring package
public class Message {
public void show() {
[Link]("Hello! This is a message from the mypack package.");
}import [Link]; // Importing the package class
public class TestPackage {
public static void main(String[] args) {
Message msg = new Message();
[Link]();
Step 1: Compile the package class
javac mypack/[Link]
Step 2: Compile the main class
javac [Link]
Step 3: Run the program
java TestPackage
OUTPUT:
EXPERIMENT 6: Write a program to demonstrate implementation of an
interface.
INPUT:
interface Greeting {
void greet(); // method declaration
class EnglishGreeting implements Greeting {
public void greet() {
[Link]("Hello, world!");
public class ImplementingInterface {
public static void main(String[] args) {
Greeting greeting = new EnglishGreeting(); // interface reference
[Link](); // calling implemented method
OUTPUT:
EXPERIMENT 7: Write a program to demonstrate exception handling in case
of division by zero error.
INPUT:
// Java Program to Handle Divide By Zero Exception
import [Link].*;
class GFG {
public static void main(String[] args)
int a = 6;
int b = 0;
[Link](a / b);
// this line Throw ArithmeticException: / by zero
OUTPUT:
Experiment 8: Write a program to demonstrate multithreading in java.
INPUT: class CookingTask extends Thread {
private String task;
CookingTask(String task) {
[Link] = task;
public void run() {
[Link](task + " is being prepared by " +
[Link]().getName());
public class Restaurant {
public static void main(String[] args) {
Thread t1 = new CookingTask("Pasta");
Thread t2 = new CookingTask("Salad");
Thread t3 = new CookingTask("Dessert");
Thread t4 = new CookingTask("Rice");
[Link]();
[Link]();
[Link]();
[Link]();
OUTPUT:
EXPERIMENT 9: Write a program to add user controls to applets.
INPUT:
// A Hello World Applet
// Save file as [Link]
import [Link];
import [Link];
/*
<applet code="HelloWorld" width=200 height=60>
</applet>
*/
// HelloWorld class extends Applet
public class HelloWorld extends Applet
// Overriding paint() method
@Override
public void paint(Graphics g)
[Link]("Hello World", 20, 20);
OUTPUT:
appletviewer HelloWorld
EXPERIMENT 10: Write a program to create an application using swing.
INPUT:
// Java program using label (swing)
// to display the message “GFG WEB Site Click”
import [Link].*;
import [Link].*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
// Creating instance of JFrame
JFrame frame = new JFrame();
// Creating instance of JButton
JButton button = new JButton(" GFG WebSite Click");
// x axis, y axis, width, height
[Link](150, 200, 220, 50);
// adding button in JFrame
[Link](button);
// 400 width and 500 height
[Link](500, 600);
// using no layout managers
[Link](null);
// making the frame visible
[Link](true);
OUTPUT: