0% found this document useful (0 votes)
25 views26 pages

Java Programs for Area, Sorting, and Banking

Uploaded by

kamarajlove5
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)
25 views26 pages

Java Programs for Area, Sorting, and Banking

Uploaded by

kamarajlove5
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

1.

PROGRAM TO FIND AREA OF SQUARE, RECTANGLE AND CIRCLE


USING METHOD OVERLOADING

FILE NAME : [Link]

import [Link].*;
class area

{
void findarea(int a)
{
[Link]("The area of Square with a side value "+a+ " is:"+a*a);
}
void findarea(int a,int b)
{
[Link]("The Area of rectangle with Width "+a+ " and Length "+b+" is:"+a*b);
}
void findarea(float a)
{
[Link]("The area of Circle with the Radius value "+a+" is: "+((3.14)*(a*a)));
}
public static void main(String args[])throws IOException
{
area d=new area();
int choice;

//BufferedReader Br=new BufferedReader(new InputStreamReader([Link]));

DataInputStream Br=new DataInputStream([Link]);


[Link]("Finding the areas of different shape Using Method Overloading");

do
{
[Link]();
[Link]("1. Square");
[Link]("2. Rectangle");
[Link]("3. Circle");
[Link]("4. Exit");
[Link]();
[Link]("Please select your choice:");
choice=[Link]([Link]());

switch(choice)
{
case 1:

[Link]("Enter a side value of a square:");


int a=[Link]([Link]());
[Link](a);
break;
case 2:

[Link]("Enter the Width of the Rectangle:");


int x=[Link]([Link]());
[Link]("Enter the Length of the Rectangle:");
int y=[Link]([Link]());
[Link](x,y);
break;

case 3:

[Link]("Enter the radius of circle in float :");


float r=[Link]([Link]());
[Link](r);
break;

case 4:

[Link](0);
break;

default:
[Link]("Invalid choice");
}
}
while(choice<=4);
}
OUTPUT:
C:\JavaLab>javac [Link]
Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:\JavaLab>java area
Finding the areas of different shape Using Method Overloading
1. Square
2. Rectangle
3. Circle
4. Exit
Please select your choice:
1
Enter a side value of a square:
5
The area of Square with a side value 5 is:25
1. Square
2. Rectangle
3. Circle
4. Exit
Please select your choice:
2
Enter the Width of the Rectangle:
4
Enter the Length of the Rectangle:
3
The Area of rectangle with Width 4 and Length 3 is:12
1. Square
2. Rectangle
3. Circle
4. Exit
Please select your choice:
3
Enter the radius of circle in float :
5.4
The area of Circle with the Radius value 5.4 is: 91.56240550994873
1. Square
2. Rectangle
3. Circle
4. Exit
Please select your choice:
4
[Link] TO SORT THE LIST OF NUMBERS USING COMMAND LINE
ARGUMENTS

FILE NAME : [Link]

public class Sorting


{
public static void main(String args[])
{
if([Link]<=0)
{
[Link]("Error: Enter some integer as command line arguments");
[Link](0);
}

[Link]("Sorting Set of Numbers Using Commandline Arguments:");


int n=[Link];
int a[]=new int[n];
int temp;
[Link]("Original Order:");

for (int i = 0; i < n; i++)


{
a[i] = [Link](args[i]);
[Link](a[i]+",");
}

for (int i = 0; i < n; i++)


{
for (int j = i + 1; j < n; j++)
{
if (a[i]> a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

[Link]("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
[Link](a[i] + ";");
}
[Link](a[n - 1]);
}
}
OUTPUT :

C:\JavaLab>javac [Link]
C:\JavaLab>java Sorting 9 6 0 7 5 3 1

Sorting Set of Numbers Using Commandline Arguments:


Original Order:
9,6,0,7,5,3,1;Ascending Order:
0,1,3,5,6,7,9

NOTE :
o We have to enter the numbers that we want to be Sorted, after the command. That is
[java Sorting] (numbers).
o Also ,we have to leave space between two numbers, Otherwise it will take the given
numbers as one single number.
3. PROGRAM TO MULTIPLY GIVEN TWO MATRICES

FILE NAME : [Link]

import [Link];
public class MatrixMultiplication
{

public static void main(String args[])


{
int n;
Scanner input=new Scanner([Link]);
[Link]("Enter the base of squared matrices:");
n=[Link]();
int[][] a=new int[n][n];
int[][] b=new int[n][n];
int[][] c=new int[n][n];

[Link]("Enter the elements of 1st matrix row wise\n");


for(int i=0; i<n;i++)
{
for(int j=0; j<n;j++)
{
a[i][j]=[Link]();
}
}

[Link]("Enter the elements of 2nd matrix row wise\n");


for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
b[i][j]=[Link]();
}
}

[Link]("Given Matrix:");
[Link]("First Matrix:");

for(int i=0; i<n; i++)


{
for(int j=0;j<n;j++)
{
[Link](a[i][j]+" ");
}
[Link]();
}

[Link]("Second Matrix:");
for(int i=0; i<n; i++)
{
for(int j=0;j<n;j++)
{
[Link](a[i][j]+" ");
}
[Link]();
}

[Link]("Multiplying the matrices.....");


for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}

[Link]("The product is:");


for(int i=0; i<n; i++)
{
for(int j=0;j<n;j++)
{
[Link](c[i][j]+" ");
}
[Link]();
}
[Link]();
}
}
OUTPUT :
C:\JavaLab>javac [Link]
C:\JavaLab>java MatrixMultiplication

Enter the base of squared matrices:


2
Enter the elements of 1st matrix row wise
1
2
3
4
Enter the elements of 2nd matrix row wise
1
2
3
4
Given Matrix:
First Matrix:
12
34
Second Matrix:
12
34
Multiplying the matrices.....
The product is:
7 10
15 22

NOTE :
o We have to enter the numbers one by one in the command line “Enter the Elements of
matrices”.
o Also ,We need to leave space between two numbers in the command “First Matrix:”
and “Second Matrix”.
4. BANKING DETAILS USING CLASS AND OBJECTS

FILE NAME : [Link]

import [Link].*;
class account
{
int Acc_Number;
String Cus_Name,Acc_Type;
double balance;
account(String name, String type, double amount)
{
Acc_Number=1000;
Cus_Name=name;
Acc_Type=type;
balance=amount;
}

public void display()


{
[Link]("************************************************************");
[Link]("Acc No\tName\t\tAccount Type\tBalance");
[Link](Acc_Number+"\t"+Cus_Name+"\t\t"+Acc_Type+"\t\t"+balance);
[Link]("************************************************************");
}

public void deposit(double amt)


{
balance+=amt;
[Link]("Your deposit was successful.");
}

public void withdraw(double money)


{
if(balance<money)
{
[Link]("Insufficient money");
}

else
{
balance-=money;
[Link]("Your withdraw Rs :"+money+" was successful.");
}
}
}

class banking
{
public static void main(String args[])throws IOException
{
DataInputStream dis=new DataInputStream([Link]);
int Choice;
[Link]("\t***************************");
[Link]("\tABC Banking Private Limited");
[Link]("\t***************************");
[Link]("Kindly Provide the following informations to create a new account");
[Link]("Enter your name:");
String name=[Link]();
[Link]("Enter your account type:");
String type=[Link]();
[Link]("What is your initial deposit amount?");
double amount=[Link]([Link]());
account obj=new account(name,type,amount);
[Link]();
[Link]("Thank you "+obj.Cus_Name+" your new account with us has been created
successfully.");
[Link]();
[Link]();

do
{
[Link]();
[Link]("1. To Deposit Money");
[Link]("2. To Withdraw Money");
[Link]("3. To Check Account Balance");
[Link]("4. To Exit");
[Link]();
[Link]("Enter your choice:");
Choice=[Link]([Link]());

switch(Choice)
{
case 1:

[Link]("How many rupees do you want to deposit?");


amount=[Link]([Link]());
[Link](amount);
break;

case 2:

[Link]("How many rupees do you want to withdraw?");


amount=[Link]([Link]());
[Link](amount);
break;

case 3:

[Link]();
break;

case 4:

[Link](0);
default:
[Link]("Your choice was wrong");
}
}while(Choice<=4);
}
}
OUTPUT :

C:\JavaLab>javac [Link]
Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:\JavaLab>java banking

***************************
ABC Banking Private Limited
***************************
Kindly Provide the following informations to create a new account

Enter your name:


Kumar

Enter your account type:


Savings

What is your initial deposit amount?


5000

Thank you Kumar your new account with us has been created successfully.

************************************************************
Acc No Name Account Type Balance
1000 Kumar Savings 5000.0
************************************************************
1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit

Enter your choice:


1
How many rupees do you want to deposit?
2000
Your deposit was successful.

1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit

Enter your choice:


3
************************************************************
Acc No Name Account Type Balance
1000 Kumar Savings 7000.0
************************************************************
1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit

Enter your choice:


2
How many rupees do you want to withdraw?
3000
Your withdraw Rs :3000.0 was successful.

1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit

Enter your choice:


3
************************************************************
Acc No Name Account Type Balance
1000 Kumar Savings 4000.0
************************************************************

1. To Deposit Money
2. To Withdraw Money
3. To Check Account Balance
4. To Exit

Enter your choice:


4
5. DEMONSTRATION OF USER DEFINED PACKAGES

FILE NAME : [Link]

Source Code:
[Link]
package Calc;
public class Arithmetic
{
public int addition(int num1,int num2)
{
return (num1+num2);
}
public int subtraction(int num1,int num2)
{
return (num1-num2);
}
public int multiplication(int num1,int num2)
{
return (num1*num2);
}
public int division(int num1,int num2)
{
return (num1/num2);
}
}

[Link]
import [Link].*;
import [Link];
class PackageDemo
{
public static void main(String args[])throws IOException
{
DataInputStream dis=new DataInputStream([Link]);
int num1,num2,add,sub,mul,div;
Arithmetic obj=new Arithmetic();
[Link]("\t*********************");
[Link]("\tPackage Demonstration");
[Link]("\t*********************");
[Link]("Enter two integer number:");
num1=[Link]([Link]());
num2=[Link]([Link]());
add=[Link](num1,num2);
sub=[Link](num1,num2);
mul=[Link](num1,num2);
div=[Link](num1,num2);
[Link]("The Addition is: "+add);
[Link]("The Subtraction is: "+sub);
[Link]("The Multiplication is: "+mul);
[Link]("The Division is: "+div);
}
}
OUTPUT:

C:\JavaLab>cd Calc
C:\JavaLab\Calc>javac [Link]
C:\JavaLab\Calc>cd..
C:\JavaLab>javac [Link]
Note: [Link] uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:\JavaLab>java PackageDemo

*********************
Package Demonstration
*********************
Enter two integer number:
10
5

The Addition is: 15


The Subtraction is: 5
The Multiplication is: 50
The Division is: 2

NOTE :
o First We have to create a folder named Calc, Then create two java files.
o One with Sourse code named [Link]
o Next with [Link].
6. DEMONSTRATION OF EXCEPTION HANDLING

FILE NAME : [Link]

import [Link].*;
import [Link].*;
class ExceptionHandling
{
public static void main(String args[])
{
try
{
int a=[Link](args[0]);
int b=[Link](args[1]);
[Link]("Answer:"+a/b);
}
catch(ArithmeticException e)
{
[Link]("\n\tDivision Error");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("\n\tError in index value");
}
catch(NumberFormatException e)
{
[Link]("\n\tData type error");
}
finally
{
[Link]("\n\tFinally block executed");
}
}
}
OUTPUT:

C:\JavaLab>javac [Link]
C:\JavaLab>java ExceptionHandling 10 5
Answer:2
Finally block executed

C:\JavaLab>java ExceptionHandling 10 0
Division Error
Finally block executed

C:\JavaLab>java ExceptionHandling
Error in index value
Finally block executed

C:\JavaLab>java ExceptionHandling 10 a
Data type error
Finally block executed

NOTE :
o We have to leave space between two numbers in the command line, Otherwise it
will take the given numbers as one single number.
7. USES OF MULTITHREADS

FILE NAME : [Link]

import [Link].*;
class Even extends Thread
{
void Even()
{
start();
}
public void run()
{
for(int j=2;j<10;j=j+2)
{
[Link]("\n\t\t Even number:"+j);
try
{
sleep(2000);
}
catch(InterruptedException e)
{
}
}}}
class Odd extends Thread
{
void Odd()
{
start();
}
public void run()
{
for(int i=1;i<10;i=i+2)
{
[Link]("\n\t\t Odd number:"+i);
try
{
sleep(1000);
}
catch(InterruptedException e)
{
}
}}}
class MultiThread
{
public static void main(String args[])
{
[Link]("\n\t\t Multithread Programming\n");
Even r=new Even();
Odd b=new Odd();
[Link]();
[Link]();
}
}
OUTPUT :

C:\JavaLab>javac [Link]
C:\JavaLab>java MultiThread

Multithread Programming

Odd number:1

Even number:2

Odd number:3

Odd number:5

Even number:4

Odd number:7

Odd number:9

Even number:6

Even number:8
8. STUDENT REGISTRATION FORM USING APPLETS

FILE NAME : [Link]

mport [Link].*;
import [Link].*;
import [Link].*;

/* <applet code="[Link]" width=600 height=500>


</applet> */

public class Student extends Applet

{
TextField t3,t4,t5;
Button b1,b2;
Checkbox c1,c2,c3,c4,m,f;
CheckboxGroup cbg;

List l1;
Label l2,l3,l4,l5,l6;
public void init()
{
setLayout(null);
l2=new Label("NAME");
[Link](0,0,50,50);

add(l2);
t3=new TextField(20);
[Link](130,10,150,20);

add(t3);
l3=new Label("ADDRESS");
[Link](0,40,70,50);

add(l3);
t4=new TextField(20);
[Link](130,50,150,20);

add(t4);
l4=new Label("SEX");
[Link](0,80,70,50);

add(l4);
cbg=new CheckboxGroup();
m=new Checkbox("Male",false,cbg);
[Link](130,90,75,20);

add(m);
f=new Checkbox("Female",false,cbg);
[Link](225,90,75,20);

add(f);
l5=new Label("Class");
[Link](0,120,120,50);
add(l5);
l6=new Label("E-Mail");
[Link](0,160,120,50);

add(l6);
t5=new TextField(20);
[Link](130,175,150,20);

add(t5);
l1=new List(1,false);
[Link]("I Year");
[Link]("II Year");
[Link]("III Year");
[Link](130,130,100,20);

add(l1);
b1= new Button("SUBMIT");
[Link](80,250,70,20);

add(b1);
b2= new Button("RESET");
[Link](200,250,70,20);

add(b2);
}
}
OUTPUT :
C:\JavaLab>javac [Link]
C:\JavaLab>appletviewer [Link]

NOTE :
o If you spot any Errors indicating the line with “l1” .
o Check them You may have typed them as 11 or LL, Because they seems like
eachother.
o It is actually “L1” means ‘Line 1’.
9. DRAWING VARIOUS SHAPES USING GRAPHICS CLASS

FILE NAME : [Link]

import [Link].*;
import [Link].*;

/*<applet code="graphics" width="800" height="600">


</applet>*/

public class graphics extends Applet


{
Font f1=new Font("Courier New",[Link],20);

public void paint(Graphics GA)


{
[Link]([Link]);
[Link](f1);
[Link]("Illustration of methods of Graphics class",130,20);
[Link](450,55,200,260);
[Link](300,375,110,160);

[Link](10,120,155,95);
[Link]([Link]);
[Link](700,140,50,150);

[Link]([Link]);
[Link](340,100,340,350);

[Link]([Link]);
[Link](510,400,90,100,20,20);
}
}
OUTPUT :

C:\JavaLab>javac [Link]
C:\JavaLab>appletviewer [Link]
10. WRITE A PROGRAM TO CREATE A SEQUENTIAL FILE THAT COULD STORE
DETAILS ABOUT FIVE PRODUCTS. DETAILS INCLUDE PRODUCT CODE, COST, AND
NUMBER OF ITEMS AVAILABLE AND ARE PROVIDED THROUGH THE KEYBOARD.
COMPAUTE AND PRINT THE TOTAL VALUE OF ALL THE FIVE PRODUCTS.

FILE NAME: [Link]


import [Link].*;

class file

public static void main(String args[])

FileReader intfile=null;

FileWriter outfile=null;

try

outfile=new FileWriter("[Link]");

[Link]("Enter Products code:cost \n number of items for five Products");

for(int i=0;i<5;i++)

InputStreamReader r=new InputStreamReader([Link]);

BufferedReader br=new BufferedReader(r);

[Link]([Link]());

catch(FileNotFoundException e)

[Link]("\n\n\n\n\t\t File not found");

catch(IOException e)

[Link](e);

}
finally

try

[Link]();

catch(IOException e)

[Link]("Error");

}
OUTPUT :

Computer 223 : 222

Graphics card 221: 3334

Mouse 112: 2234

Keyboard 113: 2345

Pen drive 114: 9876

You might also like