May 11, 2023

Brute Force Techniques I

This is a description

Stacks With Exceptions

public class Stack {
	int[] arr = new int[5];
	int top = -1;

	static class PushException extends Exception {
		public PushException(String message) {
			super(message);
		}
	}
	static class PopException extends Exception {
		public PopException(String message) {
			super(message);
		}
	}

	void push(int data) throws PushException {
		if (top == this.arr.length - 1) {
			throw new PushException("stack is full");
		} else {
			arr[++top] = data;
		}
	}

	int pop() throws PopException {
		if (top < 0) {
			throw new PopException("stack is empty");
		} else {
			return arr[top--];
		}
	}

	void display() {
		if (top < 0) {
			System.out.println("ERROR | Stack is empty!");
		} else {
			for (int i = 0; i <= top; i++) {
				System.out.print(this.arr[i] + " ");
			}
			System.out.println();
		}
	}

	public static void main(String[] args) throws PushException, PopException {
		Stack stack = new Stack();
		stack.push(1);
		stack.push(2);
		stack.push(3);
		stack.push(4);
		stack.push(5);

		stack.display();

		stack.push(6);
	}
}

Current Date

import java.util.Scanner;

public class CurrentDate {
	int day, month, year;
	String date;

	static class InvalidDayException extends Exception {}
	static class InvalidMonthException extends Exception {}

	void createDate() throws InvalidDayException, InvalidMonthException {
		Scanner scanner = new Scanner(System.in);

		System.out.print("Enter day: ");
		this.day = scanner.nextInt();
		if (this.day < 1 || this.day > 31) {
			throw new InvalidDayException();
		}

		System.out.print("Enter month: ");
		this.month = scanner.nextInt();
		if (this.month < 1 || this.month > 12) {
			throw new InvalidMonthException();
		}

		System.out.print("Enter year: ");
		this.year = scanner.nextInt();

		this.date = this.day + "/" + this.month + "/" + this.year;
	}

	public static void main(String[] args) throws InvalidDayException, InvalidMonthException {
		CurrentDate date = new CurrentDate();
		date.createDate();
		System.out.println("Current Date: " + date.date);
	}
}

Student Seats Filled

import java.util.Scanner;

public class Student {
	int regNo;
	int yearOfJoin;
	String name;
	static int number = 25; // 25 seats already filled.

	static class SeatsFilledException extends Exception {
		public SeatsFilledException(String message) {
			super(message);
		}
	}

	public Student(Scanner scanner) throws SeatsFilledException {
		if (number >= 25) {
			throw new SeatsFilledException("all seats are filled");
		}

		System.out.print("Enter name: ");
		this.name = scanner.nextLine();
		System.out.print("Enter year: ");
		this.yearOfJoin = scanner.nextInt();

		number++;
		this.regNo = ((this.yearOfJoin % 100) * 100) + number;

		this.display();
	}

	void display() {
		System.out.println();
		System.out.println("Registration Number: " + regNo);
		System.out.println("Name: " + name);
		System.out.println("Year: " + yearOfJoin);
		System.out.println();
	}

	public static void main(String[] args) throws SeatsFilledException {
		Scanner scanner = new Scanner(System.in);
		Student student = new Student(scanner);
	}
}