0% found this document useful (0 votes)
10 views8 pages

Java Examples: Banking, Shapes, and Threads

The document contains multiple Java programming examples demonstrating various concepts such as banking operations, shape area calculations using constructors, student marks calculation with inheritance, employee salary computation with interfaces, exception handling for age validation, and multithreading for multiplication tables. Each example includes a class structure, methods for user input, and output results. The outputs illustrate the functionality of the code snippets, showing how they operate in practice.

Uploaded by

sridevir112
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Java Examples: Banking, Shapes, and Threads

The document contains multiple Java programming examples demonstrating various concepts such as banking operations, shape area calculations using constructors, student marks calculation with inheritance, employee salary computation with interfaces, exception handling for age validation, and multithreading for multiplication tables. Each example includes a class structure, methods for user input, and output results. The outputs illustrate the functionality of the code snippets, showing how they operate in practice.

Uploaded by

sridevir112
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXP : 1(a)

import [Link].*;
class bank
{
float bamt,damt,wamt;
void get()
{
Scanner inp=new Scanner([Link]);
[Link]("Enter Balance Amount: ");
bamt=[Link]();
}
void deposit()
{
Scanner inp=new Scanner([Link]);
[Link]("Enter Deposit Amount: ");
damt=[Link]();
bamt=bamt+damt;
[Link]("Current Balance: "+bamt);
}
void Withdrawal()
{
Scanner inp=new Scanner([Link]);
[Link]("Enter Withdrawal Amount: ");
wamt=[Link]();
bamt=bamt-wamt;
[Link]("Current Balance: "+bamt);
}
}
public class Psgbank {

public static void main(String[] args) {


bank b=new bank();
[Link]();
[Link]();
[Link]();
}
}

Output :

Enter Balance Amount:


10000
Enter Deposit Amount:
5000
Current Balance: 15000.0
Enter Withdrawal Amount:
2500
Current Balance: 12500.0
EXP : 1(b)

class shape
{
int sarea;
shape(int a)
{
sarea=a*a;
[Link]("Area of Square: "+sarea);
}
shape(int l ,int b)
{
sarea=l*b;
[Link]("Area of Rectangle: "+sarea);
}
}
public class Constructor {

public static void main(String[] args) {


shape s=new shape(7);
shape s1=new shape(9,10);
}
}

Output :

Area of Square: 49
Area of Rectangle: 90
EXP : 2(a)

import [Link].*;
class semester
{
int rno,m1,m2,m3;
void getmarks()
{
Scanner inp=new Scanner([Link]);
[Link]("Enter Roll No: ");
rno=[Link]();
[Link]("Enter 1st subject marks: ");
m1=[Link]();
[Link]("Enter 2nd subject marks: ");
m2=[Link]();
[Link]("Enter 3nd subject marks: ");
m3=[Link]();
}
}
class sports extends semester
{
int smark,total;
void getsports()
{
smark=10;
}
void calculate()
{
total=m1+m2+m3+smark;
[Link]("Rollno: "+rno);
[Link]("Total mark: "+total);
}
}
public class Student {

public static void main(String[] args)


{
sports s=new sports();
[Link]();
[Link]();
[Link]();
}
}
Output :

Enter Roll No:


23
Enter 1st subject marks:
63
Enter 2nd subject marks:
54
Enter 3nd subject marks:
71
Rollno: 23
Total mark: 198
EXP : 2(b)

import [Link].*;
class employee
{
float bpay,hra,da,pf;
void get()
{
Scanner inp=new Scanner([Link]);
[Link]("Enter Basic Pay: ");
bpay=[Link]();
[Link]("Enter HRA,DA,PF: ");
hra=[Link]();
da=[Link]();
pf=[Link]();
}
}
interface bonus
{
final int b=100;
}
class salary extends employee implements bonus
{
float gpay,npay;
void calculate()
{
get();
gpay=bpay+hra+da+b;
npay=gpay-pf;
[Link]("Gross pay="+gpay);
[Link]("Net pay="+npay);
}
}
public class Company {
public static void main(String[] args) {
salary s=new salary();
[Link]();
}
}

Output :

Enter Basic Pay:


15000
Enter HRA,DA,PF:
5000
2500
2000
Gross pay=22600.0
Net pay=20600.0
EXP : 3

import [Link].*;
class invalidage extends Exception
{
public invalidage(String msg)
{
super(msg);
}
}
public class Customerexception {

public static void main(String[] args) {


int age;
try
{
Scanner inp=new Scanner([Link]);
[Link]("Enter Age: ");
age=[Link]();
if(age<18)
throw new invalidage("Age must be greater than 18 for vote");
else
[Link]("Eligible to vote");
}
catch(invalidage e)
{
[Link]("Caught Exception:"+[Link]());
}
}
}

Output :

Enter Age:
12
Caught Exception:Age must be greater than 18 for vote

Enter Age:
20
Eligible to vote
EXP : 4

class two extends Thread


{
public void run()
{
for(int i=1;i<=5;i++)
[Link](i+" x 2 = "+i*2);
}
}
class three extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
[Link](i+" x 3 = "+i*3);
}
}
class five extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
[Link](i+" x 5 = "+i*5);
}
}
public class Multiplication {
public static void main(String[] args) {
two t1=new two();
three t2=new three();
five t3=new five();
[Link]();
[Link]();
[Link]();
}
}
Output :

1x2=2
2x2=4
3x2=6
4x2=8
5 x 2 = 10
1x3=3
2x3=6
3x3=9
4 x 3 = 12
5 x 3 = 15
1x5=5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25

Common questions

Powered by AI

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 .

You might also like