Java Examples: Banking, Shapes, and Threads
Java Examples: Banking, Shapes, and Threads
The Multiplication program designs multiple threads by creating separate classes that extend Thread: two, three, and five. Each class overrides the run() method to print multiplication tables. In main, each thread is instantiated and started. This parallel execution showcases multithreading, allowing concurrent calculation of tables, improving program responsiveness and efficiency .
Method overloading, as demonstrated by the shape class constructors, allows similar methods to coexist with different parameters. This flexibility supports different functionality under one class, like calculating areas for different shapes, facilitating code reusability and adapting to varied requirements without altering method names or logical flow extensively .
Java classes such as bank, shape, and employee demonstrate encapsulation by containing attributes and methods related to specific functionalities. Inheritance is shown in sports and salary classes, enabling code reuse and polymorphism. Interfaces offer a way to define constants and behaviors, exemplified by bonus. These features collectively illustrate OOP principles like encapsulation, inheritance, and polymorphism, providing structure and advanced functionality .
Scanner objects are used for input handling in these Java programs, allowing the user to input values for balance, deposits, withdrawals, marks, salary components, and age. By calling methods like nextInt() and nextFloat(), Scanner facilitates reading and storing user inputs, essential for dynamic and interactive programs that process various user-supplied data .
The shape class uses constructors with different signatures to allow for calculating and displaying the area of different geometric shapes. One constructor takes one argument and calculates the area of a square, while the other takes two arguments and calculates the area of a rectangle. This use of constructors demonstrates method overloading in Java .
The bank class in this code handles deposit and withdrawal by using separate methods for each operation. The get() method initializes the balance amount, deposit() increases the balance by adding the deposited amount, and Withdrawal() decreases the balance by subtracting the withdrawal amount. Each method reads input from the user using a Scanner object and updates the balance, which affects the current balance shown after each transaction .
The salary class calculates gross pay by summing basic pay, house rent allowance, dearness allowance, and a constant bonus defined by the bonus interface. Net pay is then calculated by subtracting provident fund (pf) from the gross pay. The bonus interface is implemented by the salary class to utilize the constant bonus value in the gross pay calculation, showing how interfaces provide a mechanism to define constants and allow classes to utilize them uniformly .
In the invalidage class, the custom exception's error message clearly informs the user of the specific reason for error—"Age must be greater than 18 for vote"—improving error resolution and user understanding. This clarity in error messaging enhances program robustness and user experience by providing direct feedback on input validity .
The Customerexception class demonstrates custom exceptions through the invalidage class, which extends Exception. This custom exception is used to validate age input; if the age is less than 18, an invalidage exception is thrown, displaying a message that age must be greater than 18 to vote. This showcases how custom exceptions can enforce specific application rules and provide meaningful error messages .
The sports class extends the semester class, indicating inheritance, which allows sports to use the properties and methods of semester. Additionally, sports adds its own method getsports() to include sports marks in the total calculation. The calculate() method in sports uses both inherited subject marks and sports marks, demonstrating method utilization and overriding potential in subclasses .