java codes
java codes
Scanner;
if (isPalindrome(input)) {
System.out.println(input + " is a palindrome.");
} else {
System.out.println(input + " is not a palindrome.");
}
}
int left = 0;
int right = input.length() - 1;
return true;
}
}
C:\Users\Kashish\OneDrive\Desktop>javac PalindromeChecker.java
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 PalindromeChecker.java
C:\Users\Kashish\OneDrive\Desktop>java PalindromeChecker
Enter a string: radar
radar is a palindrome.
int uppercaseCount = 0;
int lowercaseCount = 0;
int specialCharacterCount = 0;
int digitCount = 0;
int spaceCount = 0;
C:\Users\Kashish\OneDrive\Desktop>javac CharacterCounter.java
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 java.util.Scanner;
class Shape {
public double calculateArea() {
return 0; // Base class method for calculating area
}
}
@Override
public double calculateArea() {
return side * side;
}
}
@Override
public double calculateArea() {
return length * width;
}
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
switch (choice) {
case 1:
System.out.print("Enter the side length of the square: ");
double squareSide = scanner.nextDouble();
shape = new Square(squareSide);
break;
case 2:
System.out.print("Enter the length of the rectangle: ");
double rectLength = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double rectWidth = scanner.nextDouble();
shape = new Rectangle(rectLength, rectWidth);
C:\Users\Kashish\OneDrive\Desktop>javac AreaCalculator.java
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 java.util.Scanner;
class Shape {
public double calculateArea() {
return 0; // Base class method for calculating area
}
}
@Override
public double calculateArea() {
return side * side;
}
}
@Override
public double calculateArea() {
return length * width;
}
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
switch (choice) {
case 1:
System.out.print("Enter the side length of the square: ");
double squareSide = scanner.nextDouble();
shape = new Square(squareSide);
break;
case 2:
System.out.print("Enter the length of the rectangle: ");
double rectLength = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double rectWidth = scanner.nextDouble();
shape = new Rectangle(rectLength, rectWidth);
break;
case 3:
System.out.print("Enter the radius of the circle: ");
double circleRadius = scanner.nextDouble();
shape = new Circle(circleRadius);
break;
default:
System.out.println("Invalid choice.");
}
if (shape != null) {
System.out.println("Area of the selected shape: " +
shape.calculateArea());
}
scanner.close();
}
}
C:\Users\Kashish\OneDrive\Desktop>javac AreaCalculators.java
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 AreaCalculators.java
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
System.out.println("ArithmeticException: " + e.getMessage());
} catch (NullPointerException e) {
// Handle the NullPointerException
System.out.println("NullPointerException: " + e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
// Handle the ArrayIndexOutOfBoundsException
System.out.println("ArrayIndexOutOfBoundsException: " +
e.getMessage());
}
}
C:\Users\Kashish\OneDrive\Desktop>javac ExceptionHandlingexample.java
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");
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 {
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 java.awt.*;
import java.awt.event.*;
frame.add(firstNameLabel);
frame.add(firstNameField);
frame.add(lastNameLabel);
frame.add(lastNameField);
frame.add(emailLabel);
frame.add(emailField);
frame.add(passwordLabel);
frame.add(passwordField);
frame.add(submitButton);
frame.add(cancelButton);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.setVisible(true);
}
yesButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
confirmDialog.dispose();
}
});
noButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
confirmDialog.dispose();
}
});