0% found this document useful (0 votes)
6 views6 pages

Java Aat 7

Uploaded by

faadilkhaleel360
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)
6 views6 pages

Java Aat 7

Uploaded by

faadilkhaleel360
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/ 6

B.M.S.

COLLEGE OF ENGINEERING
(Autonomous college under VTU)
Bull Temple Rd, Basavanagudi, Bengaluru, Karnataka 560019
2023-2025
Department of Computer Applications

JAVA Assignment
(22MCA2PEJP)
By

Faadil

Under the Guidance

Raghavendra Rao
(Assistant Professor)
Department of Computer Applications, BMSCE

1. Write a Java program to accept one parameter on the command line. If


there are no command line arguments entered, the program should print the
error message and exit. The program should check whether the input is a
directory. And also
display the names of fi les in the directory.
Code:
import java.io.File;
public class DirectoryLister {
public static void main(String[] args) {
// Check if exactly one command-line argument is provided if (args.length !=
1) {
System.out.println("Error: Please provide exactly one argument.");
System.exit(1); }
// Get the directory path from the command-line argument String
directoryPath = args[0];
File directory = new File(directoryPath);
// Check if the provided path is a directory if (!directory.isDirectory()) {
System.out.println("Error: The provided path is not a directory.");
System.exit(1); }
// List the files in the directory
File[] files = directory.listFiles();
if (files != null && files.length > 0) {
System.out.println("Files in directory " + directoryPath + ":");
for (File file : files) { if (file.isFile()) {
System.out.println(file.getName()); }
}
} else {
System.out.println("The directory is empty or cannot be accessed."); }

2
Department of Computer Applications, BMSCE

}}
OUTPUT:
java DirectoryLister
Error: Please provide exactly one argument.
java DirectoryLister c:/files /extra/arg Error: Please provide exactly one
argument.
Error: The provided path is not a directory.
2. Develop a program using collection framework to create a group of
productid. Check a given productid is available in the defined group or not.
Note: All input need to be received as user input only
Code:
import java.util.HashSet; import java.util.Scanner;
public class ProductIDChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); HashSet<String> productIDs =
new HashSet<>();
// Input the number of product IDs to be added System.out.print("Enter the
number of product IDs: "); int numOfProductIDs = scanner.nextInt();
scanner.nextLine(); // Consume newline
// Input product IDs System.out.println("Enter the product IDs:"); for (int i =
0; i < numOfProductIDs; i++) {
System.out.print("Product ID " + (i + 1) + ": "); String productID =
scanner.nextLine(); productIDs.add(productID);
}
// Input the product ID to be checked System.out.print("Enter the product ID
to check: "); String checkProductID = scanner.nextLine();
// Check if the product ID is in the group
if (productIDs.contains(checkProductID)) {

3
Department of Computer Applications, BMSCE

System.out.println("Product ID " + checkProductID + " is available in the


group."); } else {
System.out.println("Product ID " + checkProductID + " is not available in
the group.");
}
scanner.close(); }
}
output:
javac ProductIDChecker.java
java ProductIDChecker
Enter the number of product IDs: 3 Enter the product IDs:
Product ID 1: P001
Product ID 2: P002
Product ID 3: P003
Enter the product ID to check: P002 Product ID P002 is available in the
group.
3. Develop a linked list with user input, access the all available items from
the given value. Example output:
Assuming linked list is (4,7,1,8,90,78,6,12,76), given input is 8, its suppose
to print output : 90,78,6,12,76
Code:
import java.util.Scanner;
// Node class for linked list class Node {
int data; Node next;
Node(int data) {
this.data = data;
this.next = null; }
}
4
Department of Computer Applications, BMSCE

// LinkedList class class LinkedList {


Node head;
// Add a new node to the end of the list void add(int data) {
if (head == null) {
head = new Node(data);
} else {
Node current = head;
while (current.next != null) {
current = current.next; }
current.next = new Node(data); }
}
// Print all nodes following a given value void printFollowing(int value) {
Node current = head; boolean found = false; while (current != null) {
if (found) { System.out.print(current.data + " ");
}
if (current.data == value) { found = true;
}
current = current.next; }
if (!found) {
System.out.println("Value not found in the list.");
}
System.out.println(); }
}
public class LinkedListExample {
public static void main(String[] args) {

5
Department of Computer Applications, BMSCE

Scanner scanner = new Scanner(System.in); LinkedList list = new


LinkedList();
// Input the number of elements
System.out.print("Enter the number of elements in the linked list: "); int n =
scanner.nextInt();
// Input elements System.out.println("Enter the elements:"); for (int i = 0; i <
n; i++) {
int value = scanner.nextInt();
list.add(value); }
// Input the value to search for System.out.print("Enter the value to find: ");
int searchValue = scanner.nextInt();
// Print all items following the given value System.out.print("Items following
" + searchValue + ": "); list.printFollowing(searchValue);
scanner.close(); }
}
output:
javac LinkedListExample.java java LinkedListExample
Enter the number of elements in the linked list: 9 Enter the elements:
4 7 1 8 90 78 6 12 76
Enter the value to find: 8
Items following 8: 90 78 6 12 76

You might also like