java file
java file
Output :
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
12. Write a program to sum values of an Single Dimensional array.
public class ArraySum {
public static void main(String[] args) {
// Initialize an array
int[] numbers = {5, 10, 15, 20, 25};
int sum = 0;
// Loop through the array and add each element to the sum
for (int number : numbers) {
sum += number;
}
Output :
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
13. Design & execute a program in Java to sort a numeric array and a
string array.
import java.util.Arrays;
Output:
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
14. Calculate the average value of array elements through Java Program.
public class ArrayAverage {
public static void main(String[] args) {
// Initialize an array
int[] numbers = {10, 20, 30, 40, 50};
Output:
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
15. Write a Java program to test if an array contains a specific value.
import java.util.Scanner;
public class ArrayContainsValue {
public static void main(String[] args) {
// Initialize the array
int[] numbers = {3, 8, 15, 23, 42, 56};
// Get the value to search for from the user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a value to search for: ");
int valueToFind = scanner.nextInt();
// Flag to indicate if the value is found
boolean found = false;
// Check if the array contains the specified value
for (int number : numbers) {
if (number == valueToFind) {
found = true;
break;
}
}
// Display the result
if (found) {
System.out.println("The array contains the value " + valueToFind + ".");
} else {
System.out.println("The array does not contain the value " + valueToFind + ".");
}
}
}
Output:
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
16. Find the index of an array element by writing a program in Java.
import java.util.Scanner;
public class ArrayIndexFinder {
public static void main(String[] args) {
// Initialize the array
int[] numbers = {10, 20, 30, 40, 50, 60};
// Get the value to search for from the user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a value to find its index: ");
int valueToFind = scanner.nextInt();
// Variable to store the index of the element
int index = -1;
// Loop through the array to find the index of the specified value
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == valueToFind) {
index = i;
break;
}
}
// Output the result
if (index != -1) {
System.out.println("The index of the value " + valueToFind + " is: " + index);
} else {
System.out.println("The value " + valueToFind + " is not in the array.");
}
}
}
Output:
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
17. Write a Java program to remove a specific element from an array.
import java.util.Arrays;
import java.util.Scanner;
Output:
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
18. Design a program to copy an array by iterating the array.
import java.util.Arrays;
// Copy each element from the original array to the new array
for (int i = 0; i < originalArray.length; i++) {
copiedArray[i] = originalArray[i];
}
Output:
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
19. Write a Java program to insert an element (on a specific position) into
Multidimensional array.
import java.util.Scanner;
Output:
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
20. Write a program to perform following operations on strings:
1) Compare two strings.
2) Count string length.
3) Convert upper case to lower case & vice versa.
4) Concatenate two strings.
5) Print a substring.
import java.util.Scanner;
// 5) Print a substring
System.out.print("Enter the start index for the substring: ");
int startIndex = scanner.nextInt();
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
// Return the length of the array which represents the number of words
return words.length;
}
// Split the string by spaces and filter out empty strings caused by multiple spaces
String[] words = str.trim().split("\\s+");
// Return the length of the resulting array which represents the number of words
return words.length;
}
// Attempt to divide
int result = numerator / denominator;
System.out.println("The result of division is: " + result);
} catch (ArithmeticException e) {
// Handle divide by zero error
System.out.println("Error: Cannot divide by zero!");
}
try {
// Wait for both threads to finish execution before the main thread exits
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
thread1.join();
thread2.join();
} catch (InterruptedException e) {
System.out.println(e);
}
interface Runnable {
void run();
}
// Second interface
interface Showable {
void show();
}
Steps
1)Create a Package: Define a package in Java using the package keyword.
2)Create Classes in the Package: Add classes to this package
3)Access the Package from Another Class: Import and use the package in a main class.
package mypackage;
package mypackage;
import mypackage.Calculator;
import mypackage.Message;
Output:
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
31. To write and read a plain text file, write a Java program.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
if (filesList != null) {
System.out.println("Listing files and directories in: " + path);
for (File file : filesList) {
if (file.isDirectory()) {
System.out.println("[DIR] " + file.getName());
} else {
System.out.println("[FILE] " + file.getName());
}
}
} else {
System.out.println("The directory is empty.");
}
} else {
System.out.println("The specified path does not exist or is not a directory.");
}
}
}
Output:
Programming In Java Laboratory Subject code: UGCA 1938
Name: MD Akbar Ali Roll no.:3180/22
34. Develop a Java program to check if a file or directory specified by
pathname exists or not.
import java.io.File;
import java.util.Scanner;
if (file.exists()) {
if (file.isDirectory()) {
System.out.println("The path exists and is a directory.");
} else if (file.isFile()) {
System.out.println("The path exists and is a file.");
}
} else {
System.out.println("The specified path does not exist.");
}
}
}
Output:
Sample Run: