Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views16 pages

Experiment 1: Cse-B Azad

The document outlines a series of Java programming experiments conducted by Sriyanshu Azad. It includes code and explanations for printing numbers with specific conditions (FizzBuzz), creating an abstract class for bank accounts with subclasses for savings and current accounts, checking for prime numbers in a range, and solving the Josephus problem to find a safe position among soldiers. Each experiment is accompanied by theoretical explanations, code implementations, and expected outputs.

Uploaded by

maneetrathee98
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)
11 views16 pages

Experiment 1: Cse-B Azad

The document outlines a series of Java programming experiments conducted by Sriyanshu Azad. It includes code and explanations for printing numbers with specific conditions (FizzBuzz), creating an abstract class for bank accounts with subclasses for savings and current accounts, checking for prime numbers in a range, and solving the Josephus problem to find a safe position among soldiers. Each experiment is accompanied by theoretical explanations, code implementations, and expected outputs.

Uploaded by

maneetrathee98
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/ 16

CSE-B 07117702722 Sriyanshu Azad

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".

Theory:
CSE-B 07117702722 Sriyanshu Azad
Code:
import java.util.Scanner;

class prac1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int l,u;
System.out.println("Enter upper and lower bound of range");
l=sc.nextInt();
u=sc.nextInt();
for(int i=l;i<=u;i++)
{
if(i%15 == 0)
System.out.println("FizzBuzz");
else if(i%3==0)
System.out.println("Fizz");
else if(i%5==0)
System.out.println("Buzz");
else
System.out.println(i);
}
sc.close();
}
}

Output:
CSE-B 07117702722 Sriyanshu Azad
Learning Outcomes:
CSE-B 07117702722 Sriyanshu Azad

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.

Theory:
CSE-B 07117702722 Sriyanshu Azad
CSE-B 07117702722 Sriyanshu Azad
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()


{
System.out.println("Account number: "+accountNumber);
System.out.println("Balance: "+balance);
System.out.println();
}
}

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);
System.out.println("Deposit successful. New Balance:
"+balance);
}
else
{
System.out.println("Invalid Amount");
}
}

public void withdraw(double amount)


{
if(balance-amount>500)
{
balance -= amount;
System.out.println("Amount withdrawn: "+amount);
CSE-B 07117702722 Sriyanshu Azad
System.out.println("Balance amount is: "+balance);
}
else
{
System.out.println("Insufficient Balance");
}
}
}

class CurrentAccount extends BankAccount


{
double overdraftLimit;

CurrentAccount(String accountNumber, double balance, double odl)


{
super(accountNumber, balance);
overdraftLimit=odl;
}

public void deposit(double amount)


{
if(amount>0)
{
balance += amount;
System.out.println("Deposit successful. New Balance:
"+balance);
}
else
{
System.out.println("Invalid Amount");
}
}

public void withdraw(double amount)


{
if(balance-amount > overdraftLimit)
{
balance -= amount;
System.out.println("Withdrawal Successful. New balance:
"+balance);
}
else
{
System.out.println(“SRI Invalid amount. Overdraft Limit
exceeded.");
}
}
}

public class Main {


public static void main(String[] args) {
SavingsAccount savingsAccount = new SavingsAccount("SA123",
1000, 5);
System.out.println("\nSavings Account:");
savingsAccount.disp();
CSE-B 07117702722 Sriyanshu Azad
savingsAccount.deposit(500);
savingsAccount.withdraw(200);
savingsAccount.withdraw(1000);
System.out.println("Final account details are:");
savingsAccount.disp();

System.out.println();

CurrentAccount currentAccount = new CurrentAccount("CA456",


2000, 1000);
System.out.println("\nCurrent Account");
currentAccount.disp();
currentAccount.deposit(300);
currentAccount.withdraw(2500);
currentAccount.withdraw(1000);
System.out.println("Final account details are:”);
currentAccount.disp();
}
}

Output:
CSE-B 07117702722 Sriyanshu Azad
Learning Outcomes:
CSE-B 07117702722 Sriyanshu Azad

Experiment 3
Aim: Write an efficient code in java to check all the prime numbers in a given
range of numbers.

Theory:
CSE-B 07117702722 Sriyanshu Azad
Code:
import java.util.Scanner;

class prac3 {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int l,u;
System.out.println("Enter upper and lower bound of range");
l=sc.nextInt();
u=sc.nextInt();
for(int n=l;n<=u;n++)
{
int flag=0;
if(n==1)
{
System.out.println("Not Prime");
continue;
}
for(int i=2;i<=Math.sqrt(n);i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
System.out.println("Prime");
else
System.out.println("Not Prime");
}
sc.close();
}
}

Output:
CSE-B 07117702722 Sriyanshu Azad
Learning Outcomes:
CSE-B 07117702722 Sriyanshu Azad

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 07117702722 Sriyanshu Azad
Code:
import java.util.Scanner;
class prac4
{
class Node
{
int data;
Node next;

public Node(int data)


{
this.data=data;
this.next=null;
}

private Node head;


private Node tail;

public prac4()
{
this.head=null;
this.tail=null;
}

public void add(int data)


{
Node newNode= new Node(data);
if(head==null)
{
head=newNode;
tail=newNode;
}
else
{
tail.next=newNode;
tail=newNode;
}
}

public void kill()


{
tail.next=head;
Node current=head;
while(current.next != current)
{
current.next= current.next.next;
current=current.next;
}
System.out.println("SRISafe position is: "+current.data);
}

public static void main(String args[])


CSE-B 07117702722 Sriyanshu Azad
{
Scanner sc=new Scanner(System.in);
prac4 obj=new prac4();

System.out.println("\nEnter number of Soldiers: ");


int n = sc.nextInt();

for(int i=1;i<=n;i++)
{
obj.add(i);
}
obj.kill();

sc.close();
System.exit(0);
}
}

Output:

Alternate Method Code:


public class JosephusProblem {

public static int findSafePosition(int n)


{ int largestPowerOf2 = 1;
while (largestPowerOf2 * 2 <= n)
{ largestPowerOf2 *= 2;
}

int l = n - largestPowerOf2;
int safePosition = 2 * l + 1;

return safePosition;
}

public static void main(String[] args)


{ int n = 10;
int safePosition = findSafePosition(n);
System.out.println("The safe position for " + n + " soldiers is:
" + safePosition);
}
}
CSE-B 07117702722 Sriyanshu Azad
Output:

Learning Outcomes:

You might also like