EXPERIMENT-1
1.1.1 Write a Java program that demonstrates the default values of different
primitive data types
Source code:
package q55517;
public class DefaultValues {
byte defaultByte;
short defaultShort;
int defaultInt;
long defaultLong;
float defaultFloat;
double defaultDouble;
char defaultChar;
boolean defaultBoolean;
String defaultString;
public static void main(String[] args) {
DefaultValues obj = new DefaultValues();
[Link]("byte: " + [Link]);
[Link]("short: " + [Link]);
[Link]("int: " + [Link]);
[Link]("long: " + [Link]);
[Link]("float: " + [Link]);
[Link]("double: " + [Link]);
[Link]("char: [" + [Link] + "]");
[Link]("boolean: " + [Link]);
[Link]("String: " + [Link]);
}
}
Expected output Actual output
Expected output Actual output
byte:·0 byte:·0
short:·0 short:·0
int:·0 int:·0
long:·0 long:·0
float:·0.0 float:·0.0
double:·0.0 double:·0.0
char:·[] char:·[]
boolean:·false boolean:·false
String:·null String:·null
Write a Java program to find the roots of a quadratic equation of the form
Source code:
package q55519;
import [Link];
public class QuadraticEquation {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
double a = [Link]();
double b = [Link]();
double c = [Link]();
double D = b * b - 4 * a * c; // Compute the discriminant
if (D > 0) {
double root1 = (-b + [Link](D)) / (2 * a);
double root2 = (-b - [Link](D)) / (2 * a);
[Link]("two distinct real roots");
[Link]("Root 1: %.1f%n", root1);
[Link]("Root 2: %.1f%n", root2);
} else if (D == 0) {
double root = -b / (2 * a);
[Link]("one real root");
[Link]("Root: %.1f%n", root);
} else {
[Link]("no real roots");
}
[Link]();
}
Expected output Actual output
1 1
-3 -3
2 2
two·distinct·real·roots two·distinct·real·roots
Root·1:·2.0 Root·1:·2.0
Root·2:·1.0 Root·2:·1.0
Test case 2
Expected output Actual output
1 1
-2 -2
1 1
one·real·root one·real·root
Root:·1.0 Root:·1.0
Test case 3
Expected output Actual output
1 1
1 1
1 1
no·real·roots no·real·roots
EXPERIMENT:2
[Link] a Java program to implement binary search for finding an element in a
sorted array.
package q38068;
import [Link];
public class BinarySearch {
public static int binarySearch(int[] arr, int key) {
int left = 0;
int right = [Link] - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
return mid; // Element found
if (arr[mid] < key) {
left = mid + 1; // Search right half
} else {
right = mid - 1; // Search left half
}
return -1; // Element not found
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Number of elements: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
[Link]("Element to search: ");
int key = [Link]();
int result = binarySearch(arr, key);
if (result != -1) {
[Link]("Element found at index " + result);
} else {
[Link]("Element not found");
[Link]();
Expected output Actual output
Number·of·elements:·5 Number·of·elements:·5
Elements:⏎ Elements:⏎
10 20 30 40 50 10 20 30 40 50
Element·to·search:·30 Element·to·search:·30
Element·found·at·index·2 Element·found·at·index·2
Test case 2
Expected output Actual output
Number·of·elements:·4 Number·of·elements:·4
Elements: Elements:⏎
5 10 15 20 5 10 15 20
Element·to·search:·25 Element·to·search:·25
Element·not·found Element·not·found
[Link] a Java program to sort an array of n integers using the bubble
sort algorithm.
package q32083;
import [Link];
public class BubbleSort {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
bubbleSort(arr);
for (int num : arr) {
[Link](num + " ");
[Link]();
}
public static void bubbleSort(int[] arr) {
int n = [Link];
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
Expected output Actual output
5 5
12 35 69 48 51 12 35 69 48 51
12·35·48·51·69· 12·35·48·51·69·
Test case 2
Expected output Actual output
3 3
5 -1 25 5 -1 25
-1·5·25· -1·5·25·
2.1.3 Write a Java program that uses a StringBuffer to delete a character from a
given string based on the position provided by the user.
package q55527;
import [Link];
public class StringBufferExample {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Read input string
String inputString = [Link]();
// Read position
int position = [Link]();
StringBuffer stringBuffer = new StringBuffer(inputString);
if (position >= 0 && position < [Link]()) {
[Link](position);
[Link](stringBuffer);
} else {
[Link]("invalid position");
}
[Link]();
Expected output Actual output
Java Programming Java Programming
15 15
Java·Programming Java·Programming
Test case 2
Expected output Actual output
abcd 1234 abcd 1234
20 20
invalid·position invalid·position
Test case 3
Expected output Actual output
Programming Language Programming Language
11 11
ProgrammingLanguage ProgrammingLanguage
Experiment:3
3.1 Write a Java program to implement a basic calculator using a class mechanism.
Source code:
package q55543;
import [Link];
class Calculator {
public int add(int a, int b) {
return a + b;
public int subtract(int a, int b) {
return a - b;
public int multiply(int a, int b) {
return a * b;
public Double divide(int a, int b) {
if (b == 0) {
[Link]("error division");
return null;
return (double) a / b;
public class Calc {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Calculator calculator = new Calculator();
int num1 = [Link]();
int num2 = [Link]();
[Link]("Addition: %d%n", [Link](num1, num2));
[Link]("Subtraction: %d%n", [Link](num1, num2));
[Link]("Multiplication: %d%n", [Link](num1, num2));
Double divisionResult = [Link](num1, num2);
if (divisionResult != null) {
[Link]("Division: %.2f%n", divisionResult);
[Link]();
Expected output Actual output
5 5
10 10
Addition:·15 Addition:·15
Subtraction:·-5 Subtraction:·-5
Multiplication:·50 Multiplication:·50
Division:·0.50 Division:·0.50
Test case 2
Expected output Actual output
20 20
30 30
Addition:·50 Addition:·50
Subtraction:·-10 Subtraction:·-10
Multiplication:·600 Multiplication:·600
Division:·0.67 Division:·0.67
Test case 3
Expected output Actual output
5 5
0 0
Addition:·5 Addition:·5
Subtraction:·5 Subtraction:·5
Multiplication:·0 Multiplication:·0
error·division error·division
3.1.2:Create a Java class named Fun_overloading1. The class should include
multiple sum() methods with different parameter lists.
Source code:
package q39906;
import [Link];
public class FunOverloading1 {
// Method to calculate the sum of two integers
public static void sum(int a, int b) {
[Link](a + b);
}
// Method to calculate the sum of an integer and a float
public static void sum(int a, float b) {
[Link]("%.2f%n", a + b);
// Method to calculate the sum of a float and a double
public static void sum(float a, double b) {
[Link]("%.2f%n", a + b);
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input for two integers
int int1 = [Link]();
int int2 = [Link]();
// Input for integer and float
int intValue = [Link]();
float floatValue = [Link]();
// Input for float and double
float floatValue2 = [Link]();
double doubleValue = [Link]();
// Close the scanner
[Link]();
// Output results
sum(int1, int2); // Sum of integers
sum(intValue, floatValue); // Sum of integer and float
sum(floatValue2, doubleValue); // Sum of float and double
Test case 1
Expected output Actual output
7 12 7 12
4 5.678 4 5.678
3.45 6.789 3.45 6.789
19⏎ 19⏎
9.68⏎ 9.68⏎
10.24⏎ 10.24⏎
Test case 2
Expected output Actual output
8 13 8 13
5 6.789 5 6.789
4.56 7.890 4.56 7.890
Expected output Actual output
21 21
11.79 11.79
12.45 12.45
3.1.3: Write a Java program that demonstrates the use of constructors in a class
Source code:
package q55542;
import [Link];
class Person {
private String name;
private int age;
// Constructor to initialize name and age
public Person(String name, int age) {
[Link] = name;
[Link] = age;
// Method to display details
public void displayDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
public class Constructor {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Read input
String name = [Link]();
int age = [Link]();
// Create Person object and display details
Person person = new Person(name, age);
[Link]();
[Link]();
Test case 1
Expected output Actual output
Alice Alice
25 25
Name:·Alice Name:·Alice
Age:·25 Age:·25
Test case 2
Expected output Actual output
Bob Bob
29 29
Name:·Bob Name:·Bob
Age:·29 Age:·29