0% found this document useful (0 votes)
0 views15 pages

Java_Assignment

The document contains multiple Java assignments that cover various programming concepts, including binary search, array manipulation (insertion and removal), finding pairs with a specific sum, matrix operations, a basic calculator, and operations on complex numbers. Each assignment includes a problem statement, source code, example output, and a discussion explaining the functionality and complexity of the code. The assignments demonstrate fundamental programming techniques and data structures in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views15 pages

Java_Assignment

The document contains multiple Java assignments that cover various programming concepts, including binary search, array manipulation (insertion and removal), finding pairs with a specific sum, matrix operations, a basic calculator, and operations on complex numbers. Each assignment includes a problem statement, source code, example output, and a discussion explaining the functionality and complexity of the code. The assignments demonstrate fundamental programming techniques and data structures in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Assignment – 1

Date: 15.05.2025
Problem Statement:
Write a Java program to implement binary search.
Source Code:
import java.util.*;
class Bin{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter the range of the array: ");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter "+n+" elements: ");
for(int i=0;i<n;i++){
System.out.print(i+"th element: ");
a[i]=sc.nextInt();
}
int t;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(a[j] >a[j+1]){
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
System.out.print("Enter the value to search in array: ");
int k=sc.nextInt();
int m;
int f=0;
int l=n-1;
int flag=0;
while(f <= l){
m=(f+l)/2;
if(a[m] < k){
f=m+1;
}
else if(a[m] == k){
flag=1;
break;
}

1|Page
else if(a[m] > k){
l=m-1;
}
}
if(flag==1){
System.out.print(k+" is in the array.");
}
else{
System.out.print(k+" is not in the array.");
}
}
}

Output:
Enter the range of the array: 5
Enter 5 elements:
0th element: 67
1th element: 99
2th element: 76
3th element: 55
4th element: 23
Enter the value to search in array: 98
98 is not in the array.

Enter the range of the array: 5


Enter 5 elements:
0th element: 67
1th element: 99
2th element: 76
3th element: 55
4th element: 23
Enter the value to search in array: 23
23 is in the array.

Discussion:
The Java code allows users to input an array of integers, sorts it with Bubble Sort, and searches
for a value using Binary Search. It demonstrates basic data handling, with Bubble Sort having a
time complexity of \(O(n^2)\) and Binary Search \(O(\log n)\), ultimately informing the user if
the searched value exists in the array.

2|Page
Assignment – 2
Date: 26.05.2025
Problem Statement:
Write a Java program to insert an element at specific position of an array.

Source Code:
import java.util.*;
public class Insert {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int s = scanner.nextInt();
int[] array = new int[s + 1];
System.out.println("Enter " + s + " elements of the array:");
for (int i = 0; i < s; i++) {
array[i] = scanner.nextInt();
}
System.out.print("Enter the element to insert: ");
int element = scanner.nextInt();
System.out.print("Enter the position (1 to " + (s + 1) + ") to insert the element:");
int pos = scanner.nextInt();
if (pos < 1 || pos > s + 1) {
System.out.println("Invalid position!");
return;
}
for (int i = s; i >= pos; i--) {
array[i] = array[i - 1];
}
array[pos - 1] = element;
System.out.println("Array after insertion:");
for (int i = 0; i <= s; i++) {
System.out.print(array[i] + " ");
}
}
}

Output:
Enter the size of the array: 5
Enter 5 elements of the array:
67 55 90 21 43
Enter the element to insert: 77
Enter the position (1 to 6) to insert the element:6
Array after insertion:
67 55 90 21 43 77

Enter the size of the array: 5


Enter 5 elements of the array:
67 55 90 21 43

3|Page
Enter the element to insert: 33
Enter the position (1 to 6) to insert the element:4
Array after insertion:
67 55 90 33 21 43
Discussion:
The provided Java code implements a program that allows users to insert an element into a
specific position in an array. It begins by prompting the user for the size of the array and its
elements, utilizing the Scanner class for input. The program then asks for the element to be
inserted and the desired position. It checks if the position is valid (between 1 and the size of the
array plus one) and, if valid, shifts the existing elements to the right to create space for the new
element. Finally, it inserts the element and displays the updated array. This code effectively
demonstrates basic array manipulation techniques in Java, including element insertion and
boundary checking.

4|Page
Assignment – 3
Date: 26.05.2025
Problem Statement:
Write a Java program to remove an element from specific position of an
array.

Source Code:
import java.util.*;
class Remove {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int s = scanner.nextInt();
int[] arr = new int[s];
System.out.println("Enter " + s + " elements of the array:");
for (int i = 0; i < s; i++) {
arr[i] = scanner.nextInt();
}
System.out.print("Enter the position (1 to " + s + ") of the element to remove: ");
int position = scanner.nextInt();
if (position < 1 || position > s) {
System.out.println("Invalid position! Please run the program again.");
return;
}
for (int i = position - 1; i < s - 1; i++) {
arr[i] = arr[i + 1];
}
System.out.println("Array after removal:");
for (int i = 0; i < s - 1; i++) {
System.out.print(arr[i] + " ");
}
}
}
Output:
Enter the size of the array: 5
Enter 5 elements of the array:
43 22 31 90 76
Enter the position (1 to 5) of the element to remove: 3
Array after removal:
43 22 90 76

5|Page
Discussion:
The provided Java code implements a program that allows users to remove an element from a
specified position in an array. It starts by prompting the user for the size of the array and its
elements, using the Scanner class for input. After reading the array, the program asks for the
position of the element to be removed and checks if the position is valid (between 1 and the
size of the array). If valid, it shifts the subsequent elements to the left to fill the gap created by
the removed element. Finally, it displays the updated array, demonstrating basic array
manipulation techniques in Java, including element removal and boundary checking.

6|Page
Assignment – 4
Date: 26.05.2025

Problem Statement:
Write a Java program to find all the pairs in an array whose sum is equal to
a specific number.

Source Code:
import java.util.*;
class Pair {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = sc.nextInt();
while (n <= 0) {
System.out.print("Enter a positive integer: ");
n = sc.nextInt();
}
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter the target sum: ");
int target = sc.nextInt();
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] == target) {
System.out.println("(" + arr[i] + ", " + arr[j] + ")");
count++;
}
}
}
}
}
Output:
Enter the size of the array: 10
Enter the elements of the array:
1 2 3 4 5 6 7 9 8 11

7|Page
Enter the target sum: 9
(1, 8)
(2, 7)
(3, 6)
(4, 5)
Discussion:
The provided Java code implements a program that finds and prints all unique pairs of
elements in an array that sum up to a specified target value. It begins by prompting the user for
the size of the array and ensures that the input is a positive integer. After reading the array
elements, the program asks for the target sum. It then uses a nested loop to check all possible
pairs of elements in the array, printing those that meet the target sum condition. This code
effectively demonstrates basic array manipulation and nested iteration in Java, showcasing how
to find pairs that satisfy a specific condition. However, the algorithm has a time complexity of
O(n^2), which may not be efficient for large arrays.

8|Page
Assignment – 5
Date: 26.05.2025
Problem Statement:
Write a Java program to print the row wise and column wise minimum
number in a matrix.

Source Code:
import java.util.*;
public class Matrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter the number of columns: ");
int cols = sc.nextInt();
int[][] matrix = new int[rows][cols];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = sc.nextInt();
}}
System.out.println("Matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
int[] row = new int[rows];
for (int i = 0; i < rows; i++) {
row[i] = matrix[i][0];
for (int j = 1; j < cols; j++) {
if (matrix[i][j] < row[i]) {
row[i] = matrix[i][j];
}}}
int[] col = new int[cols];
for (int j = 0; j < cols; j++) {
col[j] = matrix[0][j];
for (int i = 1; i < rows; i++) {
if (matrix[i][j] < col[j]) {
col[j] = matrix[i][j];
}}}

9|Page
System.out.println("Matrix with Row Min and Column Min:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.print("| Row " + (i+1) + " Min: " + row[i]);
System.out.println();
}
System.out.print("Column Min: ");
for (int j = 0; j < cols; j++) {
System.out.print(col[j] + "\t");
}
System.out.println();
}
}

Output:
Enter the number of rows: 3
Enter the number of columns: 3
Enter the elements of the matrix:
23 4 5
984
261
Matrix:
23 4 5
9 8 4
2 6 1
Matrix with Row Min and Column Min:
23 4 5 | Row 1 Min: 4
9 8 4 | Row 2 Min: 4
2 6 1 | Row 3 Min: 1
Column Min: 2 4 1
Discussion:
The provided Java code implements a program that allows users to input a matrix of integers
and then calculates the minimum values for each row and each column. It starts by prompting
the user for the dimensions of the matrix and its elements, using the Scanner class for input.
After reading the matrix, the program prints it in a formatted manner. It then computes the
minimum value for each row and each column, storing these values in separate arrays. Finally,
it displays the matrix again, appending the minimum value of each row and printing the
minimum values of each column below the matrix. This code effectively demonstrates basic
matrix manipulation and iteration techniques in Java, showcasing how to work with 2D arrays
and perform calculations based on their contents.

10 | P a g e
Assignment – 6
Date: 09.06.2025
Problem Statement:
Write a Java program to implement a calculator.

Source Code:
import java.util.*;
class Calculator{
int f,s;
void add(){
int res=f+s;
System.out.println("Summation is: "+res);
}
void sub(){
int res=f-s;
System.out.println("Substraction is: "+res);
}
void mul(){
int res=f*s;
System.out.println("Multiplication is: "+res);
}
void div(){
if(s==0){
System.out.println("Error");
}
else{
int res =f/s;
System.out.println("Division is: "+res);
}
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
Calculator cal=new Calculator();
System.out.println("Enter first value: ");
cal.f=sc.nextInt();
System.out.println("Enter second value: ");
cal.s=sc.nextInt();
while(true){
System.out.println("Enter the operator type(+.-.*./) or q to quit: ");
char op=sc.next().charAt(0);
switch(op){
case '+':

11 | P a g e
cal.add();
break;
case '-':
cal.sub();
break;
case '*':
cal.mul();
break;
case '/':
cal.div();
break;
case 'q':
System.exit(0);
default:
System.out.println("Error");
break;
}
}
}
}

Output:
Enter first value:
90
Enter second value:
55
Enter the operator type(+.-.*./) or q to quit:
+
Summation is: 145
Enter the operator type(+.-.*./) or q to quit:
-
Substraction is: 35
Enter the operator type(+.-.*./) or q to quit:
*
Multiplication is: 4950
Enter the operator type(+.-.*./) or q to quit:
/
Division is: 1
Enter the operator type(+.-.*./) or q to quit:
q

12 | P a g e
Discussion:
This Java code implements a basic calculator that performs addition, subtraction,
multiplication, and division operations. It utilizes a Calculator class with methods for each
operation and a main method that takes user input for numbers and operators. The program
runs in a loop, allowing users to perform multiple calculations until they choose to quit by
entering 'q'. Error handling is included for division by zero and invalid operators, ensuring a
robust user experience. Overall, the code demonstrates fundamental concepts of object-
oriented programming and user interaction in Java.

13 | P a g e
Assignment – 7
Date: 09.06.2025
Problem Statement:
Write a Java program to implement addition, subtraction, multiplication
for complex numbers.

Source Code:
import java.util.*;
class Complex {
double real;
double imag;
Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
void add(Complex other) {
double resReal = this.real + other.real;
double resImag = this.imag + other.imag;
System.out.println("Sum: " + resReal + " + " + resImag + "i");
}
void sub(Complex other) {
double resReal = this.real - other.real;
double resImag = this.imag - other.imag;
System.out.println("Difference: " + resReal + " + " + resImag + "i");
}
void mul(Complex other) {
double resReal = this.real * other.real - this.imag * other.imag;
double resImag = this.real * other.imag + this.imag * other.real;
System.out.println("Product: " + resReal + " + " + resImag + "i");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first complex number (real imaginary): ");
Complex c1 = new Complex(sc.nextDouble(), sc.nextDouble());
System.out.println("Enter second complex number (real imaginary): ");
Complex c2 = new Complex(sc.nextDouble(), sc.nextDouble());
while(true) {
System.out.println("Enter operator (+, -, *) or q to quit: ");
char op = sc.next().charAt(0);
switch(op) {
case '+':
c1.add(c2);

14 | P a g e
break;
case '-':
c1.sub(c2);
break;
case '*':
c1.mul(c2);
break;
case 'q':
System.exit(0);
default:
System.out.println("Invalid operator.");
}
}
}
}

Output:
Enter first complex number (real imaginary):
8 5
Enter second complex number (real imaginary):
7 3
Enter operator (+, -, *) or q to quit:
+
Sum: 15.0 + 8.0i
Enter operator (+, -, *) or q to quit:
-
Difference: 1.0 + 2.0i
Enter operator (+, -, *) or q to quit:
*
Product: 41.0 + 59.0i
Enter operator (+, -, *) or q to quit:
q
Discussion:
This Java code implements a complex number calculator, allowing users to perform addition,
subtraction, and multiplication operations on complex numbers through a Complex class. It's
well-structured and demonstrates object-oriented programming in Java.

15 | P a g e

You might also like