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

Javaaa

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)
26 views15 pages

Javaaa

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

1. WAP to calculate the Simple Interest (Input by User).

import java.util.Scanner;

public class SimpleInterestCalculator {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter Principal amount: ");


double principal = sc.nextDouble();

System.out.print("Enter Annual Interest Rate (in %): ");


double rate = sc.nextDouble();

System.out.print("Enter Time (in years): ");


double time = sc.nextDouble();

double simpleInterest = (principal * rate * time) / 100;


System.out.println("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 <= Math.sqrt(num); i++) {


if (num % i == 0) {
isPrime = false;
break;
}
}

if (num > 1 && isPrime) {


System.out.println(num + " is a Prime number.");
} else {
System.out.println(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
System.out.println("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 (args.length == 0) {
System.out.println("Please provide numbers as command-
line arguments.");
return;
}

int sum = 0;
for (String arg : args) {
sum += Integer.parseInt(arg);
}

double average = (double) sum / args.length;


System.out.println("Sum: " + sum);
System.out.println("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
System.out.println("Implicit Casting (int to double): " +
convertedNum);

double decimal = 9.75;


int wholeNumber = (int) decimal; // Explicit casting
System.out.println("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 = args.length;

System.out.println("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


System.out.println("Accessing element: " + numbers[5]);

} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught: Division
by zero is not allowed.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException
caught: Invalid index access.");
} catch (Exception e) {
System.out.println("General Exception caught: " +
e.getMessage());
} finally {
System.out.println("Execution completed.");
}
}
}

Output :-

Page | 7
8. WAP to create a Dialog Box.

import javax.swing.*;

public class DialogBoxExample {


public static void main(String[] args) {
JOptionPane.showMessageDialog(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;
}

System.out.println("GCD: " + a);


}
}

Output :-

Page | 9
10. WAP to Demonstrate the System Clock.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class SystemClockDemo {


public static void main(String[] args) {
// Get the current system date and time
LocalDateTime currentDateTime = LocalDateTime.now();

// Format the date and time for better readability


DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");

// Display the current date and time


System.out.println("Current System Date and Time: " +
currentDateTime.format(formatter));

// Show the current time in milliseconds since epoch


long currentTimeMillis = System.currentTimeMillis();
System.out.println("Current Time in Milliseconds Since
Epoch: " + currentTimeMillis);

// Example of performing a time operation


System.out.println("Performing a task...");
try {
Thread.sleep(2000); // Simulate a task that takes 2
seconds
} catch (InterruptedException e) {
System.out.println("Task interrupted!");
}
System.out.println("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
System.out.println("Thread is running: " +
Thread.currentThread().getName());
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + "
- Count: " + i);
try {
Thread.sleep(500); // Pause for 500ms
} catch (InterruptedException e) {
System.out.println("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


thread1.start();
thread2.start();

System.out.println("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();
System.out.println("Original: " + str);
System.out.println("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 {
System.out.println("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
System.out.println("Exception caught: " + e.getMessage());
}

System.out.println("Program execution continues...");


}
}

Output :-

Page | 13
14. WAP to Draw a Line, Rectangle, and Oval using Graphics.

import javax.swing.*;
import java.awt.*;

public class ShapeDrawer extends JPanel {


public void paint(Graphics g) {
g.drawLine(50, 50, 200, 50);
g.drawRect(50, 100, 100, 50);
g.drawOval(50, 200, 100, 50);
}

public static void main(String[] args) {


JFrame frame = new JFrame();
frame.add(new ShapeDrawer());
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output :-

Page | 14
15. WAP to Implement FlowLayout.

import javax.swing.*;
import java.awt.*;

public class FlowLayoutDemo {


public static void main(String[] args) {
jFrame frame = new JFrame("FlowLayout Example");
frame.setLayout(new FlowLayout());
frame.add(new JButton("Button 1"));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));

frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output :-

Page | 15

You might also like