java
java
import java.util.Scanner;
public class ExpressionEvaluator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in)
// Arithmetic Expression Evaluation
System.out.print("Enter two numbers to perform arithmetic operations: ");
double num1 = scanner.nextDouble();
double num2 = scanner.nextDouble();
System.out.println("Arithmetic Operations:");
System.out.println("Addition: " + (num1 + num2));
System.out.println("Subtraction: " + (num1 - num2));
System.out.println("Multiplication: " + (num1 * num2));
System.out.println("Division: " + (num1 / num2));
// Relational Expression Evaluation
System.out.print("\nEnter two integers for relational comparison: ");
int x = scanner.nextInt();
int y = scanner.nextInt();
System.out.println("\nRelational Operations:");
System.out.println("x > y: " + (x > y));
System.out.println("x < y: " + (x < y));
System.out.println("x == y: " + (x == y));
System.out.println("x != y: " + (x != y));
// Logical Expression Evaluation
System.out.print("\nEnter two boolean values (true/false): ");
boolean a = scanner.nextBoolean();
boolean b = scanner.nextBoolean();
System.out.println("\nLogical Operations:");
System.out.println("a AND b: " + (a && b));
System.out.println("a OR b: " + (a || b));
System.out.println("NOT a: " + !a);
// Conditional (Ternary) Expression Evaluation
System.out.print("\nEnter a number to check if it's positive or negative: ");
int num = scanner.nextInt();
String result = (num >= 0) ? "Positive" : "Negative";
System.out.println("The number is " + result);
// Bitwise Expression Evaluation
System.out.print("\nEnter two integers for bitwise operation: ");
int numA = scanner.nextInt();
int numB = scanner.nextInt();
System.out.println("\nBitwise Operations:");
System.out.println("numA & numB: " + (numA & numB)); // Bitwise
AND
System.out.println("numA | numB: " + (numA | numB)); // Bitwise OR
System.out.println("numA ^ numB: " + (numA ^ numB)); // Bitwise XOR
System.out.println("~numA: " + (~numA)); // Bitwise NOT
scanner.close();
}
}
Output :-
Enter two numbers to perform arithmetic operations: 53
Enter two integers for relational comparison: 105
Enter two boolean values (true/false): true false
Enter a number to check if it's positive or negative: -7
Enter two integers for bitwise operation: 53
program :-
import java.util.Scanner;
// Using if statement
int x = 10;
if (x > 0) {
System.out.println("x is positive");
} else if (x < 0) {
System.out.println("x is negative");
} else {
System.out.println("x is zero");
}
// 1. For Loop
System.out.println("\nUsing a for loop to print numbers from 1 to 5:");
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
// 2. While Loop
System.out.println("\nUsing a while loop to print numbers from 6 to 10:");
int j = 6;
while (j <= 10) {
System.out.println(j);
j++;
}
// 3. Do-While Loop
System.out.println("\nUsing a do-while loop to print numbers from 11 to
15:");
int k = 11;
do {
System.out.println(k);
k++;
} while (k <= 15);
// 1. length()
System.out.println("Length of str1: " + str1.length()); // Output: 5
// 2. charAt()
System.out.println("Character at index 1 of str1: " + str1.charAt(1)); //
Output: e
// 3. concat()
String str3 = str1.concat(str2);
System.out.println("Concatenated String: " + str3); // Output: HelloWorld
// 4. equals()
boolean isEqual = str1.equals(str2);
System.out.println("str1 equals str2: " + isEqual); // Output: false
// 5. toLowerCase()
System.out.println("str1 in lowercase: " + str1.toLowerCase()); // Output:
hello
// 6. toUpperCase()
System.out.println("str2 in uppercase: " + str2.toUpperCase()); // Output:
WORLD
// 7. substring()
System.out.println("Substring of str3 (from index 5): " + str3.substring(5));
// Output: World
// 8. indexOf()
System.out.println("Index of 'o' in str3: " + str3.indexOf('o')); // Output: 4
// 9. replace()
String str4 = str3.replace('o', '0');
System.out.println("Replaced string: " + str4); // Output: Hell0W0rld
// 10. trim()
String str5 = " Hello World ";
System.out.println("Trimmed str5: '" + str5.trim() + "'"); // Output: 'Hello
World'
// 1. append()
sb.append(" World");
System.out.println("StringBuilder after append: " + sb); // Output: Hello
World
// 2. insert()
sb.insert(5, ",");
System.out.println("StringBuilder after insert: " + sb); // Output: Hello,
World
// 3. reverse()
sb.reverse();
System.out.println("Reversed StringBuilder: " + sb); // Output: dlroW
,olleH
// 4. delete()
sb.delete(0, 6);
System.out.println("StringBuilder after delete: " + sb); // Output: ,olleH
// 5. replace()
sb.replace(1, 3, "oo");
System.out.println("StringBuilder after replace: " + sb); // Output: ,oolleH
// 6. capacity()
System.out.println("StringBuilder capacity: " + sb.capacity()); // Output:
16 (default capacity)
// 7. setLength()
sb.setLength(5);
System.out.println("StringBuilder after setLength(5): " + sb); // Output:
,ool
// 8. toString()
String sbString = sb.toString();
System.out.println("StringBuilder to String: " + sbString); // Output: ,ool
}
}
Output :-
Length of str1: 5
Character at index 1 of str1: e
Concatenated String: HelloWorld
str1 equals str2: false
str1 in lowercase: hello
str2 in uppercase: WORLD
Substring of str3 (from index 5): World
Index of 'o' in str3: 4
Replaced string: Hell0W0rld
Trimmed str5: 'Hello World'
StringBuilder after append: Hello World
StringBuilder after insert: Hello, World
Reversed StringBuilder: dlroW ,olleH
StringBuilder after delete: ,olleH
StringBuilder after replace: ,oolleH
StringBuilder capacity: 16
StringBuilder after setLength(5): ,ool
StringBuilder to String: ,ool
Program :-
import java.util.*;
// 1. Array demonstration
System.out.println("Array demonstration:");
// 2. Vector demonstration
System.out.println("\nVector demonstration:");
Vector demonstration:
Vector elements:
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Modified Vector:
Element at index 0: 100
Element at index 1: 200
Element at index 2: 350
Element at index 3:
Program :-
class ConstructorDemo {
int num;
String name;
// 1. Default Constructor
// This constructor does not take any parameters
public ConstructorDemo() {
num = 0; // Initialize num to 0
name = "Default"; // Initialize name to "Default"
System.out.println("Default Constructor Called");
}
// 2. Parameterized Constructor
// This constructor takes two parameters: an int and a String
public ConstructorDemo(int n, String str) {
num = n; // Initialize num with the provided value
name = str; // Initialize name with the provided value
System.out.println("Parameterized Constructor Called");
}
// 3. Constructor Overloading
// Overloaded constructor with different parameters
public ConstructorDemo(String str) {
num = 100; // Default value for num
name = str; // Initialize name with the provided value
System.out.println("Constructor with String parameter Called");
}
// Multilevel Inheritance: Mammal class inherits from Animal, and Dog inherits
from Mammal
class Mammal extends Animal {
System.out.println();