Program 2:-
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;
public class ControlFlowDemo {
public static void main(String[] args) {
// 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");
}
// Using switch statement
Scanner scanner = new Scanner(System.in);
System.out.print("\nEnter a day number (1 to 7): ");
int day = scanner.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day number");
}
// Using different types of loops
// 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);
scanner.close(); // 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()
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'
// StringBuilder class methods (StringBuffer is similar to StringBuilder)
StringBuilder sb = new StringBuilder("Hello");
// 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.*;
public class ArrayVectorDemo {
public static void main(String[] args) {
// 1. Array demonstration
System.out.println("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
System.out.println("Array elements:");
for (int i = 0; i < arr.length; i++) {
System.out.println("Element at index " + i + ": " + arr[i]);
}
// 2. Vector demonstration
System.out.println("\nVector demonstration:");
// Create a Vector to store integers
Vector<Integer> vector = new Vector<>();
// Adding elements to the vector
vector.add(100);
vector.add(200);
vector.add(300);
vector.add(400);
vector.add(500);
// Accessing elements of the vector
System.out.println("Vector elements:");
for (int i = 0; i < vector.size(); i++) {
System.out.println("Element at index " + i + ": " + vector.get(i));
}
// Modifying an element in the vector
vector.set(2, 350); // Changing the element at index 2 to 350
System.out.println("\nModified Vector:");
for (int i = 0; i < vector.size(); i++) {
System.out.println("Element at index " + i + ": " + vector.get(i));
}
// Removing an element from the vector
vector.remove(3); // Removing element at index 3
System.out.println("\nVector after removal:");
for (int i = 0; i < vector.size(); i++) {
System.out.println("Element at index " + i + ": " + vector.get(i));
}
// Checking if a specific element exists in the vector
if (vector.contains(200)) {
System.out.println("\nVector contains 200.");
} else {
System.out.println("\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"
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");
}
// Method to display object values
public void display() {
System.out.println("Num: " + num + ", Name: " + name);
}
}
public class Main {
public static void main(String[] args) {
// 1. Using Default Constructor
ConstructorDemo obj1 = new ConstructorDemo();
obj1.display(); // Output: Num: 0, Name: Default
// 2. Using Parameterized Constructor
ConstructorDemo obj2 = new ConstructorDemo(25, "John");
obj2.display(); // Output: Num: 25, Name: John
// 3. Using Constructor Overloading (with String parameter)
ConstructorDemo obj3 = new ConstructorDemo("Alice");
obj3.display(); // 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) {
this.name = name;
}
// Method in the Animal class
public void eat() {
System.out.println(name + " is eating.");
}
public void sleep() {
System.out.println(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() {
System.out.println(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() {
System.out.println(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() {
System.out.println(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");
dog.eat(); // Method from Animal class
dog.bark(); // Method from Dog class
System.out.println();
// Multilevel Inheritance Example: Cat object inherits from Mammal,
which inherits from Animal
Cat cat = new Cat("Whiskers");
cat.eat(); // Method from Animal class
cat.walk(); // Method from Mammal class
cat.meow(); // 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() {
System.out.println("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() {
System.out.println("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() {
System.out.println("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
dog.sound(); // Output: The dog barks.
cat.sound(); // Output: The cat meows.
// Calling the default method sleep() from the interface
dog.sleep(); // Output: This animal is sleeping.
cat.sleep(); // Output: This animal is sleeping.
}
}
Output :-
The dog barks.
The cat meows.
This animal is sleeping.
This animal is sleeping.
Program :-
import java.util.ArrayList; // Importing the built-in ArrayList class
public class BuiltInPackageDemo {
public static void main(String[] args) {
// Using ArrayList from java.util package
ArrayList<String> fruits = new ArrayList<>();
// Adding elements to ArrayList
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Displaying elements of the ArrayList
System.out.println("Fruits List: " + fruits);
}
}
package com.mycompany.util; // 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 {
System.out.println("Cannot divide by zero");
return 0;
}
}
}
Output :-
Fruits List: [Apple, Banana, Cherry]
Output :-
Sum: 15
Difference: 5
Product: 50
Quotient: 2.0