Skip to content

Commit 1e98e2b

Browse files
committed
ucer codes
1 parent e44526c commit 1e98e2b

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

ucer/4B4/Animal.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Animal {
2+
// Instance variable
3+
String name;
4+
5+
// Method to make a generic sound
6+
public void makeSound() {
7+
System.out.println("Some sound...");
8+
}
9+
10+
// Setter for name
11+
public void setName(String name) {
12+
this.name = name;
13+
}
14+
15+
// Getter for name
16+
public String getName() {
17+
return name;
18+
}
19+
}
20+
21+
// Dog.java
22+
class Dog extends Animal {
23+
// Overriding the makeSound method
24+
@Override
25+
public void makeSound() {
26+
System.out.println("Bark! Bark!");
27+
}
28+
}
29+
30+
// Main class
31+
public class Main {
32+
public static void main(String[] args) {
33+
// Create an instance of Dog
34+
Dog myDog = new Dog();
35+
36+
// Set the dog's name
37+
myDog.setName("Buddy");
38+
39+
// Call makeSound()
40+
myDog.makeSound();
41+
42+
// Print the dog's name
43+
System.out.println("Dog's name is: " + myDog.getName());
44+
}
45+
}

ucer/4B4/BankOperation.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Base class
2+
class BankAccount {
3+
protected double balance;
4+
5+
public BankAccount(double initialBalance) {
6+
this.balance = initialBalance;
7+
}
8+
9+
public void deposit(double amount) {
10+
if (amount > 0) {
11+
balance += amount;
12+
System.out.println("Deposited ₹" + amount);
13+
} else {
14+
System.out.println("Deposit amount must be positive.");
15+
}
16+
}
17+
18+
public void withdraw(double amount) {
19+
if (amount > 0 && amount <= balance) {
20+
balance -= amount;
21+
System.out.println("Withdrew ₹" + amount);
22+
} else {
23+
System.out.println("Insufficient balance or invalid amount.");
24+
}
25+
}
26+
27+
public void displayBalance() {
28+
System.out.println("Current Balance: ₹" + balance);
29+
}
30+
}
31+
32+
// SavingsAccount subclass
33+
class SavingsAccount extends BankAccount {
34+
public SavingsAccount(double initialBalance) {
35+
super(initialBalance);
36+
}
37+
38+
@Override
39+
public void withdraw(double amount) {
40+
if (balance - amount < 100) {
41+
System.out.println("Cannot withdraw ₹" + amount + ". Minimum balance of ₹100 required.");
42+
} else {
43+
super.withdraw(amount);
44+
}
45+
}
46+
}
47+
48+
// CurrentAccount subclass
49+
class CurrentAccount extends BankAccount {
50+
public CurrentAccount(double initialBalance) {
51+
super(initialBalance);
52+
}
53+
54+
@Override
55+
public void withdraw(double amount) {
56+
if (balance - amount < -5000) {
57+
System.out.println("Cannot withdraw ₹" + amount + ". Overdraft limit of ₹5000 exceeded.");
58+
} else {
59+
balance -= amount;
60+
System.out.println("Withdrew ₹" + amount);
61+
}
62+
}
63+
}
64+
65+
// Main class to test accounts
66+
public class BankOperation{
67+
public static void main(String[] args) {
68+
System.out.println("=== Savings Account ===");
69+
SavingsAccount savings = new SavingsAccount(1000);
70+
savings.displayBalance();
71+
savings.withdraw(950); // Should fail due to ₹100 min balance
72+
savings.withdraw(800); // Should succeed
73+
savings.displayBalance();
74+
75+
System.out.println("\n=== Current Account ===");
76+
CurrentAccount current = new CurrentAccount(2000);
77+
current.displayBalance();
78+
current.withdraw(6000); // Should succeed (within overdraft)
79+
current.withdraw(2000); // Should fail (overdraft exceeded)
80+
current.deposit(3000); // Deposit to reduce overdraft
81+
current.displayBalance();
82+
}
83+
}

ucer/4B4/Employee.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Employee.java
2+
public class Employee {
3+
// Attributes
4+
int empID;
5+
String empName;
6+
String empDesignation;
7+
8+
// Constructor to initialize attributes
9+
public Employee(int id, String name, String designation) {
10+
this.empID = id;
11+
this.empName = name;
12+
this.empDesignation = designation;
13+
}
14+
15+
// Method to display employee details
16+
public void displayDetails() {
17+
System.out.println("Employee ID: " + empID);
18+
System.out.println("Employee Name: " + empName);
19+
System.out.println("Designation: " + empDesignation);
20+
System.out.println("---------------------------");
21+
}
22+
23+
// Main method to create and display employee objects
24+
public static void main(String[] args) {
25+
// Create three employee objects
26+
Employee emp1 = new Employee(101, "Alice Johnson", "Software Engineer");
27+
Employee emp2 = new Employee(102, "Bob Smith", "Project Manager");
28+
Employee emp3 = new Employee(103, "Charlie Brown", "QA Analyst");
29+
30+
// Display details of each employee
31+
emp1.displayDetails();
32+
emp2.displayDetails();
33+
emp3.displayDetails();
34+
}
35+
}

ucer/4B4/SecondLargest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class SecondLargest {
2+
public static void main(String[] args) {
3+
// Check if at least two numbers are provided
4+
if (args.length < 2) {
5+
System.out.println("Please enter at least two numbers as command-line arguments.");
6+
return;
7+
}
8+
9+
int[] numbers = new int[args.length];
10+
11+
// Parse command line arguments to integers
12+
for (int i = 0; i < args.length; i++) {
13+
try {
14+
numbers[i] = Integer.parseInt(args[i]);
15+
} catch (NumberFormatException e) {
16+
System.out.println("Invalid input: " + args[i] + " is not a valid integer.");
17+
return;
18+
}
19+
}
20+
21+
// Find the largest and second largest numbers
22+
int largest = Integer.MIN_VALUE;
23+
int secondLargest = Integer.MIN_VALUE;
24+
25+
for (int num : numbers) {
26+
if (num > largest) {
27+
secondLargest = largest;
28+
largest = num;
29+
} else if (num > secondLargest && num != largest) {
30+
secondLargest = num;
31+
}
32+
}
33+
34+
// Handle the case when all elements are the same
35+
if (secondLargest == Integer.MIN_VALUE) {
36+
System.out.println("There is no distinct second largest number.");
37+
} else {
38+
System.out.println("The second largest number is: " + secondLargest);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)