0% found this document useful (0 votes)
23 views31 pages

SAMPAD MONDAL - Java - 13001622032

Assignments of Electrical Engineering
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views31 pages

SAMPAD MONDAL - Java - 13001622032

Assignments of Electrical Engineering
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Name – Sampad

Mondal Roll–

13001622036

1. // to reverse a string

import java.util.*;

class reverse

public static void main()

Scanner sc=new Scanner (System. in);

System.out.println ("Enter the string");

String word = sc.nextLine ();

String word1="";

int i,l;char ch;

l=word.length ();

for (i=l-1;i>=0;i--)

ch=word.charAt(i);

word1=word1+ch;

System.out.println ("The Reverse string:"+word1);

==================================OUTPUT=========================================

Enter the string

The quick brown fox

The Reverse string : xof nworb kciuq ehT


2. //to display sum of digits between range 0 and 1000

import java.util.*;

class SumOfDigits

public static void main()

Scanner sc = new Scanner (System. in);

System.out.println ("Enter an integer between 0 and 1000: ");

int number = sc.nextInt();

if (number < 0 || number > 1000)

System.out.println ("Please enter a valid integer between 0 and 1000.");

return;

int sum = 0;

int originalNumber = number;

while (number != 0)

int digit = number % 10;

sum += digit;

number /= 10;

System.out.println("The sum of digits in " + originalNumber + " is: " + sum);

}
}

=================================OUTPUT=========================================

Enter an integer between 0 and 1000: 565

The sum of digits in 565 is: 16

3. //To display the right angle triangle pattern

import java.util.*;

public class pattern_1

public static void main(String[] args)

Scanner sc = new Scanner (System. in);

System.out.println("Input number of rows: ");

int numRows = sc.nextInt();

// Outer loop for rows

for (int i = 1; i <= numRows; i++)

// Inner loop for printing numbers

for (int j = 1; j <= i; j++)

System.out.println(j);

// Move to the next line after each row


System.out.println();

====================================OUTPUT=======================================

Input number of rows: 10

12

123

1234

12345

123456

1234567

12345678

123456789

12345678910

4. //To display pattern of diamond shape

import java.util.*;

public class Pattern

public static void main(String[] args)

Scanner sc = new Scanner(System.in);


System.out.println("Input number of rows (half of the diamond): ");

int numRows = sc.nextInt();

// Upper part of the diamond

for (int i = 1; i <= numRows; i++)

// Print spaces

for (int j = 1; j <= numRows - i; j++)

System.out.println(" ");

// Print '*' characters

for (int k = 1; k <= 2 * i - 1; k++)

System.out.println("*");

System.out.println();

// Lower part of the diamond (excluding the centre row)

for (int i = numRows - 1; i >= 1; i--)

// Print spaces

for (int j = 1; j <= numRows - i; j++)

{
System.out.println(" ");

// Print '*' characters

for (int k = 1; k <= 2 * i - 1; k++)

System.out.println("*");

System.out.println();

========================================OUTPUT===================================

Input number of rows (half of the diamond): 6

***

*****

*******

*********

***********

*********

*******

*****

***
*

5. //Java Program to Check whether a String is a Palindrome or not using Recursive Function

import java.util.*;

public class Palindrome

//Recursive function that checks

//whether the string is palindrome or not

static boolean isPalindrome(String str)

//If string has 0 or 1 character

if(str.length() == 0 || str.length() == 1)

return true;

//If string has multiple characters

//Check whether first and last characters are same or not

if(str.charAt(0) == str.charAt(str.length()-1))

return isPalindrome(str.substring(1, str.length()-1));

return false;

public static void main()

//Take input from the user

Scanner sc = new Scanner(System. in);

System.out.println("Enter the String :");

String str = sc.nextLine();

//Check whether palindrome or not


if (isPalindrome(str))

System.out.println(str+" is palindrome");

else

System.out.println(str+ " is not a palindrome");

======================================OUTPUT=====================================

Enter the String :

mom

mom is palindrome

Enter the String :

techno

techno is not palindrome

6. //to separate even and odd numbers from a given set of arrays and print all even numbers first
then odd numbers

class evenodd

public static void main()

int[] inputArray = {2, 5, 8, 3, 12, 7, 6, 10};

System.out.println("Original Array:");

printArray(inputArray);

separateEvenOdd(inputArray);
System.out.println("\nArray after separating even and odd numbers:");

printArray(inputArray);

public static void separateEvenOdd(int[] arr)

int left = 0;

int right = arr.length - 1;

while (left < right)

while (arr[left] % 2 == 0 && left < right)

left++;

while (arr[right] % 2 == 1 && left < right)

right--;

// Swap the found even and odd numbers

if (left < right)

int temp = arr[left];

arr[left] = arr[right];

arr[right] = temp;

left++;

right--;
}

// Function to print an array

public static void printArray(int[] arr)

for (int num : arr)

System.out.println(num + " ");

System.out.println();

==========================================OUTPUT=================================

Original Array:

2 5 8 3 12 7 6 10

Array after separating even and odd numbers:

2 10 8 6 12 7 3 5

7. class Employee

String name;

private String address;

private double salary;

private String jobTitle;


public Employee(String name, String address, double salary, String jobTitle)

this.name = name;

this.address = address;

this.salary = salary;

this.jobTitle = jobTitle;

public String getName()

return name;

public String getAddress()

return address;

public double getSalary()

return salary;

public String getJobTitle()

return jobTitle;
}

public double calculateBonus()

// Base implementation for bonus calculation

return 0.05 * salary;

public void generatePerformanceReport()

System.out.println("Generating performance report for " + name);

// Implementation for generating performance report

class Manager extends Employee

private String department;

public Manager(String name, String address, double salary, String jobTitle, String department)

super(name, address, salary, jobTitle);

this.department = department;

// Additional methods or overrides specific to Manager


public void manageProject()

System.out.println(name + " is managing a project.");

// Implementation for managing projects

class Developer extends Employee {

private String programmingLanguage;

public Developer(String name, String address, double salary, String jobTitle, String
programmingLanguage) {

super(name, address, salary, jobTitle);

this.programmingLanguage = programmingLanguage;

// Additional methods or overrides specific to Developer

public void writeCode()

System.out.println(name + " is writing code in " + programmingLanguage);

// Implementation for writing code

class Programmer extends Developer {

public Programmer(String name, String address, double salary, String programmingLanguage)

{
super(name, address, salary, "Programmer", programmingLanguage);

// Additional methods or overrides specific to Programmer

class Company

public static void main()

Manager manager = new Manager("John Manager", "123 Main St", 80000, "Manager", "IT");

Developer developer = new Developer("Alice Developer", "456 Oak St", 70000, "Developer",
"Java");

Programmer programmer = new Programmer("Bob Programmer", "789 Pine St", 75000, "C++");

System.out.println(manager.getName() + " earns a bonus of $" + manager.calculateBonus());

manager.manageProject();

System.out.println(developer.getName() + " earns a bonus of $" + developer.calculateBonus());

developer.writeCode();

System.out.println(programmer.getName() + " earns a bonus of $" +


programmer.calculateBonus());

programmer.writeCode();

=========================================OUTPUT==================================

John Manager earns a bonus of $4000.0


John Manager is managing a project.

Alice Developer earns a bonus of $3500.0

Alice Developer is writing code in Java

Bob Programmer earns a bonus of $3750.0

Bob Programmer is writing code in C++

8. // Abstract class BankAccount

abstract class BankAccount

protected double balance;

public BankAccount(double initialBalance)

this.balance = initialBalance;

// Abstract methods

public abstract void deposit(double amount);

public abstract void withdraw(double amount);

public double getBalance()

return balance;

// Subclass SavingsAccount
class SavingsAccount extends BankAccount

private double interestRate;

public SavingsAccount(double initialBalance, double interestRate)

super(initialBalance);

this.interestRate = interestRate;

@Override

public void deposit(double amount)

balance += amount + (amount * interestRate);

System.out.println("Deposit: $" + amount + " (including interest)");

@Override

public void withdraw(double amount)

if (amount <= balance)

balance -= amount;

System.out.println("Withdrawal: $" + amount);

} else

System.out.println("Insufficient funds for withdrawal");


}

// Subclass CurrentAccount

class CurrentAccount extends BankAccount

private double overdraftLimit;

public CurrentAccount(double initialBalance, double overdraftLimit)

super(initialBalance);

this.overdraftLimit = overdraftLimit;

@Override

public void deposit(double amount)

balance += amount;

System.out.println("Deposit: $" + amount);

@Override

public void withdraw(double amount)

if (amount <= balance + overdraftLimit)

{
balance -= amount;

System.out.println("Withdrawal: $" + amount);

} else

System.out.println("Insufficient funds for withdrawal");

// Main class to test the program

class BankDemo

public static void main(String[] args)

// Creating SavingsAccount and CurrentAccount objects

SavingsAccount savingsAccount = new SavingsAccount(1000, 0.05);

CurrentAccount currentAccount = new CurrentAccount(2000, 500);

// Depositing and withdrawing from SavingsAccount

savingsAccount.deposit(500);

savingsAccount.withdraw(200);

// Displaying balance of SavingsAccount

System.out.println("SavingsAccount Balance: $" + savingsAccount.getBalance());

// Depositing and withdrawing from CurrentAccount

currentAccount.deposit(800);
currentAccount.withdraw(2500);

// Displaying balance of CurrentAccount

System.out.println("CurrentAccount Balance: $" + currentAccount.getBalance());

====================================OUTPUT======================================

Deposit: $500.0 (including interest)

Withdrawal: $200.0

SavingsAccount Balance: $1325.0

Deposit: $800.0

Withdrawal: $2500.0

CurrentAccount Balance: $300.0

9. import java.util.ArrayList;

import java.util.List;

class Account

private String accountNumber;

private String accountHolder;

double balance;

public Account(String accountNumber, String accountHolder, double balance)

this.accountNumber = accountNumber;

this.accountHolder = accountHolder;

this.balance = balance;
}

public String getAccountNumber()

return accountNumber;

public String getAccountHolder()

return accountHolder;

public double getBalance()

return balance;

public void deposit(double amount)

if (amount > 0)

balance=balance + amount;

System.out.println("Deposit successful. New balance: " + balance);

} else

System.out.println("Invalid deposit amount");

}
}

public void withdraw(double amount)

if (amount > 0 && amount <= balance)

balance= balance-amount;

System.out.println("Withdrawal successful. New balance: " + balance);

} else

System.out.println("Invalid withdrawal amount or insufficient funds");

class Bank

private List<Account> accounts;

public Bank()

accounts = new ArrayList();

public void addAccount(Account account)

accounts.add(account);
System.out.println("Account added successfully");

public void removeAccount(String accountNumber)

if(accountNumber.equals(accountNumber))

System.out.println("Account removed successfully");

public void displayAccounts()

System.out.println("Accounts in the bank:");

for (Account account : accounts)

System.out.println("Account Number: " + account.getAccountNumber());

System.out.println("Account Holder: " + account.getAccountHolder());

System.out.println("Balance: " + account.getBalance());

System.out.println("--------------");

public Account findAccount(String accountNumber)

for (Account account : accounts)

if (account.getAccountNumber().equals(accountNumber))

{
return account;

return null;

public class BankApplication

public static void main(String[] args)

// Create a bank

Bank bank = new Bank();

// Create and add accounts

Account account1 = new Account("A123", "John Doe", 1000.0);

Account account2 = new Account("B456", "Jane Smith", 1500.0);

bank.addAccount(account1);

bank.addAccount(account2);

// Display accounts

bank.displayAccounts();

// Deposit and withdraw money

Account foundAccount = bank.findAccount("A123");

if (foundAccount != null)
{

foundAccount.deposit(500.0);

foundAccount.withdraw(200.0);

// Remove an account

bank.removeAccount("B456");

// Display updated accounts

bank.displayAccounts();

======================================OUTPUT=====================================

Accounts in the bank:

Account Number: A123

Account Holder: John Doe

Balance: 1000.0

Account Number: B456

Account Holder: Jane Smith

Balance: 1500.0

Deposit successful. New balance: 1500.0

Withdrawal successful. New balance: 1300.0

Account removed successfully

Accounts in the bank:

Account Number:
A123
Account Holder: John Doe

Balance: 1300.0

Account Number: B456

Account Holder: Jane Smith

Balance: 1500.0

10.

public class Employee_1

//Variables

private String name;

private int id;

private double salary;

//Setter and Getter Methods

public void setName(String name)

this.name = name;

public void setId(int id)

this.id = id;

public void setSalary(double salary)


{

this.salary = salary;

public String getName()

return name;

public int getId()

return id;

public double getSalary()

return salary;

// Main method

public static void main(String[] args)

Employee_1 emp1 = new Employee_1();

// Set the variable value using setter methods

emp1.setName("Shivam Kumar");
emp1.setId(2022098);

emp1.setSalary(60000.0);

// Print value

System.out.println("Employee Name: " + emp1.getName());

System.out.println("Employee ID: " + emp1.getId());

System.out.println("Employee Salary: " + emp1.getSalary());

=====================================OUTPUT=====================================

Employee Name: Shivam Kumar

Employee ID: 2022098

Employee Salary: 60000.0

11. // Shape.java

class Shape

void draw()

System.out.println("Drawing a generic shape");

double calculateArea()

System.out.println("Calculating area of a generic shape");

return 0.0;
}

// Circle.java

class Circle extends Shape

double radius;

Circle(double radius)

this.radius = radius;

@Override

void draw()

System.out.println("Drawing a circle");

// Add code to draw a circle

@Override

double calculateArea()

System.out.println("Calculating area of a circle");

return Math.PI * radius * radius;

}
// Cylinder.java

class Cylinder extends Circle

double height;

Cylinder(double radius, double height)

super(radius);

this.height = height;

@Override

void draw()

System.out.println("Drawing a cylinder");

// Add code to draw a cylinder

@Override

double calculateArea()

System.out.println("Calculating surface area of a cylinder");

// Total surface area of a cylinder: 2πr² + 2πrh

return 2 * Math.PI * radius * radius + 2 * Math.PI * radius * height;

}
class Main

public static void main(String[] args)

Circle circle = new Circle(5.0);

circle.draw();

System.out.println("Area of Circle: " + circle.calculateArea());

System.out.println();

Cylinder cylinder = new Cylinder(3.0, 8.0);

cylinder.draw();

System.out.println("Surface Area of Cylinder: " + cylinder.calculateArea());

=======================================OUTPUT====================================

Drawing a circle

Calculating area of a circle

Area of Circle: 78.53981633974483

Drawing a cylinder

Calculating surface area of a cylinder

Surface Area of Cylinder: 207.34511513692635

You might also like