Practical Programs
Practical Programs
Assignment 1:
Practice Set:
Set B:
Practice Set:
Set A:
Set C:
Practice Set:
Set A:
Set B:
Set C:
1. Create a hashtable containing city name & STD code. Display
the details of the hashtable. Also search for a specific city
and display the STD code of that city.
→ import java.util.Hashtable;
public class Main {
public static void main(String[] args) {
Hashtable<String, Integer> hashtable = new
Hashtable<>();
hashtable.put("New York", 212);
hashtable.put("London", 20);
hashtable.put("Tokyo", 81);
System.out.println("Hashtable details:");
for (String city : hashtable.keySet()) {
System.out.println("City: " + city + ", STD Code: " +
hashtable.get(city));
}
Practice Set:
Set B:
1. Write a java program to copy the data front: one file into
another file, while copying change the case of characters in
target file and replaces all digits by “*" symbol.
→ import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyFileChangeCase {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("source.txt");
FileWriter writer = new FileWriter("target.txt");
int character;
while ((character = reader.read()) != -1) {
if (Character.isUpperCase(character)) {
writer.write(Character.toLowerCase(character));
} else if (Character.isLowerCase(character)) {
writer.write(Character.toUpperCase(character));
} else if (Character.isDigit(character)) {
writer.write('*');
} else {
writer.write(character);
}
}
reader.close();
writer.close();
System.out.println("File copied successfully with case
change and digit replacement.");
} catch (IOException e) {
System.out.println("Error copying file: " +
e.getMessage());
}
}
}
2. Write a java program to accept strings from a user. Write
ASCII values of the characters from a string into the file.
→ import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class WriteASCIIValuesToFile {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
FileWriter writer = new FileWriter("output.txt");
for (int i = 0; i < input.length(); i++) {
int ascii = (int) input.charAt(i);
writer.write(ascii + " ");
}
writer.close();
scanner.close();
System.out.println("ASCII values written to file
successfully.");
} catch (IOException e) {
System.out.println("Error writing to file: " +
e.getMessage());
}
} }
3. Write a java program to accept a number from a user, if it
is less than 5 then throw user defined Exception "Number is
small", if it is greater than 10 then throw user defined
exception "Number is Greater", otherwise calculate its
factorial.
→ import java.util.Scanner;
class SmallNumberException extends Exception {
SmallNumberException(String message) {
super(message);
}
}
class GreaterNumberException extends Exception {
GreaterNumberException(String message) {
super(message);
}
}
public class NumberFactorial {
static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
try {
if (number < 5) {
throw new SmallNumberException("Number is
small");
} else if (number > 10) {
throw new GreaterNumberException("Number is
greater");
} else {
int fact = factorial(number);
System.out.println("Factorial of " + number + " is: "
+ fact);
}
} catch (SmallNumberException |
GreaterNumberException e) {
System.out.println("Exception: " + e.getMessage());
} finally {
scanner.close();
}
}
}
4. Write a java program to display contents of a file in
reverse order.
→ import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;
public class ReverseFileContent {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("input.txt");
Stack<Character> stack = new Stack<>();
int character;
while ((character = reader.read()) != -1) {
stack.push((char) character);
}
while (!stack.isEmpty()) {
System.out.print(stack.pop());
}
reader.close();
} catch (IOException e) {
System.out.println("Error reading from file: " +
e.getMessage());
}
}
}
5. Write a java program to display each word from a file in
reverse order.
→ import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;
import java.util.StringTokenizer;
public class ReverseWordsInFile {
public static void main(String[] args) {
try {
FileReader fileReader = new FileReader("input.txt");
BufferedReader bufferedReader = new
BufferedReader(fileReader);
String line;
Stack<String> wordsStack = new Stack<>();
while ((line = bufferedReader.readLine()) != null) {
StringTokenizer tokenizer = new
StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
wordsStack.push(tokenizer.nextToken());
}
}
while (!wordsStack.isEmpty()) {
System.out.print(wordsStack.pop() + " ");
}
fileReader.close();
bufferedReader.close();
} catch (IOException e) {
System.out.println("Error reading from file: " +
e.getMessage());
}
}
}
Set C: