import [Link].
Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
[Link]();
if (isPalindrome(input)) {
[Link](input + " is a palindrome.");
} else {
[Link](input + " is not a palindrome.");
}
}
public static boolean isPalindrome(String input) {
input = [Link]("\\s", "").toLowerCase();
int left = 0;
int right = [Link]() - 1;
while (left < right) {
if ([Link](left) != [Link](right)) {
return false;
}
left++;
right--;
}
return true;
}
}
C:\Users\Kashish\OneDrive\Desktop>javac [Link]
C:\Users\Kashish\OneDrive\Desktop>java PalindromeChecker
Enter a string: malyalam
malyalam is not a palindrome.
C:\Users\Kashish\OneDrive\Desktop>malayalam
'malayalam' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Kashish\OneDrive\Desktop>javac [Link]
C:\Users\Kashish\OneDrive\Desktop>java PalindromeChecker
Enter a string: radar
radar is a palindrome.
8b. import [Link];
public class CharacterCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
[Link]();
int uppercaseCount = 0;
int lowercaseCount = 0;
int specialCharacterCount = 0;
int digitCount = 0;
int spaceCount = 0;
for (char c : [Link]()) {
if ([Link](c)) {
uppercaseCount++;
} else if ([Link](c)) {
lowercaseCount++;
} else if ([Link](c)) {
digitCount++;
} else if ([Link](c)) {
spaceCount++;
} else {
specialCharacterCount++;
}
}
[Link]("Uppercase letters: " + uppercaseCount);
[Link]("Lowercase letters: " + lowercaseCount);
[Link]("Special characters: " + specialCharacterCount);
[Link]("Digits: " + digitCount);
[Link]("Blank spaces: " + spaceCount);
}
}
Microsoft Windows [Version 10.0.22621.2506]
(c) Microsoft Corporation. All rights reserved.
C:\Users\Kashish\OneDrive\Desktop>javac [Link]
C:\Users\Kashish\OneDrive\Desktop>java CharacterCounter
Enter a string: My name is Kashish Jitendra jain
Uppercase letters: 3
Lowercase letters: 24
Special characters: 0
Digits: 0
Blank spaces: 5
9a
import [Link];
class Shape {
public double calculateArea() {
return 0; // Base class method for calculating area
}
}
class Square extends Shape {
private double side;
public Square(double side) {
[Link] = side;
}
@Override
public double calculateArea() {
return side * side;
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
}
@Override
public double calculateArea() {
return length * width;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
[Link] = radius;
}
@Override
public double calculateArea() {
return [Link] * radius * radius;
}
}
public class AreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Choose a shape to calculate its area:");
[Link]("1. Square");
[Link]("2. Rectangle");
[Link]("3. Circle");
[Link]("Enter your choice (1/2/3): ");
int choice = [Link]();
Shape shape = null;
switch (choice) {
case 1:
[Link]("Enter the side length of the square: ");
double squareSide = [Link]();
shape = new Square(squareSide);
break;
case 2:
[Link]("Enter the length of the rectangle: ");
double rectLength = [Link]();
[Link]("Enter the width of the rectangle: ");
double rectWidth = [Link]();
shape = new Rectangle(rectLength, rectWidth);
C:\Users\Kashish\OneDrive\Desktop>javac [Link]
C:\Users\Kashish\OneDrive\Desktop>java AreaCalculator
Choose a shape to calculate its area:
1. Square
2. Rectangle
3. Circle
Enter your choice (1/2/3): 1
Enter the side length of the square: 50
Area of the selected shape: 2500.0
C:\Users\Kashish\OneDrive\Desktop>
9b
import [Link];
class Shape {
public double calculateArea() {
return 0; // Base class method for calculating area
}
}
class Square extends Shape {
private double side;
public Square(double side) {
[Link] = side;
}
@Override
public double calculateArea() {
return side * side;
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
}
@Override
public double calculateArea() {
return length * width;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
[Link] = radius;
}
@Override
public double calculateArea() {
return [Link] * radius * radius;
}
}
public class AreaCalculators {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Choose a shape to calculate its area:");
[Link]("1. Square");
[Link]("2. Rectangle");
[Link]("3. Circle");
[Link]("Enter your choice (1/2/3): ");
int choice = [Link]();
Shape shape = null;
switch (choice) {
case 1:
[Link]("Enter the side length of the square: ");
double squareSide = [Link]();
shape = new Square(squareSide);
break;
case 2:
[Link]("Enter the length of the rectangle: ");
double rectLength = [Link]();
[Link]("Enter the width of the rectangle: ");
double rectWidth = [Link]();
shape = new Rectangle(rectLength, rectWidth);
break;
case 3:
[Link]("Enter the radius of the circle: ");
double circleRadius = [Link]();
shape = new Circle(circleRadius);
break;
default:
[Link]("Invalid choice.");
}
if (shape != null) {
[Link]("Area of the selected shape: " +
[Link]());
}
[Link]();
}
}
C:\Users\Kashish\OneDrive\Desktop>javac [Link]
C:\Users\Kashish\OneDrive\Desktop>java AreaCalculators
Choose a shape to calculate its area:
1. Square
2. Rectangle
3. Circle
Enter your choice (1/2/3): 2
Enter the length of the rectangle: 15
Enter the width of the rectangle: 20
Area of the selected shape: 300.0
C:\Users\Kashish\OneDrive\Desktop>3
'3' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Kashish\OneDrive\Desktop>javac [Link]
C:\Users\Kashish\OneDrive\Desktop>java AreaCalculators
Choose a shape to calculate its area:
1. Square
2. Rectangle
3. Circle
Enter your choice (1/2/3): 3
Enter the radius of the circle: 50
Area of the selected shape: 7853.981633974483
C:\Users\Kashish\OneDrive\Desktop>
10.
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
// Attempt to perform some operations that may throw exceptions
int result = divideByZero(); // Division by zero
String nullString = null; // Null pointer
int[] array = new int[5];
int value = array[10]; // ArrayIndexOutOfBoundsException
} catch (ArithmeticException e) {
// Handle the ArithmeticException
[Link]("ArithmeticException: " + [Link]());
} catch (NullPointerException e) {
// Handle the NullPointerException
[Link]("NullPointerException: " + [Link]());
} catch (ArrayIndexOutOfBoundsException e) {
// Handle the ArrayIndexOutOfBoundsException
[Link]("ArrayIndexOutOfBoundsException: " +
[Link]());
}
}
public static int divideByZero() {
int numerator = 10;
int denominator = 0;
return numerator / denominator;
}
}
Microsoft Windows [Version 10.0.22621.2506]
(c) Microsoft Corporation. All rights reserved.
C:\Users\Kashish\OneDrive\Desktop>javac [Link]
C:\Users\Kashish\OneDrive\Desktop>java ExceptionHandlingExample
ArithmeticException: / by zero
C:\Users\Kashish\OneDrive\Desktop>
12a
public class ThreadName {
public static void main(String[] args) {
// Create the first thread
Thread thread1 = new Thread(new MyRunnable(), "Thread-1");
// Create the second thread
Thread thread2 = new Thread(new MyRunnable(), "Thread-2");
// Start both threads
[Link]();
[Link]();
// Fetch and print the names of the threads
String thread1Name = [Link]();
String thread2Name = [Link]();
[Link]("Thread 1 name: " + thread1Name);
[Link]("Thread 2 name: " + thread2Name);
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
[Link]("Thread running: " + [Link]().getName());
}
}
C:\Users\Kashish\OneDrive\Desktop>java ThreadName
Thread 1 name: Thread-1
Thread 2 name: Thread-2
Thread running: Thread-2
Thread running: Thread-1
12b
public class MyThread {
public static void main(String[] args) {
Runnable r = new Runnable1();
Thread t = new Thread(r);
[Link]();
Runnable r2 = new Runnable2();
Thread t2 = new Thread(r2);
[Link]();
}
}
class Runnable2 implements Runnable {
public void run() {
for (int i = 0; i < 11; i++) {
if (i % 2 == 1)
[Link](i);
}
}
}
class Runnable1 implements Runnable {
public void run() {
for (int i = 0; i < 11; i++) {
if (i % 2 == 0)
[Link](i);
}
}
}
C:\Users\Kashish\OneDrive\Desktop>javac MyThread
error: Class names, 'MyThread', are only accepted if annotation processing is
explicitly requested
1 error
C:\Users\Kashish\OneDrive\Desktop>java MyThread
0
2
4
6
8
10
1
3
5
7
9
15.
import [Link].*;
import [Link].*;
public class RegistrationFormAWT {
public static void main(String[] args) {
Frame frame = new Frame("Registration Form");
[Link](400, 250);
[Link](new GridLayout(6, 2));
Label firstNameLabel = new Label("First Name:");
TextField firstNameField = new TextField(20);
Label lastNameLabel = new Label("Last Name:");
TextField lastNameField = new TextField(20);
Label emailLabel = new Label("Email:");
TextField emailField = new TextField(20);
Label passwordLabel = new Label("Password:");
TextField passwordField = new TextField(20);
[Link]('*'); // Set echo character to hide the password
Button submitButton = new Button("Submit");
Button cancelButton = new Button("Cancel");
// Action listener for the Submit button
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
String firstName = [Link]();
String lastName = [Link]();
String email = [Link]();
String password = [Link]();
// Perform registration logic here
// For now, we'll just display the entered data
showMessage("Registration Successful\n" +
"First Name: " + firstName + "\n" +
"Last Name: " + lastName + "\n" +
"Email: " + email);
}
});
// Action listener for the Cancel button
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
int choice = showConfirmation("Are you sure you want to cancel
registration?");
if (choice == 0) {
[Link](0);
}
}
});
[Link](firstNameLabel);
[Link](firstNameField);
[Link](lastNameLabel);
[Link](lastNameField);
[Link](emailLabel);
[Link](emailField);
[Link](passwordLabel);
[Link](passwordField);
[Link](submitButton);
[Link](cancelButton);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link](0);
}
});
[Link](true);
}
// Helper method to display a message dialog
private static void showMessage(String message) {
Dialog messageDialog = new Dialog(new Frame(), "Message", true);
Label label = new Label(message);
[Link](label);
[Link](300, 100);
[Link](true);
}
// Helper method to display a confirmation dialog
private static int showConfirmation(String message) {
Dialog confirmDialog = new Dialog(new Frame(), "Confirmation", true);
Label label = new Label(message);
[Link](label);
Button yesButton = new Button("Yes");
Button noButton = new Button("No");
[Link](yesButton);
[Link](noButton);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]();
}
});
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]();
}
});
[Link](new GridLayout(2, 2));
[Link](300, 100);
[Link](true);
return 0; // Return 0 for 'Yes' (in this example)
}
}