Skip to content

Commit 606cfd5

Browse files
committed
3 Programs
1 parent 2f440e2 commit 606cfd5

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

ucer/4B4/BankDemo.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
class BankAccount {
2+
protected String accountHolder;
3+
protected double balance;
4+
5+
public BankAccount(String accountHolder, double balance) {
6+
this.accountHolder = accountHolder;
7+
this.balance = balance;
8+
}
9+
10+
public void deposit(double amount) {
11+
if (amount > 0) {
12+
balance += amount;
13+
System.out.println(amount + " deposited. New balance: " + balance);
14+
} else {
15+
System.out.println("Invalid deposit amount.");
16+
}
17+
}
18+
19+
public void withdraw(double amount) {
20+
if (amount <= balance) {
21+
balance -= amount;
22+
System.out.println( amount + " withdrawn. New balance: " + balance);
23+
} else {
24+
System.out.println("Insufficient balance.");
25+
}
26+
}
27+
28+
public void displayBalance() {
29+
System.out.println(accountHolder + "Balance: " + balance);
30+
}
31+
}
32+
33+
class SavingsAccount extends BankAccount {
34+
35+
public SavingsAccount(String accountHolder, double balance) {
36+
super(accountHolder, balance);
37+
}
38+
39+
@Override
40+
public void withdraw(double amount) {
41+
if (balance - amount < 100) {
42+
System.out.println("Withdrawal denied. Balance cannot go below 100 in Savings Account.");
43+
} else {
44+
super.withdraw(amount);
45+
}
46+
}
47+
}
48+
49+
class CurrentAccount extends BankAccount {
50+
private final double overdraftLimit = 5000;
51+
52+
public CurrentAccount(String accountHolder, double balance) {
53+
super(accountHolder, balance);
54+
}
55+
56+
@Override
57+
public void withdraw(double amount) {
58+
if (balance - amount < -overdraftLimit) {
59+
System.out.println("Withdrawal denied. Overdraft limit of 5000 exceeded in Current Account.");
60+
} else {
61+
balance -= amount;
62+
System.out.println(amount + " withdrawn. New balance: " + balance);
63+
}
64+
}
65+
}
66+
67+
public class BankDemo {
68+
public static void main(String[] args) {
69+
// Creating instances
70+
SavingsAccount savings = new SavingsAccount("Alice", 1000);
71+
CurrentAccount current = new CurrentAccount("Bob", 2000);
72+
73+
// Transactions on SavingsAccount
74+
System.out.println("\n--- Savings Account Transactions ---");
75+
savings.displayBalance();
76+
savings.deposit(500);
77+
savings.withdraw(1400); // Should fail
78+
savings.withdraw(200); // Should succeed
79+
savings.displayBalance();
80+
81+
// Transactions on CurrentAccount
82+
System.out.println("\n--- Current Account Transactions ---");
83+
current.displayBalance();
84+
current.withdraw(6000); // Should succeed (within overdraft)
85+
current.withdraw(2000); // Should fail (overdraft exceeded)
86+
current.deposit(3000);
87+
current.displayBalance();
88+
}
89+
}
90+

ucer/4B4/VehicleTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Abstract class Vehicle
2+
abstract class Vehicle {
3+
protected String brand;
4+
5+
public Vehicle(String brand) {
6+
this.brand = brand;
7+
}
8+
9+
// Abstract method
10+
public abstract void startEngine();
11+
}
12+
13+
// Interface FuelType
14+
interface FuelType {
15+
void fuelEfficiency(); // No body since it's an interface method
16+
}
17+
18+
// Car class extending Vehicle and implementing FuelType
19+
class Car extends Vehicle implements FuelType {
20+
21+
public Car(String brand) {
22+
super(brand);
23+
}
24+
25+
@Override
26+
public void startEngine() {
27+
System.out.println(brand + " Car engine started with key ignition.");
28+
}
29+
30+
@Override
31+
public void fuelEfficiency() {
32+
System.out.println(brand + " Car has fuel efficiency of 15 km/l.");
33+
}
34+
}
35+
36+
// Bike class extending Vehicle and implementing FuelType
37+
class Bike extends Vehicle implements FuelType {
38+
39+
public Bike(String brand) {
40+
super(brand);
41+
}
42+
43+
@Override
44+
public void startEngine() {
45+
System.out.println(brand + " Bike engine started with kick start.");
46+
}
47+
48+
@Override
49+
public void fuelEfficiency() {
50+
System.out.println(brand + " Bike has fuel efficiency of 45 km/l.");
51+
}
52+
}
53+
54+
// Main class to test the implementation
55+
public class VehicleTest {
56+
public static void main(String[] args) {
57+
Car myCar = new Car("Toyota");
58+
Bike myBike = new Bike("Honda");
59+
60+
System.out.println("--- Car ---");
61+
myCar.startEngine();
62+
myCar.fuelEfficiency();
63+
64+
System.out.println("\n--- Bike ---");
65+
myBike.startEngine();
66+
myBike.fuelEfficiency();
67+
}
68+
}
69+

ucer/4B4/hello.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is a Java Programming Lab.
2+
We are learning file reading and writing.
3+
We are applying try-with-resource method.

ucer/4B4/readwrite.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Java program for try-with-resources having multiple resources
2+
3+
import java.io.*;
4+
5+
class readwrite {
6+
public static void main(String[] args)
7+
{
8+
try (FileOutputStream f
9+
= new FileOutputStream("outputfile.txt");
10+
BufferedReader br = new BufferedReader(
11+
new FileReader("hello.txt"))) {
12+
String text;
13+
while ((text = br.readLine()) != null) {
14+
byte arr[] = text.getBytes();
15+
f.write(arr);
16+
}
17+
18+
System.out.println(
19+
"File content copied to another one.");
20+
}
21+
catch (Exception e) {
22+
System.out.println(e);
23+
}
24+
System.out.println(
25+
"Resource are closed and message has been written into the file.");
26+
}
27+
}

0 commit comments

Comments
 (0)