CSE B 07917702722 MANEET
Experiment 1
Aim: Write a Java program to print numbers from 1 to n. For multiples of 3,
print "Fizz" instead of the number, and for multiples of 5, print "Buzz". For
numbers that are multiples of both 3 and 5, print "FizzBuzz".
Code:
import [Link];
class fizz {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter 1st number: ");
int a = [Link]();
[Link]("Enter 2nd number: ");
int b = [Link]();
for(int i=a;i<=b;i++)
{
if(i%15==0)
[Link]("FizzBuzz");
else if(i%3==0)
[Link]("Fizz");
else if(i%5==0)
[Link]("Buzz");
else
[Link](i);
}
}
}
CSE B 07917702722 MANEET
Output:
Learning Outcomes:
CSE B 07917702722 MANEET
Experiment 2
Aim: Create an abstract class BankAccount with the following:
a) An accountNumber (String) and balance (double) as instance variables.
b) A constructor to initialize the account number and balance.
c) Abstract methods:
• deposit(double amount)
• withdraw(double amount)
d) Create two subclasses:
SavingsAccount:
• Has an additional variable interestRate (double).
• Overrides the deposit method to add interest to the balance. • Withdrawals are
allowed only if the balance remains above a certain minimum (e.g., 500).
CurrentAccount:
• Has an additional variable overdraftLimit (double).
• Overrides the withdraw method to allow overdraft up to the specified limit.
Write a program that:
e) Creates objects of both subclasses.
f) Performs deposit and withdrawal operations.
g) Displays the final account details for each object.
Code:
abstract class BankAccount {
String accountNumber;
double balance;
BankAccount() {}
BankAccount(String a, double b) {
accountNumber = a;
balance = b;
}
abstract void deposit(double amount);
abstract void withdraw(double amount);
public void disp() {
[Link]("Account number: " + accountNumber);
[Link]("Balance: " + balance);
[Link]();
}
CSE B 07917702722 MANEET
class SavingsAccount extends BankAccount {
double interestRate;
SavingsAccount() {}
SavingsAccount(String accountNumber, double balance, double ir) {
super(accountNumber, balance);
interestRate = ir;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
balance += balance * (interestRate / 100);
[Link]("Deposit successful. New Balance: " +
balance);
} else {
[Link]("Invalid Amount");
}
}
public void withdraw(double amount) {
if (balance - amount > 500) {
balance -= amount;
[Link]("Amount withdrawn: " + amount);
[Link]("Balance amount is: " + balance);
} else {
[Link]("Insufficient Balance");
}
}
}
class CurrentAccount extends BankAccount {
double overdraftLimit;
CurrentAccount(String accountNumber, double balance, double odl) {
super(accountNumber, balance);
overdraftLimit = odl;
CSE B 07917702722 MANEET
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
[Link]("Deposit successful. New Balance: " +
balance);
} else {
[Link]("Invalid Amount");
}
}
public void withdraw(double amount) {
if (balance - amount > overdraftLimit) {
balance -= amount;
[Link]("Withdrawal Successful. New balance: " +
balance);
} else {
[Link]("Invalid amount. Overdraft Limit
exceeded.");
}
}
}
public class Main {
public static void main(String[] args) {
SavingsAccount savingsAccount = new SavingsAccount("SA123", 1000,
5);
[Link]("\nSavings Account:");
[Link]();
[Link](500);
[Link](200);
[Link](1000);
[Link]("Final account details are:");
[Link]();
[Link]();
CurrentAccount currentAccount = new CurrentAccount("CA456", 2000,
1000);
[Link]("\nCurrent Account:");
CSE B 07917702722 MANEET
[Link]();
[Link](300);
[Link](2500);
[Link](1000);
[Link]("Final account details are:");
[Link]();
}
}
CSE B 07917702722 MANEET
Output:
Learning Outcomes:
CSE B 07917702722 MANEET
Experiment 3
Aim: Write an efficient code in java to check all the prime numbers in a given
range of numbers.
CSE B 07917702722 MANEET
Code:
import [Link];
public class Prime1 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the starting number (a): ");
int a = [Link]();
[Link]("Enter the ending number (b): ");
int b = [Link]();
for (int i = a; i <= b; i++) {
int c = 0;
if (i % 2 == 0 && i > 2) {
[Link](i + " is not a prime number");
continue;
}
for (int j = 2; j <= [Link](i); j++) {
if (i % j == 0) {
c++;
break;
}
}
if (c == 0 && i > 1) {
[Link](i + " is a prime number");
CSE B 07917702722 MANEET
}
else {
[Link](i + " is not a prime number");
}
}
[Link]();
}
}
Output:
Learning Outcomes:
CSE B 07917702722 MANEET
Experiment 4
Aim: N soldiers (or people) stand in a circle. The king gives a sword to the first
soldier, who kills the person to their left and passes the sword to the next
surviving person. This process continues until only one person remains. Write a
java program to find the safe position to stand. Assuming first position is=1.
Theory:
CSE B 07917702722 MANEET
Code:
import [Link];
public class Soldiers {
public static int solution(int n) {
if (n == 1)
return 1;
else
return (solution(n - 1) + 1) % n + 1;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of soldiers: ");
int n = [Link]();
[Link]();
int safePosition = solution(n);
[Link]("The safe position is: " + safePosition);
}
}
CSE B 07917702722 MANEET
Output:
Learning Outcomes:
CSE B 07917702722 MANEET
Experiment 5
Aim: Write a two thread based program in java, where thread1 adds
all even numbers and thread2 adds all odd numbers from a given file
which has large number of positive integers.
Code:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class numbers {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("[Link]")) {
Random r = new Random();
for (int i = 0; i < 10; i++) {
int num = [Link](1000) + 1;
[Link]([Link]("%d%n", num));
}
} catch (IOException e) {
[Link]("Error writing to file: " +
[Link]());
}
[Link]("\nRandom numbers saved to [Link]");
ExecutorService executor = [Link](2);
[Link](new EvenNumberAdder("[Link]"));
[Link](new OddNumberAdder("[Link]"));
[Link]();
}
}
class EvenNumberAdder implements Runnable {
CSE B 07917702722 MANEET
private String filename;
public EvenNumberAdder(String filename) {
[Link] = filename;
}
@Override
public void run() {
try (Scanner scanner = new Scanner(new [Link](filename)))
{
int sum = 0;
while ([Link]()) {
int num = [Link]();
if (num % 2 == 0) {
sum += num;
}
}
[Link]("Sum of even numbers: " + sum);
} catch (IOException e) {
[Link]("Error reading from file: " +
[Link]());
}
}
}
class OddNumberAdder implements Runnable {
private String filename;
public OddNumberAdder(String filename) {
[Link] = filename;
}
@Override
public void run() {
try (Scanner scanner = new Scanner(new [Link](filename)))
{
int sum = 0;
CSE B 07917702722 MANEET
while ([Link]()) {
int num = [Link]();
if (num % 2 != 0) {
sum += num;
}
}
[Link]("Sum of odd numbers: " + sum);
} catch (IOException e) {
[Link]("Error reading from file: " +
[Link]());
}
}
}
Output:
Learning Outcomes:
CSE B 07917702722 MANEET
Experiment 6
Aim: Write a java program using Java Database
Connectivity (JDBC) to:
1. Fetch all the records from the employee table.
2. Search employee with empid
3. Update specific employee details
4. Delete a particular employee record
Employee has fields in MySQL Empid, EmpName,
EmpCity, EmpSalary.
Setup:
CSE B 07917702722 MANEET
CSE B 07917702722 MANEET
Code:
import [Link].*;
import [Link];
public class jdbc {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/maneet"; // Change
"testdb" to your database name
String user = "root"; // Default XAMPP user
String password = ""; // Default XAMPP password (empty)
try {
// Load MySQL JDBC Driver
[Link]("[Link]");
Connection conn = [Link](url, user,
password);
[Link]("\nConnected to the database!");
Scanner scanner = new Scanner([Link]);
while (true) {
[Link]("\nEnter an SQL query (or type 'exit'
to quit):");
String query = [Link]().trim();
if ([Link]("exit")) {
[Link]("Exiting program...");
break;
}
try (Statement stmt = [Link]()) {
if ([Link]().startsWith("select")) {
ResultSet rs = [Link](query);
ResultSetMetaData metaData = [Link]();
int columnCount = [Link]();
// Print column names
for (int i = 1; i <= columnCount; i++) {
[Link]("%-15s",
[Link](i));
}
[Link]("\
n-------------------------------------------------");
// Print rows
while ([Link]()) {
for (int i = 1; i <= columnCount; i++) {
[Link]("%-15s",
[Link](i));
}
[Link]();
}
} else {
CSE B 07917702722 MANEET
int rowsAffected = [Link](query);
[Link]("Query executed successfully.
Rows affected: " + rowsAffected);
}
} catch (SQLException e) {
[Link]("Error executing query: " +
[Link]());
}
}
// Close resources
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
}
}
Output:
Learning Outcomes: