NOTHIING
NOTHIING
JAVA Page 2
}
JAVA Page 4
System.out.print(" ".repeat(n - i));
System.out.println("*".repeat(2 * i - 1));
}
for (int i = n - 1; i >= 1; i--) {
System.out.print(" ".repeat(n - i));
System.out.println("*".repeat(2 * i - 1));
}
}
}
JAVA Page 5
public class CharFrequency {
public static void main(String[] args) {
String str = "hello world";
HashMap<Character, Integer> freq = new HashMap<>();
for (char ch : str.toCharArray()) {
if (ch != ' ')
freq.put(ch, freq.getOrDefault(ch, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : freq.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
JAVA Page 6
balance -= amount;
}
public double checkBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.deposit(1000);
acc.withdraw(500);
System.out.println("Balance: " + acc.checkBalance()); // 500
}
}
JAVA Page 7
return Math.PI * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
Shape s1 = new Rectangle();
Shape s2 = new Circle();
System.out.println(s1.area()); // 15.0
System.out.println(s2.area()); // ~50.27
}
}
JAVA Page 8
System.out.println(calc.add(2, 3)); // 5
System.out.println(calc.add(2.5, 3.5)); // 6.0
System.out.println(calc.add(1, 2, 3)); // 6
}
}
JAVA Page 9
✅ 9. Sortable interface with BubbleSort & SelectionSort
java
Copy code
interface Sortable {
void sort(int[] arr);
}
class BubbleSort implements Sortable {
public void sort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++)
for (int j = 0; j < arr.length - 1 - i; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
class SelectionSort implements Sortable {
public void sort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[min]) min = j;
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
public class Main {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1};
Sortable sorter = new BubbleSort(); // or new SelectionSort();
sorter.sort(arr);
for (int i : arr) System.out.print(i + " "); // 1 2 5 9
}
}
JAVA Page 10
}
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(1000);
account.withdraw(500);
System.out.println("Current Balance: " + account.checkBalance());
}
}
JAVA Page 11
}
public static void main(String[] args) {
Shape s1 = new Rectangle(4, 5);
Shape s2 = new Circle(3);
System.out.println("Rectangle Area: " + s1.area());
System.out.println("Circle Area: " + s2.area());
}
}
JAVA Page 12
void swim();
}
class Duck implements Flyable, Swimmable {
public void fly() {
System.out.println("Duck is flying");
}
public void swim() {
System.out.println("Duck is swimming");
}
public static void main(String[] args) {
Duck d = new Duck();
d.fly();
d.swim();
}
}
JAVA Page 13
}
class UPIPayment extends Payment {
void pay(double amount) {
System.out.println("Paid ₹" + amount + " using UPI");
}
public static void main(String[] args) {
Payment p1 = new CreditCardPayment();
Payment p2 = new UPIPayment();
p1.pay(500);
p2.pay(300);
}
}
JAVA Page 14
}
}
class SortTest {
public static void main(String[] args) {
int[] arr = {5, 3, 8, 1, 2};
Sortable sorter = new BubbleSort(); // or new SelectionSort();
sorter.sort(arr);
for (int i : arr)
System.out.print(i + " ");
}
}
JAVA Page 15