1. WAP to calculate the Simple Interest (Input by User).
import [Link];
public class SimpleInterestCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Principal amount: ");
double principal = [Link]();
[Link]("Enter Annual Interest Rate (in %): ");
double rate = [Link]();
[Link]("Enter Time (in years): ");
double time = [Link]();
double simpleInterest = (principal * rate * time) / 100;
[Link]("Simple Interest: " + simpleInterest);
}
}
Output :-
Page | 1
2. WAP to Test if a Number is Prime.
public class PrimeChecker {
public static void main(String[] args) {
int num = 29; // Example number to test
boolean isPrime = true;
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (num > 1 && isPrime) {
[Link](num + " is a Prime number.");
} else {
[Link](num + " is NOT a Prime number.");
}
}
}
Output :-
Page | 2
3. WAP to find the factorial using Recursion.
public class FactorialRecursion {
public static int findFactorial(int n) {
if (n <= 1) return 1;
return n * findFactorial(n - 1);
}
public static void main(String[] args) {
int num = 5; // Example number
[Link]("Factorial of " + num + " is: " findFactorial
(num));
}
}
Output :-
Page | 3
4. WAP to find the Average and Sum of N numbers using Command Line
Arguments.
public class SumAverageCLI {
public static void main(String[] args) {
if ([Link] == 0) {
[Link]("Please provide numbers as command-
line arguments.");
return;
}
int sum = 0;
for (String arg : args) {
sum += [Link](arg);
}
double average = (double) sum / [Link];
[Link]("Sum: " + sum);
[Link]("Average: " + average);
}
}
Output :-
Page | 4
5. WAP to Demonstrate Type Casting.
public class TypeCastingDemo {
public static void main(String[] args) {
int num = 10;
double convertedNum = num; // Implicit casting
[Link]("Implicit Casting (int to double): " +
convertedNum);
double decimal = 9.75;
int wholeNumber = (int) decimal; // Explicit casting
[Link]("Explicit Casting (double to int): " +
wholeNumber);
}
}
Output :-
Page | 5
6. WAP to find the number of arguments provided at runtime.
public class CountArguments {
public static void main(String[] args) {
// Simulating command-line arguments
args = new String[]{"10", "20", "Hello", "World"};
int numberOfArguments = [Link];
[Link]("Number of arguments provided: " +
numberOfArguments);
}
}
Output :-
Page | 6
7. WAP to handle Exception using try and multiple catch blocks.
public class MultipleCatchExample {
public static void main(String[] args) {
try {
// Simulate two potential exceptions
int[] numbers = {10, 20, 30};
int divisor = 0;
// This may throw an ArithmeticException
int result = numbers[1] / divisor;
// This may throw an ArrayIndexOutOfBoundsException
[Link]("Accessing element: " + numbers[5]);
} catch (ArithmeticException e) {
[Link]("ArithmeticException caught: Division
by zero is not allowed.");
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("ArrayIndexOutOfBoundsException
caught: Invalid index access.");
} catch (Exception e) {
[Link]("General Exception caught: " +
[Link]());
} finally {
[Link]("Execution completed.");
}
}
}
Output :-
Page | 7
8. WAP to create a Dialog Box.
import [Link].*;
public class DialogBoxExample {
public static void main(String[] args) {
[Link](null, "This is a simple dialog
box!");
}
}
Output :-
Page | 8
9. WAP to find the GCD of two numbers.
public class GCDCalculator {
public static void main(String[] args) {
int a = 36, b = 60;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
[Link]("GCD: " + a);
}
}
Output :-
Page | 9
10. WAP to Demonstrate the System Clock.
import [Link];
import [Link];
public class SystemClockDemo {
public static void main(String[] args) {
// Get the current system date and time
LocalDateTime currentDateTime = [Link]();
// Format the date and time for better readability
DateTimeFormatter formatter =
[Link]("dd-MM-yyyy HH:mm:ss");
// Display the current date and time
[Link]("Current System Date and Time: " +
[Link](formatter));
// Show the current time in milliseconds since epoch
long currentTimeMillis = [Link]();
[Link]("Current Time in Milliseconds Since
Epoch: " + currentTimeMillis);
// Example of performing a time operation
[Link]("Performing a task...");
try {
[Link](2000); // Simulate a task that takes 2
seconds
} catch (InterruptedException e) {
[Link]("Task interrupted!");
}
[Link]("Task completed.");
}
}
Output :-
Page | 10
11. WAP to create a Thread using the Runnable Interface.
class MyRunnable implements Runnable {
@Override
public void run() {
// Code executed by the thread
[Link]("Thread is running: " +
[Link]().getName());
for (int i = 1; i <= 5; i++) {
[Link]([Link]().getName() + "
- Count: " + i);
try {
[Link](500); // Pause for 500ms
} catch (InterruptedException e) {
[Link]("Thread interrupted!");
}
}
}
}
public class RunnableExample {
public static void main(String[] args) {
// Create an instance of the MyRunnable class
MyRunnable myRunnable = new MyRunnable();
// Create a thread using the Runnable object
Thread thread1 = new Thread(myRunnable, "Worker-1");
Thread thread2 = new Thread(myRunnable, "Worker-2");
// Start the threads
[Link]();
[Link]();
[Link]("Threads have been started!");
}
}
Output :-
Page | 11
12. WAP to Reverse a String.
public class StringReverser {
public static void main(String[] args) {
String str = "HelloWorld";
String reversed = new
StringBuilder(str).reverse().toString();
[Link]("Original: " + str);
[Link]("Reversed: " + reversed);
}
}
Output :-
Page | 12
13. WAP to handle User-defined Exception using throw.
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
public class UserDefinedExceptionExample {
// Method to validate age
public static void validateAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age is less than 18. Not eligible
to vote.");
} else {
[Link]("Age is valid. You are eligible to vote.");
}
}
public static void main(String[] args) {
try {
// Test with an invalid age
validateAge(15);
// Test with a valid age
validateAge(20);
} catch (InvalidAgeException e) {
// Handle the custom exception
[Link]("Exception caught: " + [Link]());
}
[Link]("Program execution continues...");
}
}
Output :-
Page | 13
14. WAP to Draw a Line, Rectangle, and Oval using Graphics.
import [Link].*;
import [Link].*;
public class ShapeDrawer extends JPanel {
public void paint(Graphics g) {
[Link](50, 50, 200, 50);
[Link](50, 100, 100, 50);
[Link](50, 200, 100, 50);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
[Link](new ShapeDrawer());
[Link](400, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
Output :-
Page | 14
15. WAP to Implement FlowLayout.
import [Link].*;
import [Link].*;
public class FlowLayoutDemo {
public static void main(String[] args) {
jFrame frame = new JFrame("FlowLayout Example");
[Link](new FlowLayout());
[Link](new JButton("Button 1"));
[Link](new JButton("Button 2"));
[Link](new JButton("Button 3"));
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
Output :-
Page | 15