Program 2:-
import [Link];
public class ExpressionEvaluator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link])
// Arithmetic Expression Evaluation
[Link]("Enter two numbers to perform arithmetic operations: ");
double num1 = [Link]();
double num2 = [Link]();
[Link]("Arithmetic Operations:");
[Link]("Addition: " + (num1 + num2));
[Link]("Subtraction: " + (num1 - num2));
[Link]("Multiplication: " + (num1 * num2));
[Link]("Division: " + (num1 / num2));
// Relational Expression Evaluation
[Link]("\nEnter two integers for relational comparison: ");
int x = [Link]();
int y = [Link]();
[Link]("\nRelational Operations:");
[Link]("x > y: " + (x > y));
[Link]("x < y: " + (x < y));
[Link]("x == y: " + (x == y));
[Link]("x != y: " + (x != y));
// Logical Expression Evaluation
[Link]("\nEnter two boolean values (true/false): ");
boolean a = [Link]();
boolean b = [Link]();
[Link]("\nLogical Operations:");
[Link]("a AND b: " + (a && b));
[Link]("a OR b: " + (a || b));
[Link]("NOT a: " + !a);
// Conditional (Ternary) Expression Evaluation
[Link]("\nEnter a number to check if it's positive or negative: ");
int num = [Link]();
String result = (num >= 0) ? "Positive" : "Negative";
[Link]("The number is " + result);
// Bitwise Expression Evaluation
[Link]("\nEnter two integers for bitwise operation: ");
int numA = [Link]();
int numB = [Link]();
[Link]("\nBitwise Operations:");
[Link]("numA & numB: " + (numA & numB)); // Bitwise
AND
[Link]("numA | numB: " + (numA | numB)); // Bitwise OR
[Link]("numA ^ numB: " + (numA ^ numB)); // Bitwise XOR
[Link]("~numA: " + (~numA)); // Bitwise NOT
[Link]();
}
}
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 [Link];
public class ControlFlowDemo {
public static void main(String[] args) {
// Using if statement
int x = 10;
if (x > 0) {
[Link]("x is positive");
} else if (x < 0) {
[Link]("x is negative");
} else {
[Link]("x is zero");
}
// Using switch statement
Scanner scanner = new Scanner([Link]);
[Link]("\nEnter a day number (1 to 7): ");
int day = [Link]();
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
case 4:
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
break;
case 6:
[Link]("Saturday");
break;
case 7:
[Link]("Sunday");
break;
default:
[Link]("Invalid day number");
}
// Using different types of loops
// 1. For Loop
[Link]("\nUsing a for loop to print numbers from 1 to 5:");
for (int i = 1; i <= 5; i++) {
[Link](i);
}
// 2. While Loop
[Link]("\nUsing a while loop to print numbers from 6 to 10:");
int j = 6;
while (j <= 10) {
[Link](j);
j++;
}
// 3. Do-While Loop
[Link]("\nUsing a do-while loop to print numbers from 11 to
15:");
int k = 11;
do {
[Link](k);
k++;
} while (k <= 15);
[Link](); // Close the scanner
}
}
Output :-
x is positive
Enter a day number (1 to 7): 3
Wednesday
Using a for loop to print numbers from 1 to 5:
1
2
3
4
5
Using a while loop to print numbers from 6 to 10:
6
7
8
9
10
Using a do-while loop to print numbers from 11 to 15:
11
12
13
14
15
Program :-
public class StringStringBufferDemo {
public static void main(String[] args) {
// String class methods
String str1 = "Hello";
String str2 = "World";
// 1. length()
[Link]("Length of str1: " + [Link]()); // Output: 5
// 2. charAt()
[Link]("Character at index 1 of str1: " + [Link](1)); //
Output: e
// 3. concat()
String str3 = [Link](str2);
[Link]("Concatenated String: " + str3); // Output: HelloWorld
// 4. equals()
boolean isEqual = [Link](str2);
[Link]("str1 equals str2: " + isEqual); // Output: false
// 5. toLowerCase()
[Link]("str1 in lowercase: " + [Link]()); // Output:
hello
// 6. toUpperCase()
[Link]("str2 in uppercase: " + [Link]()); // Output:
WORLD
// 7. substring()
[Link]("Substring of str3 (from index 5): " + [Link](5));
// Output: World
// 8. indexOf()
[Link]("Index of 'o' in str3: " + [Link]('o')); // Output: 4
// 9. replace()
String str4 = [Link]('o', '0');
[Link]("Replaced string: " + str4); // Output: Hell0W0rld
// 10. trim()
String str5 = " Hello World ";
[Link]("Trimmed str5: '" + [Link]() + "'"); // Output: 'Hello
World'
// StringBuilder class methods (StringBuffer is similar to StringBuilder)
StringBuilder sb = new StringBuilder("Hello");
// 1. append()
[Link](" World");
[Link]("StringBuilder after append: " + sb); // Output: Hello
World
// 2. insert()
[Link](5, ",");
[Link]("StringBuilder after insert: " + sb); // Output: Hello,
World
// 3. reverse()
[Link]();
[Link]("Reversed StringBuilder: " + sb); // Output: dlroW
,olleH
// 4. delete()
[Link](0, 6);
[Link]("StringBuilder after delete: " + sb); // Output: ,olleH
// 5. replace()
[Link](1, 3, "oo");
[Link]("StringBuilder after replace: " + sb); // Output: ,oolleH
// 6. capacity()
[Link]("StringBuilder capacity: " + [Link]()); // Output:
16 (default capacity)
// 7. setLength()
[Link](5);
[Link]("StringBuilder after setLength(5): " + sb); // Output:
,ool
// 8. toString()
String sbString = [Link]();
[Link]("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 [Link].*;
public class ArrayVectorDemo {
public static void main(String[] args) {
// 1. Array demonstration
[Link]("Array demonstration:");
// Initialize an array of integers
int[] arr = new int[5];
// Adding values to the array
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
// Accessing elements of the array
[Link]("Array elements:");
for (int i = 0; i < [Link]; i++) {
[Link]("Element at index " + i + ": " + arr[i]);
}
// 2. Vector demonstration
[Link]("\nVector demonstration:");
// Create a Vector to store integers
Vector<Integer> vector = new Vector<>();
// Adding elements to the vector
[Link](100);
[Link](200);
[Link](300);
[Link](400);
[Link](500);
// Accessing elements of the vector
[Link]("Vector elements:");
for (int i = 0; i < [Link](); i++) {
[Link]("Element at index " + i + ": " + [Link](i));
}
// Modifying an element in the vector
[Link](2, 350); // Changing the element at index 2 to 350
[Link]("\nModified Vector:");
for (int i = 0; i < [Link](); i++) {
[Link]("Element at index " + i + ": " + [Link](i));
}
// Removing an element from the vector
[Link](3); // Removing element at index 3
[Link]("\nVector after removal:");
for (int i = 0; i < [Link](); i++) {
[Link]("Element at index " + i + ": " + [Link](i));
}
// Checking if a specific element exists in the vector
if ([Link](200)) {
[Link]("\nVector contains 200.");
} else {
[Link]("\nVector does not contain 200.");
}
}
}
Output :-
Array demonstration:
Array elements:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
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"
[Link]("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
[Link]("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
[Link]("Constructor with String parameter Called");
}
// Method to display object values
public void display() {
[Link]("Num: " + num + ", Name: " + name);
}
}
public class Main {
public static void main(String[] args) {
// 1. Using Default Constructor
ConstructorDemo obj1 = new ConstructorDemo();
[Link](); // Output: Num: 0, Name: Default
// 2. Using Parameterized Constructor
ConstructorDemo obj2 = new ConstructorDemo(25, "John");
[Link](); // Output: Num: 25, Name: John
// 3. Using Constructor Overloading (with String parameter)
ConstructorDemo obj3 = new ConstructorDemo("Alice");
[Link](); // Output: Num: 100, Name: Alice
}
}
Output :-
Default Constructor Called
Num: 0, Name: Default
Parameterized Constructor Called
Num: 25, Name: John
Constructor with String parameter Called
Num: 100
Name: Alice
Program :-
// Parent class for Single Inheritance
class Animal {
String name;
// Constructor of the Animal class
public Animal(String name) {
[Link] = name;
}
// Method in the Animal class
public void eat() {
[Link](name + " is eating.");
}
public void sleep() {
[Link](name + " is sleeping.");
}
}
// Single Inheritance: Dog class inherits from Animal class
class Dog extends Animal {
// Constructor of Dog class
public Dog(String name) {
// Calling the parent (Animal) class constructor
super(name);
}
// Method specific to the Dog class
public void bark() {
[Link](name + " is barking.");
}
}
// Multilevel Inheritance: Mammal class inherits from Animal, and Dog inherits
from Mammal
class Mammal extends Animal {
// Constructor of Mammal class
public Mammal(String name) {
// Calling the parent (Animal) class constructor
super(name);
}
// Method specific to Mammals
public void walk() {
[Link](name + " is walking.");
}
}
// Dog class now extends Mammal (multilevel inheritance)
class Cat extends Mammal {
// Constructor of Cat class
public Cat(String name) {
// Calling the parent (Mammal) class constructor
super(name);
}
// Method specific to Cat class
public void meow() {
[Link](name + " is meowing.");
}
}
public class InheritanceDemo {
public static void main(String[] args) {
// Single Inheritance Example: Dog object inherits from Animal
Dog dog = new Dog("Buddy");
[Link](); // Method from Animal class
[Link](); // Method from Dog class
[Link]();
// Multilevel Inheritance Example: Cat object inherits from Mammal,
which inherits from Animal
Cat cat = new Cat("Whiskers");
[Link](); // Method from Animal class
[Link](); // Method from Mammal class
[Link](); // Method from Cat class
}
}
Output :-
Buddy is eating.
Buddy is barking.
Whiskers is eating.
Whiskers is walking.
Whiskers is meowing.
Program :-
// Defining an interface
interface Animal {
// Abstract method (does not have a body)
void sound();
// Default method with a body
default void sleep() {
[Link]("This animal is sleeping.");
}
}
// Implementing the interface in a class
class Dog implements Animal {
// Providing implementation for the sound() method of the Animal interface
public void sound() {
[Link]("The dog barks.");
}
}
// Implementing the interface in another class
class Cat implements Animal {
// Providing implementation for the sound() method of the Animal interface
public void sound() {
[Link]("The cat meows.");
}
}
public class InterfaceDemo {
public static void main(String[] args) {
// Creating instances of classes that implement the Animal interface
Animal dog = new Dog();
Animal cat = new Cat();
// Calling the sound() method for both animals
[Link](); // Output: The dog barks.
[Link](); // Output: The cat meows.
// Calling the default method sleep() from the interface
[Link](); // Output: This animal is sleeping.
[Link](); // Output: This animal is sleeping.
}
}
Output :-
The dog barks.
The cat meows.
This animal is sleeping.
This animal is sleeping.
Program :-
import [Link]; // Importing the built-in ArrayList class
public class BuiltInPackageDemo {
public static void main(String[] args) {
// Using ArrayList from [Link] package
ArrayList<String> fruits = new ArrayList<>();
// Adding elements to ArrayList
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
// Displaying elements of the ArrayList
[Link]("Fruits List: " + fruits);
}
}
package [Link]; // Defining the package
public class Calculator {
// Method to add two numbers
public int add(int a, int b) {
return a + b;
}
// Method to subtract two numbers
public int subtract(int a, int b) {
return a - b;
} // Method to multiply two numbers
public int multiply(int a, int b) {
return a * b;
}
// Method to divide two numbers
public double divide(int a, int b) {
if (b != 0) {
return (double) a / b;
} else {
[Link]("Cannot divide by zero");
return 0;
}
}
}
Output :-
Fruits List: [Apple, Banana, Cherry]
Output :-
Sum: 15
Difference: 5
Product: 50
Quotient: 2.0